Ejemplo n.º 1
0
func (*filesSuite) TestCurrentCommenctionNameEnvironment(c *gc.C) {
	err := envcmd.WriteCurrentEnvironment("fubar")
	c.Assert(err, jc.ErrorIsNil)
	name, isController, err := envcmd.CurrentConnectionName()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(isController, jc.IsFalse)
	c.Assert(name, gc.Equals, "fubar")
}
Ejemplo n.º 2
0
func (*filesSuite) TestCurrentCommenctionNameController(c *gc.C) {
	err := envcmd.WriteCurrentController("baz")
	c.Assert(err, jc.ErrorIsNil)
	name, isController, err := envcmd.CurrentConnectionName()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(isController, jc.IsTrue)
	c.Assert(name, gc.Equals, "baz")
}
Ejemplo n.º 3
0
func (c *switchCommand) Run(ctx *cmd.Context) error {
	// Switch is an alternative way of dealing with environments than using
	// the JUJU_ENV environment setting, and as such, doesn't play too well.
	// If JUJU_ENV 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 environment")
		}
		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.EnvName != "" {
			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_ENV")
	if jujuEnv != "" {
		if c.EnvName == "" {
			fmt.Fprintf(ctx.Stdout, "%s\n", jujuEnv)
			return nil
		} else {
			return errors.Errorf("cannot switch when JUJU_ENV is overriding the environment (set to %q)", jujuEnv)
		}
	}

	current, isController, err := envcmd.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.EnvName == "" && current == "":
		// Nothing specified and nothing to switch to.
		return errors.New("no currently specified environment")
	case c.EnvName == "":
		// Simply print the current environment.
		fmt.Fprintf(ctx.Stdout, "%s\n", current)
		return nil
	default:
		// Switch the environment.
		if !names.Contains(c.EnvName) {
			return errors.Errorf("%q is not a name of an existing defined environment or controller", c.EnvName)
		}
		// 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("environs: %v", configEnvirons)
		newEnv := c.EnvName
		if configControllers.Contains(newEnv) && !configEnvirons.Contains(newEnv) {
			return envcmd.SetCurrentController(ctx, newEnv)
		}
		return envcmd.SetCurrentEnvironment(ctx, newEnv)
	}
}
Ejemplo n.º 4
0
func (*filesSuite) TestCurrentCommenctionNameMissing(c *gc.C) {
	name, isController, err := envcmd.CurrentConnectionName()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(isController, jc.IsFalse)
	c.Assert(name, gc.Equals, "")
}