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


Golang cmd.Main函数代码示例

本文整理汇总了Golang中launchpad/net/juju-core/cmd.Main函数的典型用法代码示例。如果您正苦于以下问题:Golang Main函数的具体用法?Golang Main怎么用?Golang Main使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Main函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Main

// Main runs the Command specified by req, and fills in resp. A single command
// is run at a time.
func (j *Jujuc) Main(req Request, resp *Response) error {
	if req.CommandName == "" {
		return badReqErrorf("command not specified")
	}
	if !filepath.IsAbs(req.Dir) {
		return badReqErrorf("Dir is not absolute")
	}
	c, err := j.getCmd(req.ContextId, req.CommandName)
	if err != nil {
		return badReqErrorf("%s", err)
	}
	var stdin, stdout, stderr bytes.Buffer
	ctx := &cmd.Context{
		Dir:    req.Dir,
		Stdin:  &stdin,
		Stdout: &stdout,
		Stderr: &stderr,
	}
	j.mu.Lock()
	defer j.mu.Unlock()
	log.Infof("worker/uniter/jujuc: running hook tool %q %q", req.CommandName, req.Args)
	log.Debugf("worker/uniter/jujuc: hook context id %q; dir %q", req.ContextId, req.Dir)
	resp.Code = cmd.Main(c, ctx, req.Args)
	resp.Stdout = stdout.Bytes()
	resp.Stderr = stderr.Bytes()
	return nil
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:29,代码来源:server.go

示例2: TestRelationListHelp

func (s *RelationListSuite) TestRelationListHelp(c *C) {
	template := `
usage: relation-list [options] %s
purpose: list relation units

options:
--format  (= smart)
    specify output format (json|smart|yaml)
-o, --output (= "")
    specify an output file
%s`[1:]

	for relid, t := range map[int]struct {
		usage, doc string
	}{
		-1: {"<id>", ""},
		0:  {"[<id>]", "\nCurrent default relation id is \"peer0:0\".\n"},
	} {
		c.Logf("test relid %d", relid)
		hctx := s.GetHookContext(c, relid, "")
		com, err := jujuc.NewCommand(hctx, "relation-list")
		c.Assert(err, IsNil)
		ctx := dummyContext(c)
		code := cmd.Main(com, ctx, []string{"--help"})
		c.Assert(code, Equals, 0)
		c.Assert(bufferString(ctx.Stdout), Equals, "")
		expect := fmt.Sprintf(template, t.usage, t.doc)
		c.Assert(bufferString(ctx.Stderr), Equals, expect)

	}
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:31,代码来源:relation-list_test.go

示例3: TestSSHCommand

func (s *SSHSuite) TestSSHCommand(c *C) {
	m := s.makeMachines(3, c)
	ch := coretesting.Charms.Dir("series", "dummy")
	curl := charm.MustParseURL(
		fmt.Sprintf("local:series/%s-%d", ch.Meta().Name, ch.Revision()),
	)
	bundleURL, err := url.Parse("http://bundles.example.com/dummy-1")
	c.Assert(err, IsNil)
	dummy, err := s.State.AddCharm(ch, curl, bundleURL, "dummy-1-sha256")
	c.Assert(err, IsNil)
	srv, err := s.State.AddService("mysql", dummy)
	c.Assert(err, IsNil)
	s.addUnit(srv, m[0], c)

	srv, err = s.State.AddService("mongodb", dummy)
	c.Assert(err, IsNil)
	s.addUnit(srv, m[1], c)
	s.addUnit(srv, m[2], c)

	for _, t := range sshTests {
		c.Logf("testing juju ssh %s", t.args)
		ctx := &cmd.Context{c.MkDir(), &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}}
		code := cmd.Main(&SSHCommand{}, ctx, t.args)
		c.Check(code, Equals, 0)
		c.Check(ctx.Stderr.(*bytes.Buffer).String(), Equals, "")
		c.Check(ctx.Stdout.(*bytes.Buffer).String(), Equals, t.result)
	}
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:28,代码来源:ssh_test.go

示例4: TestAutoSync

func (s *BootstrapSuite) TestAutoSync(c *gc.C) {
	// Prepare a mock storage for testing and store the
	// dummy tools in there.
	restore := createToolsStore(c)
	defer restore()

	// Change the tools location to be the test location and also
	// the version and ensure their later restoring.
	origVersion := version.Current
	version.Current.Number = version.MustParse("1.2.3")
	defer func() { version.Current = origVersion }()

	// Create home with dummy provider and remove all
	// of its tools.
	env, fake := makeEmptyFakeHome(c)
	defer fake.Restore()

	// Bootstrap the environment now detects the missing
	// tools and automatically synchronizes them from the
	// storage above.
	ctx := coretesting.Context(c)
	code := cmd.Main(&BootstrapCommand{}, ctx, nil)
	c.Check(code, gc.Equals, 0)

	// Now check the available tools which are the 1.0.0 tools.
	checkTools(c, env, v100All)
}
开发者ID:hivetech,项目名称:judo.legacy,代码行数:27,代码来源:bootstrap_test.go

示例5: TestHelp

func (s *RelationIdsSuite) TestHelp(c *C) {
	template := `
usage: %s
purpose: list all relation ids with the given relation name

options:
--format  (= smart)
    specify output format (json|smart|yaml)
-o, --output (= "")
    specify an output file
%s`[1:]

	for relid, t := range map[int]struct {
		usage, doc string
	}{
		-1: {"relation-ids [options] <name>", ""},
		0:  {"relation-ids [options] [<name>]", "\nCurrent default relation name is \"x\".\n"},
		3:  {"relation-ids [options] [<name>]", "\nCurrent default relation name is \"y\".\n"},
	} {
		c.Logf("relid %d", relid)
		hctx := s.GetHookContext(c, relid, "")
		com, err := jujuc.NewCommand(hctx, "relation-ids")
		c.Assert(err, IsNil)
		ctx := testing.Context(c)
		code := cmd.Main(com, ctx, []string{"--help"})
		c.Assert(code, Equals, 0)
		expect := fmt.Sprintf(template, t.usage, t.doc)
		c.Assert(bufferString(ctx.Stdout), Equals, expect)
		c.Assert(bufferString(ctx.Stderr), Equals, "")
	}
}
开发者ID:rif,项目名称:golang-stuff,代码行数:31,代码来源:relation-ids_test.go

示例6: assertLogs

func assertLogs(c *C, ctx jujuc.Context, badge string) {
	loggo.ConfigureLoggers("juju=DEBUG")
	writer := &loggo.TestWriter{}
	old_writer, err := loggo.ReplaceDefaultWriter(writer)
	c.Assert(err, IsNil)
	defer loggo.ReplaceDefaultWriter(old_writer)
	msg1 := "the chickens"
	msg2 := "are 110% AWESOME"
	com, err := jujuc.NewCommand(ctx, "juju-log")
	c.Assert(err, IsNil)
	for _, t := range commonLogTests {
		writer.Clear()
		c.Assert(err, IsNil)

		var args []string
		if t.debugFlag {
			args = []string{"--debug", msg1, msg2}
		} else {
			args = []string{msg1, msg2}
		}
		code := cmd.Main(com, &cmd.Context{}, args)
		c.Assert(code, Equals, 0)
		c.Assert(writer.Log, HasLen, 1)
		c.Assert(writer.Log[0].Level, Equals, t.level)
		c.Assert(writer.Log[0].Message, Equals, fmt.Sprintf("%s: %s %s", badge, msg1, msg2))
	}
}
开发者ID:rif,项目名称:golang-stuff,代码行数:27,代码来源:juju-log_test.go

示例7: TestGetConfig

func (s *ConfigSuite) TestGetConfig(c *C) {
	sch := s.AddTestingCharm(c, "dummy")
	svc, err := s.State.AddService("dummy-service", sch)
	c.Assert(err, IsNil)
	err = svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"})
	c.Assert(err, IsNil)
	for _, t := range getTests {
		ctx := coretesting.Context(c)
		code := cmd.Main(&GetCommand{}, ctx, []string{t.service})
		c.Check(code, Equals, 0)
		c.Assert(ctx.Stderr.(*bytes.Buffer).String(), Equals, "")
		// round trip via goyaml to avoid being sucked into a quagmire of
		// map[interface{}]interface{} vs map[string]interface{}. This is
		// also required if we add json support to this command.
		buf, err := goyaml.Marshal(t.expected)
		c.Assert(err, IsNil)
		expected := make(map[string]interface{})
		err = goyaml.Unmarshal(buf, &expected)
		c.Assert(err, IsNil)

		actual := make(map[string]interface{})
		err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
		c.Assert(err, IsNil)
		c.Assert(actual, DeepEquals, expected)
	}
}
开发者ID:sankark,项目名称:golang-stuff,代码行数:26,代码来源:config_test.go

示例8: assertLogs

func assertLogs(c *C, ctx jujuc.Context, badge string) {
	msg1 := "the chickens"
	msg2 := "are 110% AWESOME"
	com, err := jujuc.NewCommand(ctx, "juju-log")
	c.Assert(err, IsNil)
	for _, t := range commonLogTests {
		buf, pop := pushLog(t.debugEnabled)
		defer pop()

		var args []string
		if t.debugFlag {
			args = []string{"--debug", msg1, msg2}
		} else {
			args = []string{msg1, msg2}
		}
		code := cmd.Main(com, &cmd.Context{}, args)
		c.Assert(code, Equals, 0)

		if t.target == "" {
			c.Assert(buf.String(), Equals, "")
		} else {
			expect := fmt.Sprintf("%s %s: %s %s\n", t.target, badge, msg1, msg2)
			c.Assert(buf.String(), Equals, expect)
		}
	}
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:26,代码来源:juju-log_test.go

示例9: TestMainRunSilentError

func (s *CmdSuite) TestMainRunSilentError(c *C) {
	ctx := dummyContext(c)
	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "silent-error"})
	c.Assert(result, Equals, 1)
	c.Assert(bufferString(ctx.Stdout), Equals, "")
	c.Assert(bufferString(ctx.Stderr), Equals, "")
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:7,代码来源:cmd_test.go

示例10: TestMainSuccess

func (s *CmdSuite) TestMainSuccess(c *C) {
	ctx := dummyContext(c)
	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "success!"})
	c.Assert(result, Equals, 0)
	c.Assert(bufferString(ctx.Stdout), Equals, "success!\n")
	c.Assert(bufferString(ctx.Stderr), Equals, "")
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:7,代码来源:cmd_test.go

示例11: TestMainRunError

func (s *CmdSuite) TestMainRunError(c *C) {
	ctx := testing.Context(c)
	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "error"})
	c.Assert(result, Equals, 1)
	c.Assert(bufferString(ctx.Stdout), Equals, "")
	c.Assert(bufferString(ctx.Stderr), Equals, "error: BAM!\n")
}
开发者ID:rif,项目名称:golang-stuff,代码行数:7,代码来源:cmd_test.go

