func (*filesSuite) TestCurrentCommenctionNameEnvironment(c *gc.C) { err := modelcmd.WriteCurrentModel("fubar") c.Assert(err, jc.ErrorIsNil) name, isController, err := modelcmd.CurrentConnectionName() c.Assert(err, jc.ErrorIsNil) c.Assert(isController, jc.IsFalse) c.Assert(name, gc.Equals, "fubar") }
func (*filesSuite) TestCurrentCommenctionNameController(c *gc.C) { err := modelcmd.WriteCurrentController("baz") c.Assert(err, jc.ErrorIsNil) name, isController, err := modelcmd.CurrentConnectionName() c.Assert(err, jc.ErrorIsNil) c.Assert(isController, jc.IsTrue) c.Assert(name, gc.Equals, "baz") }
func (c *switchCommand) Run(ctx *cmd.Context) error { // Switch is an alternative way of dealing with environments than using // the JUJU_MODEL environment setting, and as such, doesn't play too well. // If JUJU_MODEL is set we should report that as the current environment, // and not allow switching when it is set. // Passing through the empty string reads the default environments.yaml file. // If the environments.yaml file doesn't exist, just list environments in // the configstore. envFileExists := true names := set.NewStrings() environments, err := environs.ReadEnvirons("") if err != nil { if !environs.IsNoEnv(err) { return errors.Annotate(err, "couldn't read the model") } envFileExists = false } else { for _, name := range environments.Names() { names.Add(name) } } configEnvirons, configControllers, err := getConfigstoreOptions() if err != nil { return err } names = names.Union(configEnvirons) names = names.Union(configControllers) if c.List { // List all environments and controllers. if c.ModelName != "" { return errors.New("cannot switch and list at the same time") } for _, name := range names.SortedValues() { if configControllers.Contains(name) && !configEnvirons.Contains(name) { name += controllerSuffix } fmt.Fprintf(ctx.Stdout, "%s\n", name) } return nil } jujuEnv := os.Getenv("JUJU_MODEL") if jujuEnv != "" { if c.ModelName == "" { fmt.Fprintf(ctx.Stdout, "%s\n", jujuEnv) return nil } else { return errors.Errorf("cannot switch when JUJU_MODEL is overriding the model (set to %q)", jujuEnv) } } current, isController, err := modelcmd.CurrentConnectionName() if err != nil { return errors.Trace(err) } if current == "" { if envFileExists { current = environments.Default } } else if isController { current += controllerSuffix } // Handle the different operation modes. switch { case c.ModelName == "" && current == "": // Nothing specified and nothing to switch to. return errors.New("no currently specified model") case c.ModelName == "": // Simply print the current environment. fmt.Fprintf(ctx.Stdout, "%s\n", current) return nil default: // Switch the environment. if !names.Contains(c.ModelName) { return errors.Errorf("%q is not a name of an existing defined model or controller", c.ModelName) } // If the name is not in the environment set, but is in the controller // set, then write the name into the current controller file. logger.Debugf("controllers: %v", configControllers) logger.Debugf("models: %v", configEnvirons) newEnv := c.ModelName if configControllers.Contains(newEnv) && !configEnvirons.Contains(newEnv) { return modelcmd.SetCurrentController(ctx, newEnv) } return modelcmd.SetCurrentModel(ctx, newEnv) } }
func (*filesSuite) TestCurrentCommenctionNameMissing(c *gc.C) { name, isController, err := modelcmd.CurrentConnectionName() c.Assert(err, jc.ErrorIsNil) c.Assert(isController, jc.IsFalse) c.Assert(name, gc.Equals, "") }