本文整理汇总了Golang中github.com/juju/core/cmd.Main函数的典型用法代码示例。如果您正苦于以下问题:Golang Main函数的具体用法?Golang Main怎么用?Golang Main使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Main函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testSSHCommandHostAddressRetry
func (s *SSHSuite) testSSHCommandHostAddressRetry(c *gc.C, proxy bool) {
m := s.makeMachines(1, c, false)
ctx := coretesting.Context(c)
var called int
next := func() bool {
called++
return called < 2
}
attemptStarter := &callbackAttemptStarter{next: next}
s.PatchValue(&sshHostFromTargetAttemptStrategy, attemptStarter)
// Ensure that the ssh command waits for a public address, or the attempt
// strategy's Done method returns false.
args := []string{"--proxy=" + fmt.Sprint(proxy), "0"}
code := cmd.Main(&SSHCommand{}, ctx, args)
c.Check(code, gc.Equals, 1)
c.Assert(called, gc.Equals, 2)
called = 0
attemptStarter.next = func() bool {
called++
if called > 1 {
s.setAddresses(m[0], c)
}
return true
}
code = cmd.Main(&SSHCommand{}, ctx, args)
c.Check(code, gc.Equals, 0)
c.Assert(called, gc.Equals, 2)
}
示例2: TestRelationListHelp
func (s *RelationListSuite) TestRelationListHelp(c *gc.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, gc.IsNil)
ctx := testing.Context(c)
code := cmd.Main(com, ctx, []string{"--help"})
c.Assert(code, gc.Equals, 0)
expect := fmt.Sprintf(template, t.usage, t.doc)
c.Assert(bufferString(ctx.Stdout), gc.Equals, expect)
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
}
}
示例3: Main
// Main registers subcommands for the juju-metadata executable, and hands over control
// to the cmd package. This function is not redundant with main, because it
// provides an entry point for testing with arbitrary command line arguments.
func Main(args []string) {
ctx, err := cmd.DefaultContext()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}
if err := juju.InitJujuHome(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(2)
}
metadatacmd := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "metadata",
UsagePrefix: "juju",
Doc: metadataDoc,
Purpose: "tools for generating and validating image and tools metadata",
Log: &cmd.Log{}})
metadatacmd.Register(envcmd.Wrap(&ValidateImageMetadataCommand{}))
metadatacmd.Register(envcmd.Wrap(&ImageMetadataCommand{}))
metadatacmd.Register(envcmd.Wrap(&ToolsMetadataCommand{}))
metadatacmd.Register(envcmd.Wrap(&ValidateToolsMetadataCommand{}))
metadatacmd.Register(&SignMetadataCommand{})
os.Exit(cmd.Main(metadatacmd, ctx, args[1:]))
}
示例4: TestSSHCommand
func (s *SSHSuite) TestSSHCommand(c *gc.C) {
m := s.makeMachines(3, c, true)
ch := coretesting.Charms.Dir("dummy")
curl := charm.MustParseURL(
fmt.Sprintf("local:quantal/%s-%d", ch.Meta().Name, ch.Revision()),
)
bundleURL, err := url.Parse("http://bundles.testing.invalid/dummy-1")
c.Assert(err, gc.IsNil)
dummy, err := s.State.AddCharm(ch, curl, bundleURL, "dummy-1-sha256")
c.Assert(err, gc.IsNil)
srv := s.AddTestingService(c, "mysql", dummy)
s.addUnit(srv, m[0], c)
srv = s.AddTestingService(c, "mongodb", dummy)
s.addUnit(srv, m[1], c)
s.addUnit(srv, m[2], c)
for i, t := range sshTests {
c.Logf("test %d: %s -> %s\n", i, t.about, t.args)
ctx := coretesting.Context(c)
jujucmd := cmd.NewSuperCommand(cmd.SuperCommandParams{})
jujucmd.Register(envcmd.Wrap(&SSHCommand{}))
code := cmd.Main(jujucmd, ctx, t.args)
c.Check(code, gc.Equals, 0)
c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, t.result)
}
}
示例5: TestRelationList
func (s *RelationListSuite) TestRelationList(c *gc.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, gc.IsNil)
ctx := testing.Context(c)
code := cmd.Main(com, ctx, t.args)
c.Logf(bufferString(ctx.Stderr))
c.Assert(code, gc.Equals, t.code)
if code == 0 {
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
expect := t.out
if expect != "" {
expect = expect + "\n"
}
c.Assert(bufferString(ctx.Stdout), gc.Equals, expect)
} else {
c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out)
c.Assert(bufferString(ctx.Stderr), gc.Matches, expect)
}
}
}
示例6: TestDescription
func (s *SuperCommandSuite) TestDescription(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpose: "blow up the death star"})
jc.Register(&TestCommand{Name: "blah"})
ctx := testing.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--description"})
c.Assert(code, gc.Equals, 0)
c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n")
}
示例7: assertSetSuccess
// assertSetSuccess sets configuration options and checks the expected settings.
func assertSetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) {
ctx := coretesting.ContextForDir(c, dir)
code := cmd.Main(envcmd.Wrap(&SetCommand{}), ctx, append([]string{"dummy-service"}, args...))
c.Check(code, gc.Equals, 0)
settings, err := svc.ConfigSettings()
c.Assert(err, gc.IsNil)
c.Assert(settings, gc.DeepEquals, expect)
}
示例8: TestHelpWithPrefix
func (s *SuperCommandSuite) TestHelpWithPrefix(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", UsagePrefix: "juju"})
jc.Register(&TestCommand{Name: "blah"})
ctx := testing.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--help"})
c.Assert(code, gc.Equals, 0)
stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
c.Assert(stripped, gc.Matches, ".*usage: juju jujutest blah.*blah-doc.*")
}
示例9: TestAllPlusKey
func (s *ConfigGetSuite) TestAllPlusKey(c *gc.C) {
hctx := s.GetHookContext(c, -1, "")
com, err := jujuc.NewCommand(hctx, "config-get")
c.Assert(err, gc.IsNil)
ctx := testing.Context(c)
code := cmd.Main(com, ctx, []string{"--all", "--format", "json", "monsters"})
c.Assert(code, gc.Equals, 2)
c.Assert(bufferString(ctx.Stderr), gc.Equals, "error: cannot use argument --all together with key \"monsters\"\n")
}
示例10: TestOutputFormat
func (s *UnitGetSuite) TestOutputFormat(c *gc.C) {
for _, t := range unitGetTests {
com := s.createCommand(c)
ctx := testing.Context(c)
code := cmd.Main(com, ctx, t.args)
c.Assert(code, gc.Equals, 0)
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
c.Assert(bufferString(ctx.Stdout), gc.Matches, t.out)
}
}
示例11: TestEc2LocalMetadataNoMatch
func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataNoMatch(c *gc.C) {
s.setupEc2LocalMetadata(c, "us-east-1", "")
ctx := coretesting.Context(c)
code := cmd.Main(
envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{
"-p", "ec2", "-s", "raring", "-r", "us-west-1",
"-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir},
)
c.Assert(code, gc.Equals, 1)
code = cmd.Main(
envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{
"-p", "ec2", "-s", "precise", "-r", "region",
"-u", "https://ec2.region.amazonaws.com", "-d", s.metadataDir},
)
c.Assert(code, gc.Equals, 1)
errOut := ctx.Stderr.(*bytes.Buffer).String()
strippedOut := strings.Replace(errOut, "\n", "", -1)
c.Check(strippedOut, gc.Matches, `.*Resolve Metadata:.*`)
}
示例12: TestMissingCallbackErrors
func (s *SuperCommandSuite) TestMissingCallbackErrors(c *gc.C) {
callback := func(ctx *cmd.Context, subcommand string, args []string) error {
return fmt.Errorf("command not found %q", subcommand)
}
ctx := testing.Context(c)
code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo"})
c.Assert(code, gc.Equals, 1)
c.Assert(testing.Stdout(ctx), gc.Equals, "")
c.Assert(testing.Stderr(ctx), gc.Equals, "ERROR command not found \"foo\"\n")
}
示例13: TestOutputPath
func (s *UnitGetSuite) TestOutputPath(c *gc.C) {
com := s.createCommand(c)
ctx := testing.Context(c)
code := cmd.Main(com, ctx, []string{"--output", "some-file", "private-address"})
c.Assert(code, gc.Equals, 0)
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
c.Assert(err, gc.IsNil)
c.Assert(string(content), gc.Equals, "192.168.0.99\n")
}
示例14: TestVersionFlag
func (s *SuperCommandSuite) TestVersionFlag(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
Purpose: "to be purposeful",
Doc: "doc\nblah\ndoc",
})
testVersionFlagCommand := &testVersionFlagCommand{}
jc.Register(&cmd.VersionCommand{})
jc.Register(testVersionFlagCommand)
var stdout, stderr bytes.Buffer
ctx := &cmd.Context{
Stdout: &stdout,
Stderr: &stderr,
}
// baseline: juju version
code := cmd.Main(jc, ctx, []string{"version"})
c.Check(code, gc.Equals, 0)
baselineStderr := stderr.String()
baselineStdout := stdout.String()
stderr.Reset()
stdout.Reset()
// juju --version output should match that of juju version.
code = cmd.Main(jc, ctx, []string{"--version"})
c.Check(code, gc.Equals, 0)
c.Assert(stderr.String(), gc.Equals, baselineStderr)
c.Assert(stdout.String(), gc.Equals, baselineStdout)
stderr.Reset()
stdout.Reset()
// juju test --version should update testVersionFlagCommand.version,
// and there should be no output. The --version flag on the 'test'
// subcommand has a different type to the "juju --version" flag.
code = cmd.Main(jc, ctx, []string{"test", "--version=abc.123"})
c.Check(code, gc.Equals, 0)
c.Assert(stderr.String(), gc.Equals, "")
c.Assert(stdout.String(), gc.Equals, "")
c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123")
}
示例15: TestOutputPath
func (s *RelationGetSuite) TestOutputPath(c *gc.C) {
hctx := s.GetHookContext(c, 1, "m/0")
com, err := jujuc.NewCommand(hctx, "relation-get")
c.Assert(err, gc.IsNil)
ctx := testing.Context(c)
code := cmd.Main(com, ctx, []string{"--output", "some-file", "pew"})
c.Assert(code, gc.Equals, 0)
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
c.Assert(err, gc.IsNil)
c.Assert(string(content), gc.Equals, "pew\npew\n\n")
}