当前位置: 首页>>代码示例>>Golang>>正文


Golang envcmd.ReadCurrentEnvironment函数代码示例

本文整理汇总了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")
}
开发者ID:Pankov404,项目名称:juju,代码行数:33,代码来源:jenv_test.go

示例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")
}
开发者ID:Pankov404,项目名称:juju,代码行数:7,代码来源:switch_test.go

示例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")
}
开发者ID:imoapps,项目名称:juju,代码行数:11,代码来源:bootstrap_test.go

示例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")
}
开发者ID:snailwalker,项目名称:juju,代码行数:17,代码来源:useenvironment_test.go

示例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
}
开发者ID:jxaas,项目名称:jxaas,代码行数:17,代码来源:connection.go

示例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
}
开发者ID:macressler,项目名称:misc,代码行数:18,代码来源:juju-ping.go

示例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)
}
开发者ID:snailwalker,项目名称:juju,代码行数:19,代码来源:useenvironment_test.go

示例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")
}
开发者ID:Pankov404,项目名称:juju,代码行数:19,代码来源:jenv_test.go

示例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
}
开发者ID:jiasir,项目名称:juju,代码行数:64,代码来源:switch.go

示例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")
}
开发者ID:klyachin,项目名称:juju,代码行数:6,代码来源:environmentcommand_test.go

示例11: TestReadCurrentEnvironmentUnset

func (s *EnvironmentCommandSuite) TestReadCurrentEnvironmentUnset(c *gc.C) {
	env := envcmd.ReadCurrentEnvironment()
	c.Assert(env, gc.Equals, "")
}
开发者ID:klyachin,项目名称:juju,代码行数:4,代码来源:environmentcommand_test.go

示例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)
}
开发者ID:imoapps,项目名称:juju,代码行数:5,代码来源:files_test.go


注:本文中的github.com/juju/juju/cmd/envcmd.ReadCurrentEnvironment函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。