コード例 #1
0
ファイル: models.go プロジェクト: exekias/juju
// Run implements Command.Run
func (c *environmentsCommand) Run(ctx *cmd.Context) error {
	if c.user == "" {
		creds, err := c.getConnectionCredentials()
		if err != nil {
			return errors.Trace(err)
		}
		c.user = creds.User
	}

	var envs []base.UserModel
	var err error
	if c.all {
		envs, err = c.getAllModels()
	} else {
		envs, err = c.getUserModels()
	}
	if err != nil {
		return errors.Annotate(err, "cannot list models")
	}

	output := make([]UserModel, len(envs))
	now := time.Now()
	for i, env := range envs {
		output[i] = UserModel{
			Name:           env.Name,
			UUID:           env.UUID,
			Owner:          env.Owner,
			LastConnection: user.LastConnection(env.LastConnection, now, c.exactTime),
		}
	}

	return c.out.Write(ctx, output)
}
コード例 #2
0
ファイル: listmodels.go プロジェクト: AlexisBruemmer/juju
// 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
	}

	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")
	}

	modelDetails := make([]UserModel, len(models))
	now := time.Now()
	for i, model := range models {
		modelDetails[i] = UserModel{
			Name:           model.Name,
			UUID:           model.UUID,
			Owner:          model.Owner,
			LastConnection: user.LastConnection(model.LastConnection, now, c.exactTime),
		}
	}
	modelSet := ModelSet{
		Models: modelDetails,
	}
	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
}