package cmd import ( "errors" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "strconv" "udico.de/uditaren/loadmaster2k/api" . "udico.de/uditaren/loadmaster2k/config" ) var cGet = &cobra.Command{ Use: "get ", Short: "gets the load of a given area", Args: cobra.ExactArgs(1), } var pRaw = false func init() { cGet.RunE = executeGet cGet.Flags().BoolVarP(&pRaw, "raw", "r", false, "Show raw response instead of the parsed result") cRoot.AddCommand(cGet) } func executeGet(cmd *cobra.Command, args []string) error { var tArea []*Area = nil if args[0] == "all" { for i,_ := range C.Areas { tArea = append(tArea, &C.Areas[i]) } } else { tAreaId, err := strconv.Atoi(args[0]) if err != nil { // not a number, maybe a name? tmp := C.GetAreaByName(args[0]) if tmp != nil { tArea = append(tArea, tmp) } } else { tmp := C.GetAreaById(tAreaId) if tmp != nil { tArea = append(tArea, tmp) } } } if len(tArea) == 0 { return errors.New("cannot find area or no areas configured") } err := getArea(tArea...) if err != nil { log.WithError(err).Error("cannot process areas") } return nil } func getArea(area ... *Area) error { for _, tArea := range area { tBody, err := api.FetchArea(tArea.Id) if err != nil { return err } if pRaw { log.Infof("%v: %v", tArea, tBody) } else { load, status := api.ParseAreaStatus(tBody) log.Infof("%v: Load=%v, Status='%v'", tArea, load, status) } } return nil }