示例12: TestRelationList

func (s *RelationListSuite) TestRelationList(c *C) {
	for i, t := range relationListTests {
		c.Logf("test %d: %s", i, t.summary)
		hctx := s.GetHookContext(c, t.relid, "")
		setMembers(hctx.rels[0], t.members0)
		setMembers(hctx.rels[1], t.members1)
		com, err := jujuc.NewCommand(hctx, "relation-list")
		c.Assert(err, IsNil)
		ctx := dummyContext(c)
		code := cmd.Main(com, ctx, t.args)
		c.Logf(bufferString(ctx.Stderr))
		c.Assert(code, Equals, t.code)
		if code == 0 {
			c.Assert(bufferString(ctx.Stderr), Equals, "")
			expect := t.out
			if expect != "" {
				expect = expect + "\n"
			}
			c.Assert(bufferString(ctx.Stdout), Equals, expect)
		} else {
			c.Assert(bufferString(ctx.Stdout), Equals, "")
			expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out)
			c.Assert(bufferString(ctx.Stderr), Matches, expect)
		}
	}
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:26,代码来源:relation-list_test.go

示例13: runStatus

func runStatus(c *C, args ...string) (code int, stdout, stderr []byte) {
	ctx := coretesting.Context(c)
	code = cmd.Main(&StatusCommand{}, ctx, args)
	stdout = ctx.Stdout.(*bytes.Buffer).Bytes()
	stderr = ctx.Stderr.(*bytes.Buffer).Bytes()
	return
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:7,代码来源:status_test.go

示例14: TestRelationListHelp

func (s *RelationListSuite) TestRelationListHelp(c *C) {
	template := `
usage: relation-list [options]
purpose: list relation units

options:
--format  (= smart)
    specify output format (json|smart|yaml)
-o, --output (= "")
    specify an output file
-r  (= %s)
    specify a relation by id
%s`[1:]

	for relid, t := range map[int]struct {
		usage, doc string
	}{
		-1: {"", "\n-r must be specified when not in a relation hook\n"},
		0:  {"peer0:0", ""},
	} {
		c.Logf("test relid %d", relid)
		hctx := s.GetHookContext(c, relid, "")
		com, err := jujuc.NewCommand(hctx, "relation-list")
		c.Assert(err, IsNil)
		ctx := testing.Context(c)
		code := cmd.Main(com, ctx, []string{"--help"})
		c.Assert(code, Equals, 0)
		expect := fmt.Sprintf(template, t.usage, t.doc)
		c.Assert(bufferString(ctx.Stdout), Equals, expect)
		c.Assert(bufferString(ctx.Stderr), Equals, "")
	}
}
开发者ID:rif,项目名称:golang-stuff,代码行数:32,代码来源:relation-list_test.go

示例15: TestOpenstackLocalMetadataNoMatch

func (s *ValidateMetadataSuite) TestOpenstackLocalMetadataNoMatch(c *gc.C) {
	s.makeLocalMetadata(c, "1234", "region-2", "raring", "some-auth-url")
	ctx := coretesting.Context(c)
	metadataDir := config.JujuHomePath("")
	code := cmd.Main(
		&ValidateImageMetadataCommand{}, ctx, []string{
			"-p", "openstack", "-s", "precise", "-r", "region-2",
			"-u", "some-auth-url", "-d", metadataDir},
	)
	c.Assert(code, gc.Equals, 1)
	code = cmd.Main(
		&ValidateImageMetadataCommand{}, ctx, []string{
			"-p", "openstack", "-s", "raring", "-r", "region-3",
			"-u", "some-auth-url", "-d", metadataDir},
	)
	c.Assert(code, gc.Equals, 1)
}
开发者ID:hivetech,项目名称:judo.legacy,代码行数:17,代码来源:validatemetadata_test.go


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