Added format flag to convery output to JSON and slighly changed scanning logic

This commit is contained in:
David J. Allen 2024-05-10 13:41:08 -06:00
parent a2b841b401
commit b3ba75ea9a
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
3 changed files with 52 additions and 26 deletions

View file

@ -1,7 +1,9 @@
package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
@ -17,12 +19,19 @@ var listCmd = &cobra.Command{
if err != nil {
logrus.Errorf("failed toget probe results: %v\n", err)
}
for _, r := range probeResults {
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
format = strings.ToLower(format)
if format == "json" {
b, _ := json.Marshal(probeResults)
fmt.Printf("%s\n", string(b))
} else {
for _, r := range probeResults {
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
}
}
},
}
func init() {
listCmd.Flags().StringVar(&format, "format", "", "set the output format")
rootCmd.AddCommand(listCmd)
}