// Run implements Command.Run func (c *modelsCommand) Run(ctx *cmd.Context) error { if c.user == "" { accountDetails, err := c.ClientStore().AccountByName( c.ControllerName(), c.AccountName(), ) if err != nil { return err } c.user = accountDetails.User } // First get a list of the models. var models []base.UserModel var err error if c.all { models, err = c.getAllModels() } else { models, err = c.getUserModels() } if err != nil { return errors.Annotate(err, "cannot list models") } // And now get the full details of the models. paramsModelInfo, err := c.getModelInfo(models) if err != nil { return errors.Annotate(err, "cannot get model details") } now := time.Now() modelInfo := make([]common.ModelInfo, 0, len(models)) for _, info := range paramsModelInfo { model, err := common.ModelInfoFromParams(info, now) if err != nil { return errors.Trace(err) } modelInfo = append(modelInfo, model) } modelSet := ModelSet{Models: modelInfo} current, err := c.ClientStore().CurrentModel(c.ControllerName(), c.AccountName()) if err != nil && !errors.IsNotFound(err) { return err } modelSet.CurrentModel = current if err := c.out.Write(ctx, modelSet); err != nil { return err } if len(models) == 0 && c.out.Name() == "tabular" { // When the output is tabular, we inform the user when there // are no models available, and tell them how to go about // creating or granting access to them. fmt.Fprintf(ctx.Stderr, "\n%s\n\n", errNoModels.Error()) } return nil }
func (c *showModelCommand) apiModelInfoToModelInfoMap(modelInfo []params.ModelInfo) (map[string]common.ModelInfo, error) { now := time.Now() output := make(map[string]common.ModelInfo) for _, info := range modelInfo { out, err := common.ModelInfoFromParams(info, now) if err != nil { return nil, errors.Trace(err) } output[out.Name] = out } return output, nil }
func (c *showModelCommand) apiModelInfoToModelInfoMap(modelInfo []params.ModelInfo) (map[string]common.ModelInfo, error) { // TODO(perrito666) 2016-05-02 lp:1558657 now := time.Now() output := make(map[string]common.ModelInfo) for _, info := range modelInfo { out, err := common.ModelInfoFromParams(info, now) if err != nil { return nil, errors.Trace(err) } out.ControllerName = c.ControllerName() output[out.Name] = out } return output, nil }
// Run implements Command.Run func (c *modelsCommand) Run(ctx *cmd.Context) error { accountDetails, err := c.ClientStore().AccountDetails(c.ControllerName()) if err != nil { return err } c.loggedInUser = accountDetails.User // First get a list of the models. var models []base.UserModel if c.all { models, err = c.getAllModels() } else { if c.user == "" { c.user = accountDetails.User } models, err = c.getUserModels() } if err != nil { return errors.Annotate(err, "cannot list models") } // And now get the full details of the models. paramsModelInfo, err := c.getModelInfo(models) if err != nil { return errors.Annotate(err, "cannot get model details") } // TODO(perrito666) 2016-05-02 lp:1558657 now := time.Now() modelInfo := make([]common.ModelInfo, 0, len(models)) for _, info := range paramsModelInfo { model, err := common.ModelInfoFromParams(info, now) if err != nil { return errors.Trace(err) } model.ControllerName = c.ControllerName() modelInfo = append(modelInfo, model) } modelSet := ModelSet{Models: modelInfo} current, err := c.ClientStore().CurrentModel(c.ControllerName()) if err != nil && !errors.IsNotFound(err) { return err } modelSet.CurrentModelQualified = current modelSet.CurrentModel = current if c.user != "" { userForListing := names.NewUserTag(c.user) unqualifiedModelName, owner, err := jujuclient.SplitModelName(current) if err == nil { modelSet.CurrentModel = common.OwnerQualifiedModelName( unqualifiedModelName, owner, userForListing, ) } } if err := c.out.Write(ctx, modelSet); err != nil { return err } if len(models) == 0 && c.out.Name() == "tabular" { // When the output is tabular, we inform the user when there // are no models available, and tell them how to go about // creating or granting access to them. fmt.Fprintln(ctx.Stderr, noModelsMessage) } return nil }