func (s *SuperCommandSuite) TestDispatch(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) info := jc.Info() c.Assert(info.Name, gc.Equals, "jujutest") c.Assert(info.Args, gc.Equals, "<command> ...") c.Assert(info.Doc, gc.Matches, helpCommandsText) jc, _, err := initDefenestrate([]string{"discombobulate"}) c.Assert(err, gc.ErrorMatches, "unrecognized command: jujutest discombobulate") info = jc.Info() c.Assert(info.Name, gc.Equals, "jujutest") c.Assert(info.Args, gc.Equals, "<command> ...") c.Assert(info.Doc, gc.Matches, "commands:\n defenestrate - defenestrate the juju"+helpText) jc, tc, err := initDefenestrate([]string{"defenestrate"}) c.Assert(err, gc.IsNil) c.Assert(tc.Option, gc.Equals, "") info = jc.Info() c.Assert(info.Name, gc.Equals, "jujutest defenestrate") c.Assert(info.Args, gc.Equals, "<something>") c.Assert(info.Doc, gc.Equals, "defenestrate-doc") _, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"}) c.Assert(err, gc.IsNil) c.Assert(tc.Option, gc.Equals, "firmly") _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"}) c.Assert(err, gc.ErrorMatches, `unrecognized args: \["gibberish"\]`) // --description must be used on it's own. _, _, err = initDefenestrate([]string{"--description", "defenestrate"}) c.Assert(err, gc.ErrorMatches, `unrecognized args: \["defenestrate"\]`) }
func (s *SuperCommandSuite) TestRegister(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) jc.Register(&TestCommand{Name: "flip"}) jc.Register(&TestCommand{Name: "flap"}) badCall := func() { jc.Register(&TestCommand{Name: "flap"}) } c.Assert(badCall, gc.PanicMatches, "command already registered: flap") }
func NewSuperWithCallback(callback func(*cmd.Context, string, []string) error) cmd.Command { return cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", Log: &cmd.Log{}, MissingCallback: callback, }) }
func NewJujuCommand(ctx *cmd.Context) cmd.Command { jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, UserAliasesFilename: osenv.JujuXDGDataHomePath("aliases"), }) jcmd.AddHelpTopic("basics", "Basic commands", helptopics.Basics) jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helptopics.OpenstackProvider, "openstack") jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helptopics.EC2Provider, "ec2", "aws", "amazon") jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helptopics.HPCloud, "hpcloud", "hp-cloud") jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helptopics.AzureProvider, "azure") jcmd.AddHelpTopic("maas-provider", "How to configure a MAAS provider", helptopics.MAASProvider, "maas") jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helptopics.Constraints) jcmd.AddHelpTopic("placement", "How to use placement directives", helptopics.Placement) jcmd.AddHelpTopic("spaces", "How to configure more complex networks using spaces", helptopics.Spaces, "networking") jcmd.AddHelpTopic("glossary", "Glossary of terms", helptopics.Glossary) jcmd.AddHelpTopic("logging", "How Juju handles logging", helptopics.Logging) jcmd.AddHelpTopic("juju", "What is Juju?", helptopics.Juju) jcmd.AddHelpTopic("controllers", "About Juju Controllers", helptopics.JujuControllers) jcmd.AddHelpTopic("users", "About users in Juju", helptopics.Users) jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jcmd, ctx) return jcmd }
// Main registers subcommands for the jujud executable, and hands over control // to the cmd package. func jujuDMain(args []string, ctx *cmd.Context) (code int, err error) { // Assuming an average of 200 bytes per log message, use up to // 200MB for the log buffer. defer logger.Debugf("jujud complete, code %d, err %v", code, err) logCh, err := logsender.InstallBufferedLogWriter(1048576) if err != nil { return 1, errors.Trace(err) } jujud := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujud", Doc: jujudDoc, }) jujud.Log.NewWriter = func(target io.Writer) loggo.Writer { return &jujudWriter{target: target} } jujud.Register(NewBootstrapCommand()) // TODO(katco-): AgentConf type is doing too much. The // MachineAgent type has called out the separate concerns; the // AgentConf should be split up to follow suit. agentConf := agentcmd.NewAgentConf("") machineAgentFactory := agentcmd.MachineAgentFactoryFn(agentConf, logCh, "") jujud.Register(agentcmd.NewMachineAgentCmd(ctx, machineAgentFactory, agentConf, agentConf)) jujud.Register(agentcmd.NewUnitAgent(ctx, logCh)) jujud.Register(NewUpgradeMongoCommand()) code = cmd.Main(jujud, ctx, args[1:]) return code, nil }
func NewJujuCommand(ctx *cmd.Context) cmd.Command { jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, }) jcmd.AddHelpTopic("basics", "Basic commands", helpBasics) jcmd.AddHelpTopic("local-provider", "How to configure a local (LXC) provider", helpProviderStart+helpLocalProvider+helpProviderEnd) jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helpProviderStart+helpOpenstackProvider+helpProviderEnd, "openstack") jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helpProviderStart+helpEC2Provider+helpProviderEnd, "ec2", "aws", "amazon") jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helpProviderStart+helpHPCloud+helpProviderEnd, "hpcloud", "hp-cloud") jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helpProviderStart+helpAzureProvider+helpProviderEnd, "azure") jcmd.AddHelpTopic("maas-provider", "How to configure a MAAS provider", helpProviderStart+helpMAASProvider+helpProviderEnd, "maas") jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helpConstraints) jcmd.AddHelpTopic("placement", "How to use placement directives", helpPlacement) jcmd.AddHelpTopic("glossary", "Glossary of terms", helpGlossary) jcmd.AddHelpTopic("logging", "How Juju handles logging", helpLogging) jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jcmd, ctx) return jcmd }
// Main registers subcommands for the jujud executable, and hands over control // to the cmd package. func jujuDMain(args []string, ctx *cmd.Context) (code int, err error) { // Assuming an average of 200 bytes per log message, use up to // 200MB for the log buffer. logCh, err := logsender.InstallBufferedLogWriter(1048576) if err != nil { return 1, errors.Trace(err) } jujud := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujud", Doc: jujudDoc, }) jujud.Log.Factory = &writerFactory{} jujud.Register(NewBootstrapCommand()) // TODO(katco-): AgentConf type is doing too much. The // MachineAgent type has called out the separate concerns; the // AgentConf should be split up to follow suit. agentConf := agentcmd.NewAgentConf("") machineAgentFactory := agentcmd.MachineAgentFactoryFn( agentConf, logCh, looputil.NewLoopDeviceManager(), ) jujud.Register(agentcmd.NewMachineAgentCmd(ctx, machineAgentFactory, agentConf, agentConf)) jujud.Register(agentcmd.NewUnitAgent(ctx, logCh)) code = cmd.Main(jujud, ctx, args[1:]) return code, nil }
func (s *SuperCommandSuite) TestInfo(c *gc.C) { commandsDoc := `commands: flapbabble - flapbabble the juju flip - flip the juju` jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", Purpose: "to be purposeful", Doc: "doc\nblah\ndoc", }) info := jc.Info() c.Assert(info.Name, gc.Equals, "jujutest") c.Assert(info.Purpose, gc.Equals, "to be purposeful") // info doc starts with the jc.Doc and ends with the help command c.Assert(info.Doc, gc.Matches, jc.Doc+"(.|\n)*") c.Assert(info.Doc, gc.Matches, "(.|\n)*"+helpCommandsText) jc.Register(&TestCommand{Name: "flip"}) jc.Register(&TestCommand{Name: "flapbabble"}) info = jc.Info() c.Assert(info.Doc, gc.Matches, jc.Doc+"\n\n"+commandsDoc+helpText) jc.Doc = "" info = jc.Info() c.Assert(info.Doc, gc.Matches, commandsDoc+helpText) }
func NewJujuCommand(ctx *cmd.Context) cmd.Command { jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, }) jcmd.AddHelpTopic("basics", "Basic commands", helptopics.Basics) jcmd.AddHelpTopic("local-provider", "How to configure a local (LXC) provider", helptopics.LocalProvider) jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helptopics.OpenstackProvider, "openstack") jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helptopics.EC2Provider, "ec2", "aws", "amazon") jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helptopics.HPCloud, "hpcloud", "hp-cloud") jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helptopics.AzureProvider, "azure") jcmd.AddHelpTopic("maas-provider", "How to configure a MAAS provider", helptopics.MAASProvider, "maas") jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helptopics.Constraints) jcmd.AddHelpTopic("placement", "How to use placement directives", helptopics.Placement) jcmd.AddHelpTopic("glossary", "Glossary of terms", helptopics.Glossary) jcmd.AddHelpTopic("logging", "How Juju handles logging", helptopics.Logging) jcmd.AddHelpTopic("juju", "What is Juju?", helptopics.Juju) jcmd.AddHelpTopic("juju-systems", "About Juju Environment Systems (JES)", helptopics.JujuSystems) jcmd.AddHelpTopic("users", "About users in Juju", helptopics.Users) jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jcmd, ctx) return jcmd }
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") }
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.*") }
func jujuLocalPlugin() cmd.Command { plugin := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju local", UsagePrefix: "juju", Doc: localDoc, Purpose: "local provider specific commands", }) return plugin }
func (s *SuperCommandSuite) TestRegisterAlias(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"}}) info := jc.Info() c.Assert(info.Doc, gc.Equals, `commands: flap - alias for flip flip - flip the juju flop - alias for flip help - show help on a command or other topic`) }
// NewJujuCommand ... func NewJujuCommand(ctx *cmd.Context) cmd.Command { jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, UserAliasesFilename: osenv.JujuXDGDataHomePath("aliases"), }) jcmd.AddHelpTopic("basics", "Basic Help Summary", usageHelp) registerCommands(jcmd, ctx) return jcmd }
// Main registers subcommands for the jujud executable, and hands over control // to the cmd package. func jujuDMain(args []string, ctx *cmd.Context) (code int, err error) { jujud := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujud", Doc: jujudDoc, }) jujud.Log.Factory = &writerFactory{} jujud.Register(&BootstrapCommand{}) jujud.Register(&MachineAgent{}) jujud.Register(&UnitAgent{}) code = cmd.Main(jujud, ctx, args[1:]) return code, nil }
func (s *SuperCommandSuite) TestSupercommandAliases(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", UsagePrefix: "juju", }) sub := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jubar", UsagePrefix: "juju jujutest", Aliases: []string{"jubaz", "jubing"}, }) info := sub.Info() c.Check(info.Aliases, gc.DeepEquals, []string{"jubaz", "jubing"}) jc.Register(sub) for _, name := range []string{"jubar", "jubaz", "jubing"} { c.Logf("testing command name %q", name) ctx := testing.Context(c) code := cmd.Main(jc, ctx, []string{name, "--help"}) c.Assert(code, gc.Equals, 0) stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) c.Assert(stripped, gc.Matches, ".*usage: juju jujutest jubar.*aliases: jubaz, jubing") } }
func main() { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } admcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "charm-admin", }) admcmd.Register(&DeleteCharmCommand{}) os.Exit(cmd.Main(admcmd, ctx, os.Args[1:])) }
// Main registers subcommands for the juju 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) } for i := range x { x[i] ^= 255 } if len(args) == 2 && args[1] == string(x[0:2]) { os.Stdout.Write(x[2:]) os.Exit(0) } jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, }) jcmd.AddHelpTopic("basics", "Basic commands", helpBasics) jcmd.AddHelpTopic("local-provider", "How to configure a local (LXC) provider", helpProviderStart+helpLocalProvider+helpProviderEnd) jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helpProviderStart+helpOpenstackProvider+helpProviderEnd, "openstack") jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helpProviderStart+helpEC2Provider+helpProviderEnd, "ec2", "aws", "amazon") jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helpProviderStart+helpHPCloud+helpProviderEnd, "hpcloud", "hp-cloud") jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helpProviderStart+helpAzureProvider+helpProviderEnd, "azure") jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helpConstraints) jcmd.AddHelpTopic("glossary", "Glossary of terms", helpGlossary) jcmd.AddHelpTopic("logging", "How Juju handles logging", helpLogging) jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jcmd, ctx) os.Exit(cmd.Main(jcmd, ctx, args[1:])) }
// Main registers subcommands for the jujud executable, and hands over control // to the cmd package. func jujuDMain(args []string, ctx *cmd.Context) (code int, err error) { jujud := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujud", Doc: jujudDoc, }) jujud.Log.Factory = &writerFactory{} jujud.Register(NewBootstrapCommand()) // TODO(katco-): AgentConf type is doing too much. The // MachineAgent type has called out the seperate concerns; the // AgentConf should be split up to follow suite. agentConf := agentcmd.NewAgentConf("") machineAgentFactory := agentcmd.MachineAgentFactoryFn(agentConf, agentConf) jujud.Register(agentcmd.NewMachineAgentCmd(ctx, machineAgentFactory, agentConf, agentConf)) jujud.Register(agentcmd.NewUnitAgent(ctx)) code = cmd.Main(jujud, ctx, args[1:]) return code, nil }
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") }
// Main registers subcommands for the juju-local executable. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { logger.Debugf("error: %v\n", err) os.Exit(2) } if err := juju.InitJujuXDGDataHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } plugin := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "tools", UsagePrefix: "juju", Doc: doc, Purpose: "manage tools in the controller", Log: &cmd.Log{}, }) plugin.Register(modelcmd.Wrap(&buildToolsCommand{})) plugin.Register(modelcmd.Wrap(&uploadToolsCommand{})) plugin.Register(modelcmd.Wrap(&listToolsCommand{})) os.Exit(cmd.Main(plugin, ctx, args[1:])) }
func (s *SuperCommandSuite) TestLogging(c *gc.C) { s.PatchValue(&version.Current, version.MustParseBinary("1.2.3.4-plan9-mips")) s.PatchValue(&version.Compiler, "llgo") loggingTests := []struct { usagePrefix, name string pattern string }{ {"juju", "juju", `^.* running juju \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"something", "else", `^.* running something else \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"", "juju", `^.* running juju \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"", "myapp", `^.* running myapp \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"same", "same", `^.* running same \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, } for _, test := range loggingTests { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ UsagePrefix: test.usagePrefix, Name: test.name, Log: &cmd.Log{}, }) jc.Register(&TestCommand{Name: "blah"}) ctx := testing.Context(c) code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug"}) c.Assert(code, gc.Equals, 1) c.Assert(bufferString(ctx.Stderr), gc.Matches, test.pattern) } }
func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) tc := &TestCommand{Name: "defenestrate"} jc.Register(tc) return jc, tc, testing.InitCommand(jc, args) }