func (*jenvSuite) TestSuccess(c *gc.C) { // Create a jenv file. contents := makeJenvContents("who", "Secret!", "env-UUID", testing.CACert, "1.2.3.4:17070") f := openJenvFile(c, contents) defer f.Close() // Import the newly created jenv file. jenvCmd := &environment.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, jc.ErrorIsNil) // The jenv file has been properly imported. assertJenvContents(c, contents, "testing") // The default environment is now the newly imported one, and the output // reflects the change. c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "testing") c.Assert(testing.Stdout(ctx), gc.Equals, "erewhemos -> testing\n") // Trying to import the jenv with the same name a second time raises an // error. jenvCmd = &environment.JenvCommand{} ctx, err = testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, gc.ErrorMatches, `an environment named "testing" already exists: you can provide a second parameter to rename the environment`) // Overriding the environment name solves the problem. jenvCmd = &environment.JenvCommand{} ctx, err = testing.RunCommand(c, jenvCmd, f.Name(), "another") c.Assert(err, jc.ErrorIsNil) assertJenvContents(c, contents, "another") c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "another") c.Assert(testing.Stdout(ctx), gc.Equals, "testing -> another\n") }
func (*SwitchSimpleSuite) TestSettingWritesFile(c *gc.C) { testing.WriteEnvironments(c, testing.MultipleEnvConfig) context, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2") c.Assert(err, jc.ErrorIsNil) c.Assert(testing.Stdout(context), gc.Equals, "erewhemos -> erewhemos-2\n") c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "erewhemos-2") }
func (s *BootstrapSuite) TestBootstrapSetsCurrentEnvironment(c *gc.C) { const envName = "devenv" s.patchVersionAndSeries(c, envName) coretesting.WriteEnvironments(c, coretesting.MultipleEnvConfig) ctx, err := coretesting.RunCommand(c, newBootstrapCommand(), "-e", "devenv", "--auto-upgrade") c.Assert(coretesting.Stderr(ctx), jc.Contains, "-> devenv") currentEnv, err := envcmd.ReadCurrentEnvironment() c.Assert(err, jc.ErrorIsNil) c.Assert(currentEnv, gc.Equals, "devenv") }
func (s *UseEnvironmentSuite) TestUseEnvAlreadyExistingSameEnv(c *gc.C) { s.makeLocalEnvironment(c, "unique", "some-uuid", "tester") ctx, err := s.run(c, "unique") c.Assert(err, gc.IsNil) message := strings.TrimSpace(testing.Stderr(ctx)) lines := strings.Split(message, "\n") c.Assert(lines, gc.HasLen, 2) expected := `You already have environment details for "unique" cached locally.` c.Assert(lines[0], gc.Equals, expected) c.Assert(lines[1], gc.Equals, `fake (system) -> unique`) current, err := envcmd.ReadCurrentEnvironment() c.Assert(err, gc.IsNil) c.Assert(current, gc.Equals, "unique") }
func EnvClientFactory() (*Client, error) { envName := envcmd.ReadCurrentEnvironment() state, err := juju.NewAPIFromName(envName) if err != nil { log.Warn("Got error building API from name: %v", envName, err) return nil, fmt.Errorf(connectionError, envName, err) } client := state.Client() wrapper := &Client{} wrapper.client = client wrapper.apiState = state //defer apiclient.Close() return wrapper, err }
// getDefaultEnvironment is copied from github.com/juju/juju/cmd/envcmd/environmentcommand.go func getDefaultEnvironment() (string, error) { if defaultEnv := os.Getenv(osenv.JujuEnvEnvKey); defaultEnv != "" { return defaultEnv, nil } if currentEnv := envcmd.ReadCurrentEnvironment(); currentEnv != "" { return currentEnv, nil } envs, err := environs.ReadEnvirons("") if environs.IsNoEnv(err) { // That's fine, not an error here. return "", nil } if err != nil { return "", errors.Trace(err) } return envs.Default, nil }
func (s *UseEnvironmentSuite) assertCurrentEnvironment(c *gc.C, name, uuid string) { current, err := envcmd.ReadCurrentEnvironment() c.Assert(err, gc.IsNil) c.Assert(current, gc.Equals, name) store, err := configstore.Default() c.Assert(err, gc.IsNil) info, err := store.ReadInfo(name) c.Assert(err, gc.IsNil) c.Assert(info.APIEndpoint(), jc.DeepEquals, configstore.APIEndpoint{ Addresses: []string{"127.0.0.1:12345"}, Hostnames: []string{"localhost:12345"}, CACert: testing.CACert, EnvironUUID: uuid, ServerUUID: serverUUID, }) c.Assert(info.APICredentials(), jc.DeepEquals, s.creds) }
func (*jenvSuite) TestSuccessCustomEnvironmentName(c *gc.C) { // Create a jenv file. contents := makeValidJenvContents() f := openJenvFile(c, contents) defer f.Close() // Import the newly created jenv file with a customized name. jenvCmd := &environment.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name(), "my-env") c.Assert(err, jc.ErrorIsNil) // The jenv file has been properly imported. assertJenvContents(c, contents, "my-env") // The default environment is now the newly imported one, and the output // reflects the change. c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "my-env") c.Assert(testing.Stdout(ctx), gc.Equals, "erewhemos -> my-env\n") }
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. environments, err := environs.ReadEnvirons("") if err != nil { return errors.New("couldn't read the environment") } names := environments.Names() sort.Strings(names) if c.List { // List all environments. if c.EnvName != "" { return errors.New("cannot switch and list at the same time") } for _, name := range names { 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 fmt.Errorf("cannot switch when JUJU_ENV is overriding the environment (set to %q)", jujuEnv) } } currentEnv := envcmd.ReadCurrentEnvironment() if currentEnv == "" { currentEnv = environments.Default } // Handle the different operation modes. switch { case c.EnvName == "" && currentEnv == "": // 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", currentEnv) default: // Switch the environment. if !validEnvironmentName(c.EnvName, names) { return fmt.Errorf("%q is not a name of an existing defined environment", c.EnvName) } if err := envcmd.WriteCurrentEnvironment(c.EnvName); err != nil { return err } if currentEnv == "" { fmt.Fprintf(ctx.Stdout, "-> %s\n", c.EnvName) } else { fmt.Fprintf(ctx.Stdout, "%s -> %s\n", currentEnv, c.EnvName) } } return nil }
func (s *EnvironmentCommandSuite) TestReadCurrentEnvironmentSet(c *gc.C) { err := envcmd.WriteCurrentEnvironment("fubar") c.Assert(err, gc.IsNil) env := envcmd.ReadCurrentEnvironment() c.Assert(env, gc.Equals, "fubar") }
func (s *EnvironmentCommandSuite) TestReadCurrentEnvironmentUnset(c *gc.C) { env := envcmd.ReadCurrentEnvironment() c.Assert(env, gc.Equals, "") }
func (s *filesSuite) assertCurrentEnvironment(c *gc.C, environmentName string) { current, err := envcmd.ReadCurrentEnvironment() c.Assert(err, jc.ErrorIsNil) c.Assert(current, gc.Equals, environmentName) }