func (s *SuperCommandSuite) TestRegister(c *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, 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 (s *SuperCommandSuite) TestDispatch(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) info := jc.Info() c.Assert(info.Name, Equals, "jujutest") c.Assert(info.Args, Equals, "<command> ...") c.Assert(info.Doc, Matches, helpCommandsText) jc, _, err := initDefenestrate([]string{"discombobulate"}) c.Assert(err, ErrorMatches, "unrecognized command: jujutest discombobulate") info = jc.Info() c.Assert(info.Name, Equals, "jujutest") c.Assert(info.Args, Equals, "<command> ...") c.Assert(info.Doc, Matches, "commands:\n defenestrate - defenestrate the juju"+helpText) jc, tc, err := initDefenestrate([]string{"defenestrate"}) c.Assert(err, IsNil) c.Assert(tc.Option, Equals, "") info = jc.Info() c.Assert(info.Name, Equals, "jujutest defenestrate") c.Assert(info.Args, Equals, "<something>") c.Assert(info.Doc, Equals, "defenestrate-doc") _, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"}) c.Assert(err, IsNil) c.Assert(tc.Option, Equals, "firmly") _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"}) c.Assert(err, ErrorMatches, `unrecognized args: \["gibberish"\]`) }
func (s *SuperCommandSuite) TestDescription(c *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, Equals, 0) c.Assert(bufferString(ctx.Stdout), Equals, "blow up the death star\n") }
func (s *SuperCommandSuite) TestHelpWithPrefix(c *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, Equals, 0) stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) c.Assert(stripped, Matches, ".*usage: juju jujutest blah.*blah-doc.*") }
func (s *SuperCommandSuite) TestLogging(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", 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, Equals, 1) c.Assert(bufferString(ctx.Stderr), Matches, `^.* ERROR .* command failed: BAM! error: BAM! `) }
func (s *SuperCommandSuite) TestRegisterAlias(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"}}) info := jc.Info() c.Assert(info.Doc, Equals, `commands: flap - alias for flip flip - flip the juju flop - alias for flip help - show help on a command or other topic`) }
// Main registers subcommands for the jujud executable, and hands over control // to the cmd package. func jujuDMain(args []string) (code int, err error) { jujud := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujud", Doc: jujudDoc, Log: &cmd.Log{}, }) jujud.Register(&BootstrapCommand{}) jujud.Register(&MachineAgent{}) jujud.Register(&UnitAgent{}) jujud.Register(&cmd.VersionCommand{}) code = cmd.Main(jujud, cmd.DefaultContext(), args[1:]) return code, nil }
// 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) { 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(&ValidateImageMetadataCommand{}) metadatacmd.Register(&ImageMetadataCommand{}) os.Exit(cmd.Main(metadatacmd, cmd.DefaultContext(), args[1:])) }
func (s *SuperCommandSuite) TestVersionFlag(c *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, 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, Equals, 0) c.Assert(stderr.String(), Equals, baselineStderr) c.Assert(stdout.String(), 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, Equals, 0) c.Assert(stderr.String(), Equals, "") c.Assert(stdout.String(), Equals, "") c.Assert(testVersionFlagCommand.version, Equals, "abc.123") }
func (s *SuperCommandSuite) TestInfo(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", Purpose: "to be purposeful", Doc: "doc\nblah\ndoc", }) info := jc.Info() c.Assert(info.Name, Equals, "jujutest") c.Assert(info.Purpose, Equals, "to be purposeful") // info doc starts with the jc.Doc and ends with the help command c.Assert(info.Doc, Matches, jc.Doc+"(.|\n)*") c.Assert(info.Doc, Matches, "(.|\n)*"+helpCommandsText) jc.Register(&TestCommand{Name: "flip"}) jc.Register(&TestCommand{Name: "flapbabble"}) info = jc.Info() c.Assert(info.Doc, Matches, jc.Doc+"\n\n"+commandsDoc+helpText) jc.Doc = "" info = jc.Info() c.Assert(info.Doc, Matches, commandsDoc+helpText) }
// 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) { 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) } jujucmd := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, Log: &cmd.Log{}, MissingCallback: RunPlugin, }) jujucmd.AddHelpTopic("basics", "Basic commands", helpBasics) jujucmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) // Creation commands. jujucmd.Register(&BootstrapCommand{}) jujucmd.Register(&AddMachineCommand{}) jujucmd.Register(&DeployCommand{}) jujucmd.Register(&AddRelationCommand{}) jujucmd.Register(&AddUnitCommand{}) // Destruction commands. jujucmd.Register(&DestroyMachineCommand{}) jujucmd.Register(&DestroyRelationCommand{}) jujucmd.Register(&DestroyServiceCommand{}) jujucmd.Register(&DestroyUnitCommand{}) jujucmd.Register(&DestroyEnvironmentCommand{}) // Reporting commands. jujucmd.Register(&StatusCommand{}) jujucmd.Register(&SwitchCommand{}) // Error resolution and debugging commands. jujucmd.Register(&SCPCommand{}) jujucmd.Register(&SSHCommand{}) jujucmd.Register(&ResolvedCommand{}) jujucmd.Register(&DebugLogCommand{sshCmd: &SSHCommand{}}) jujucmd.Register(&DebugHooksCommand{}) // Configuration commands. jujucmd.Register(&InitCommand{}) jujucmd.Register(&GetCommand{}) jujucmd.Register(&SetCommand{}) jujucmd.Register(&GetConstraintsCommand{}) jujucmd.Register(&SetConstraintsCommand{}) jujucmd.Register(&GetEnvironmentCommand{}) jujucmd.Register(&SetEnvironmentCommand{}) jujucmd.Register(&ExposeCommand{}) jujucmd.Register(&SyncToolsCommand{}) jujucmd.Register(&UnexposeCommand{}) jujucmd.Register(&UpgradeJujuCommand{}) jujucmd.Register(&UpgradeCharmCommand{}) // Charm publishing commands. jujucmd.Register(&PublishCommand{}) // Charm tool commands. jujucmd.Register(&HelpToolCommand{}) // Common commands. jujucmd.Register(&cmd.VersionCommand{}) os.Exit(cmd.Main(jujucmd, cmd.DefaultContext(), args[1:])) }
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) }