// modelsCmdPreRun sets up the modelsCmdState and modelsCmdConfig objects
425257
func modelsCmdPreRun(cmd *cobra.Command, args []string) error {
	var err error
	cnf, err = config.Initialize(cmd.Flags().Lookup("env"))
	if err != nil {
		return err
	}

	cnf.ModeViper.BindPFlags(modelsCmd.Flags())

	err = cnf.CheckEnv()
	if err != nil {
		return err
	}

	return modelsCmdSetup(cmd, args)
}
579979
func commandsPubsub(m *Miniredis) {
	m.srv.Register("SUBSCRIBE", m.cmdSubscribe)
	m.srv.Register("UNSUBSCRIBE", m.cmdUnsubscribe)
	m.srv.Register("PSUBSCRIBE", m.cmdPsubscribe)
	m.srv.Register("PUNSUBSCRIBE", m.cmdPunsubscribe)
	m.srv.Register("PUBLISH", m.cmdPublish)
	m.srv.Register("PUBSUB", m.cmdPubSub)
}
885354
func (w *modelCommandWrapper) validateCommandForModelType(runStarted bool) error {
	_, iaasOnly := w.inner().(IAASOnlyCommand)
	_, caasOnly := w.inner().(CAASOnlyCommand)
	if !caasOnly && !iaasOnly {
		return nil
	}

	modelType, err := w.ModelCommand.ModelType()
	if err != nil {
		err = errors.Cause(err)
		// We need to error if Run() has been invoked the model is known and there was
		// some other error. If the model is not yet known, we'll grab the details
		// during the Run() API call later.
		if runStarted || (err != ErrNoModelSpecified && !errors.IsNotFound(err)) {
			return errors.Trace(err)
		}
		return nil
	}
	if modelType == model.CAAS && iaasOnly {
		err = errors.Errorf("Juju command %q not supported on kubernetes models", w.Info().Name)
	}
	if modelType == model.IAAS && caasOnly {
		err = errors.Errorf("Juju command %q not supported on non-container models", w.Info().Name)
	}

	if c, ok := w.inner().(modelSpecificCommand); ok {
		return c.IncompatibleModel(err)
	}
	return err
}
146919
func NewStatusCommand() cmd.Command {
	return modelcmd.Wrap(&statusCommand{
		checkProvidedIgnoredFlagF: func() set.Strings { return set.NewStrings() },
	})
}
806426
func (c *ControllerAPI) ModelConfig() (params.ModelConfigResults, error) {
	result := params.ModelConfigResults{}
	if err := c.checkHasAdmin(); err != nil {
		return result, errors.Trace(err)
	}

	controllerState := c.statePool.SystemState()
	controllerModel, err := controllerState.Model()
	if err != nil {
		return result, errors.Trace(err)
	}
	cfg, err := controllerModel.Config()
	if err != nil {
		return result, errors.Trace(err)
	}

	result.Config = make(map[string]params.ConfigValue)
	for name, val := range cfg.AllAttrs() {
		result.Config[name] = params.ConfigValue{
			Value: val,
		}
	}
	return result, nil
}
545466
func (m *Miniredis) cmdZremrangebyrank(c *server.Peer, cmd string, args []string) {
	if len(args) != 3 {
		setDirty(c)
		c.WriteError(errWrongNumber(cmd))
		return
	}
	if !m.handleAuth(c) {
		return
	}
	if m.checkPubsub(c) {
		return
	}

	key := args[0]
	start, err := strconv.Atoi(args[1])
	if err != nil {
		setDirty(c)
		c.WriteError(msgInvalidInt)
		return
	}
	end, err := strconv.Atoi(args[2])
	if err != nil {
		setDirty(c)
		c.WriteError(msgInvalidInt)
		return
	}

	withTx(m, c, func(c *server.Peer, ctx *connCtx) {
		db := m.db(ctx.selectedDB)

		if !db.exists(key) {
			c.WriteInt(0)
			return
		}

		if db.t(key) != "zset" {
			c.WriteError(ErrWrongType.Error())
			return
		}

		members := db.ssetMembers(key)
		rs, re := redisRange(len(members), start, end, false)
		for _, el := range members[rs:re] {
			db.ssetRem(key, el)
		}
		c.WriteInt(re - rs)
	})
}
770959
func (c *Client) ModelUserInfo() ([]params.ModelUserInfo, error) {
	var results params.ModelUserInfoResults
	err := c.facade.FacadeCall("ModelUserInfo", nil, &results)
	if err != nil {
		return nil, errors.Trace(err)
	}

	info := []params.ModelUserInfo{}
	for i, result := range results.Results {
		if result.Result == nil {
			return nil, errors.Errorf("unexpected nil result at position %d", i)
		}
		info = append(info, *result.Result)
	}
	return info, nil
}
150439
func FnRun(client *docker.Client, containerID, input string) (Stdout *bytes.Buffer, Stderr *bytes.Buffer, err error) {
	err = FnStart(client, containerID)
	if err != nil {
		return
	}

	// attach to write input
	_, err = FnAttach(client, containerID, strings.NewReader(input), nil, nil)
	if err != nil {
		return
	}

	e := FnWaitContainer(client, containerID)
	err = <-e

	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	// omit logs because execution error is more important
	_ = FnLogs(client, containerID, stdout, stderr) // nolint

	Stdout = stdout
	Stderr = stderr
	return
}
416469
func (c *ModelCommandBase) ModelUUIDs(modelNames []string) ([]string, error) {
	controllerName, err := c.ControllerName()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return c.CommandBase.ModelUUIDs(c.ClientStore(), controllerName, modelNames)
}
1294586
func (c *ModelConfigAPI) ModelGet() (params.ModelConfigResults, error) {
	result := params.ModelConfigResults{}
	if err := c.canReadModel(); err != nil {
		return result, errors.Trace(err)
	}

	values, err := c.backend.ModelConfigValues()
	if err != nil {
		return result, errors.Trace(err)
	}

	result.Config = make(map[string]params.ConfigValue)
	for attr, val := range values {
		// Authorized keys are able to be listed using
		// juju ssh-keys and including them here just
		// clutters everything.
		if attr == config.AuthorizedKeysKey {
			continue
		}
		result.Config[attr] = params.ConfigValue{
			Value:  val.Value,
			Source: val.Source,
		}
	}
	return result, nil
}
947327
func newCmdRunner(p runCmdProperties) (Runner, error) {
	if p.code == "" {
		return nil, errors.New("No code specified to run")
	}

	cmd := runCmd{}

	path, err := exec.LookPath(strings.Fields(p.runner)[0])
	if err != nil {
		return nil, err
	}
	cmd.Path = path
	cmd.Args = append(cmd.Args, strings.Fields(p.runner)...)
	err = cmd.writeTempFile(p.code, p.fileExtension)
	if err != nil {
		return nil, err
	}
	cmd.Args = append(cmd.Args, cmd.tmpFile)
	cmd.Dir = p.workdir
	cmd.Env = append(cmd.Env, os.Environ()...)
	cmd.Env = append(cmd.Env, p.env...)
	cmd.Stdin = os.Stdin

	return &cmd, nil
}
911639
func (c *Client) HostedModelConfigs() ([]HostedConfig, error) {
	result := params.HostedModelConfigsResults{}
	err := c.facade.FacadeCall("HostedModelConfigs", nil, &result)
	if err != nil {
		return nil, errors.Trace(err)
	}
	// If we get to here, we have some values. Each value may or
	// may not have an error, but it should at least have a name
	// and owner.
	hostedConfigs := make([]HostedConfig, len(result.Models))
	for i, modelConfig := range result.Models {
		hostedConfigs[i].Name = modelConfig.Name
		tag, err := names.ParseUserTag(modelConfig.OwnerTag)
		if err != nil {
			hostedConfigs[i].Error = errors.Trace(err)
			continue
		}
		hostedConfigs[i].Owner = tag
		if modelConfig.Error != nil {
			hostedConfigs[i].Error = errors.Trace(modelConfig.Error)
			continue
		}
		hostedConfigs[i].Config = modelConfig.Config
		spec, err := c.MakeCloudSpec(modelConfig.CloudSpec)
		if err != nil {
			hostedConfigs[i].Error = errors.Trace(err)
			continue
		}
		hostedConfigs[i].CloudSpec = spec
	}
	return hostedConfigs, err
}
659754
func cmdGlobalMachineState(cCmd *cobra.Command, globalUnits []schema.Unit) (err error) {
	cmd := cCmd.Name()
	mapUNs := map[string]string{}
	for _, unit := range globalUnits {
		m := cachedMachineState(unit.MachineID)
		if m == nil || m.ID == "" || m.PublicIP == "" {
			continue
		}
		mapUNs[m.ID] = unit.Name
	}

	// create a list of unique unit names
	resultIDs := map[string]string{}
	for id, name := range mapUNs {
		resultIDs[id] = name
	}

	for id, name := range resultIDs {
		// run a correspondent systemctl command
		if exitVal := runCommand(cCmd, id, "systemctl", cmd, name); exitVal != 0 {
			err = fmt.Errorf("Error running systemctl %s. machine id=%v, unit name=%s",
				cmd, id, name)
			break
		}
	}

	return err
}
1201080
func NewModelUserAccess(st *State, userDoc userAccessDoc) (permission.UserAccess, error) {
	perm, err := st.userPermission(modelKey(userDoc.ObjectUUID), userGlobalKey(strings.ToLower(userDoc.UserName)))
	if err != nil {
		return permission.UserAccess{}, errors.Annotate(err, "obtaining model permission")
	}
	return newUserAccess(perm, userDoc, names.NewModelTag(userDoc.ObjectUUID)), nil
}
678358
func RegisteredCmdMethods() []string {
	registerLock.Lock()
	defer registerLock.Unlock()

	methods := make([]string, 0, len(methodToInfo))
	for k := range methodToInfo {
		methods = append(methods, k)
	}

	sort.Sort(sort.StringSlice(methods))
	return methods
}
1092048
func (st modelManagerStateShim) ModelConfig() (*config.Config, error) {
	model, err := st.State.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return model.ModelConfig()
}
1307488
func (c *Client) ModelUserInfo() (params.ModelUserInfoResults, error) {
	var results params.ModelUserInfoResults
	if err := c.checkCanRead(); err != nil {
		return results, err
	}

	model, err := c.api.stateAccessor.Model()
	if err != nil {
		return results, errors.Trace(err)
	}
	users, err := model.Users()
	if err != nil {
		return results, errors.Trace(err)
	}

	for _, user := range users {
		var result params.ModelUserInfoResult
		userInfo, err := modelInfo(c.api.state(), user)
		if err != nil {
			result.Error = common.ServerError(err)
		} else {
			result.Result = &userInfo
		}
		results.Results = append(results.Results, result)
	}
	return results, nil
}