func (s *EnvironmentCommandSuite) TestWriteAddsNewline(c *C) { err := cmd.WriteCurrentEnvironment("fubar") c.Assert(err, IsNil) current, err := ioutil.ReadFile(cmd.GetCurrentEnvironmentFilePath()) c.Assert(err, IsNil) c.Assert(string(current), Equals, "fubar\n") }
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentBothSet(c *C) { os.Setenv(osenv.JujuEnv, "magic") err := cmd.WriteCurrentEnvironment("fubar") c.Assert(err, IsNil) env := cmd.GetDefaultEnvironment() c.Assert(env, Equals, "magic") }
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. jujuEnv := os.Getenv("JUJU_ENV") if jujuEnv != "" { if c.EnvName == "" { fmt.Fprintf(ctx.Stdout, "Current environment: %q (from JUJU_ENV)\n", jujuEnv) return nil } else { return fmt.Errorf("Cannot switch when JUJU_ENV is overriding the environment (set to %q)", jujuEnv) } } // Passing through the empty string reads the default environments.yaml file. environments, err := environs.ReadEnvirons("") if err != nil { return errors.New("couldn't read the environment.") } names := environments.Names() sort.Strings(names) currentEnv := cmd.ReadCurrentEnvironment() if currentEnv == "" { currentEnv = environments.Default } // In order to have only a set environment name quoted, make a small function env := func() string { if currentEnv == "" { return "<not specified>" } return fmt.Sprintf("%q", currentEnv) } if c.EnvName == "" || c.EnvName == currentEnv { fmt.Fprintf(ctx.Stdout, "Current environment: %s\n", env()) } else { // Check to make sure that the specified environment if !validEnvironmentName(c.EnvName, names) { return fmt.Errorf("%q is not a name of an existing defined environment", c.EnvName) } if err := cmd.WriteCurrentEnvironment(c.EnvName); err != nil { return err } fmt.Fprintf(ctx.Stdout, "Changed default environment from %s to %q\n", env(), c.EnvName) } if c.List { fmt.Fprintf(ctx.Stdout, "\nEnvironments:\n") for _, name := range names { fmt.Fprintf(ctx.Stdout, "\t%s\n", name) } } return nil }
func (*EnvironmentCommandSuite) TestErrorWritingFile(c *C) { // Can't write a file over a directory. os.MkdirAll(cmd.GetCurrentEnvironmentFilePath(), 0777) err := cmd.WriteCurrentEnvironment("fubar") c.Assert(err, ErrorMatches, "unable to write to the environment file: .*") }
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentCurrentEnvironmentSet(c *C) { err := cmd.WriteCurrentEnvironment("fubar") c.Assert(err, IsNil) env := cmd.GetDefaultEnvironment() c.Assert(env, Equals, "fubar") }