func (c *statusHistoryCommand) Run(ctx *cmd.Context) error { apiclient, err := c.NewAPIClient() if err != nil { return fmt.Errorf(connectionError, c.ConnectionName(), err) } defer apiclient.Close() var statuses *params.UnitStatusHistory kind := params.HistoryKind(c.outputContent) statuses, err = apiclient.UnitStatusHistory(kind, c.unitName, c.backlogSize) if err != nil { if len(statuses.Statuses) == 0 { return errors.Trace(err) } // Display any error, but continue to print status if some was returned fmt.Fprintf(ctx.Stderr, "%v\n", err) } else if len(statuses.Statuses) == 0 { return errors.Errorf("no status history available") } table := [][]string{{"TIME", "TYPE", "STATUS", "MESSAGE"}} lengths := []int{1, 1, 1, 1} for _, v := range statuses.Statuses { fields := []string{common.FormatTime(v.Since, c.isoTime), string(v.Kind), string(v.Status), v.Info} for k, v := range fields { if len(v) > lengths[k] { lengths[k] = len(v) } } table = append(table, fields) } f := fmt.Sprintf("%%-%ds\t%%-%ds\t%%-%ds\t%%-%ds\n", lengths[0], lengths[1], lengths[2], lengths[3]) for _, v := range table { fmt.Printf(f, v[0], v[1], v[2], v[3]) } return nil }
func (c *statusHistoryCommand) Init(args []string) error { switch { case len(args) > 1: return errors.Errorf("unexpected arguments after unit name.") case len(args) == 0: return errors.Errorf("unit name is missing.") default: c.unitName = args[0] } // If use of ISO time not specified on command line, // check env var. if !c.isoTime { var err error envVarValue := os.Getenv(osenv.JujuStatusIsoTimeEnvKey) if envVarValue != "" { if c.isoTime, err = strconv.ParseBool(envVarValue); err != nil { return errors.Annotatef(err, "invalid %s env var, expected true|false", osenv.JujuStatusIsoTimeEnvKey) } } } kind := params.HistoryKind(c.outputContent) switch kind { case params.KindCombined, params.KindAgent, params.KindWorkload: return nil } return errors.Errorf("unexpected status type %q", c.outputContent) }