本文整理汇总了Golang中github.com/juju/juju/cmd/envcmd.ReadCurrentEnvironment函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadCurrentEnvironment函数的具体用法?Golang ReadCurrentEnvironment怎么用?Golang ReadCurrentEnvironment使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadCurrentEnvironment函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSuccess
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")
}
示例2: TestSettingWritesFile
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")
}
示例3: TestBootstrapSetsCurrentEnvironment
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")
}
示例4: TestUseEnvAlreadyExistingSameEnv
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")
}
示例5: EnvClientFactory
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
}
示例6: getDefaultEnvironment
// 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
}
示例7: assertCurrentEnvironment
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)
}
示例8: TestSuccessCustomEnvironmentName
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")
}
示例9: Run
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
}
示例10: TestReadCurrentEnvironmentSet
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")
}
示例11: TestReadCurrentEnvironmentUnset
func (s *EnvironmentCommandSuite) TestReadCurrentEnvironmentUnset(c *gc.C) {
env := envcmd.ReadCurrentEnvironment()
c.Assert(env, gc.Equals, "")
}
示例12: assertCurrentEnvironment
func (s *filesSuite) assertCurrentEnvironment(c *gc.C, environmentName string) {
current, err := envcmd.ReadCurrentEnvironment()
c.Assert(err, jc.ErrorIsNil)
c.Assert(current, gc.Equals, environmentName)
}