From 3c62cd9fc8bdf7826c87c961db69a14658d821bf Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Tue, 9 Feb 2016 13:04:57 -0800 Subject: [PATCH] ref(cmd): break up command definitions --- cmd/helm/create.go | 12 ++++ cmd/helm/deploy.go | 39 +++++++++++++ cmd/helm/dm.go | 74 +++++++++++++++++++++++++ cmd/helm/doctor.go | 16 +++++- cmd/helm/get.go | 4 ++ cmd/helm/helm.go | 135 ++++----------------------------------------- cmd/helm/list.go | 4 ++ cmd/helm/pack.go | 13 +++++ 8 files changed, 173 insertions(+), 124 deletions(-) diff --git a/cmd/helm/create.go b/cmd/helm/create.go index 177f5e25b..3cbaa11fc 100644 --- a/cmd/helm/create.go +++ b/cmd/helm/create.go @@ -7,6 +7,18 @@ import ( "github.com/kubernetes/deployment-manager/chart" ) +func init() { + addCommands(createCmd()) +} + +func createCmd() cli.Command { + return cli.Command{ + Name: "create", + Usage: "Create a new local chart for editing.", + Action: func(c *cli.Context) { run(c, create) }, + } +} + func create(c *cli.Context) error { args := c.Args() if len(args) < 1 { diff --git a/cmd/helm/deploy.go b/cmd/helm/deploy.go index 12c5f2c97..e21bdc712 100644 --- a/cmd/helm/deploy.go +++ b/cmd/helm/deploy.go @@ -10,6 +10,45 @@ import ( "github.com/kubernetes/deployment-manager/chart" ) +func init() { + addCommands(deployCmd()) +} + +func deployCmd() cli.Command { + return cli.Command{ + Name: "deploy", + Aliases: []string{"install"}, + Usage: "Deploy a chart into the cluster.", + Action: func(c *cli.Context) { run(c, deploy) }, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "dry-run", + Usage: "Only display the underlying kubectl commands.", + }, + cli.BoolFlag{ + Name: "stdin,i", + Usage: "Read a configuration from STDIN.", + }, + cli.StringFlag{ + Name: "name", + Usage: "Name of deployment, used for deploy and update commands (defaults to template name)", + }, + // TODO: I think there is a Generic flag type that we can implement parsing with. + cli.StringFlag{ + Name: "properties,p", + Usage: "A comma-separated list of key=value pairs: 'foo=bar,foo2=baz'.", + }, + cli.StringFlag{ + // FIXME: This is not right. It's sort of a half-baked forward + // port of dm.go. + Name: "repository", + Usage: "The default repository", + Value: "kubernetes/application-dm-templates", + }, + }, + } +} + func deploy(c *cli.Context) error { args := c.Args() if len(args) < 1 { diff --git a/cmd/helm/dm.go b/cmd/helm/dm.go index b331e62c7..97592b48d 100644 --- a/cmd/helm/dm.go +++ b/cmd/helm/dm.go @@ -2,7 +2,9 @@ package main import ( "errors" + "os" + "github.com/codegangsta/cli" "github.com/deis/helm-dm/dm" "github.com/deis/helm-dm/format" "github.com/deis/helm-dm/kubectl" @@ -11,6 +13,78 @@ import ( // ErrAlreadyInstalled indicates that DM is already installed. var ErrAlreadyInstalled = errors.New("Already Installed") +func init() { + addCommands(dmCmd()) +} + +func dmCmd() cli.Command { + return cli.Command{ + Name: "dm", + Usage: "Manage DM on Kubernetes", + Subcommands: []cli.Command{ + { + Name: "install", + Usage: "Install DM on Kubernetes.", + Description: ``, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "dry-run", + Usage: "Show what would be installed, but don't install anything.", + }, + }, + Action: func(c *cli.Context) { + if err := install(c.Bool("dry-run")); err != nil { + format.Err("%s (Run 'helm doctor' for more information)", err) + os.Exit(1) + } + }, + }, + { + Name: "uninstall", + Usage: "Uninstall the DM from Kubernetes.", + Description: ``, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "dry-run", + Usage: "Show what would be installed, but don't install anything.", + }, + }, + Action: func(c *cli.Context) { + if err := uninstall(c.Bool("dry-run")); err != nil { + format.Err("%s (Run 'helm doctor' for more information)", err) + os.Exit(1) + } + }, + }, + { + Name: "status", + Usage: "Show status of DM.", + Action: func(c *cli.Context) { + format.Err("Not yet implemented") + os.Exit(1) + }, + }, + { + Name: "target", + Usage: "Displays information about cluster.", + ArgsUsage: "", + Action: func(c *cli.Context) { + if err := target(c.Bool("dry-run")); err != nil { + format.Err("%s (Is the cluster running?)", err) + os.Exit(1) + } + }, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "dry-run", + Usage: "Only display the underlying kubectl commands.", + }, + }, + }, + }, + } +} + func install(dryRun bool) error { runner := getKubectlRunner(dryRun) diff --git a/cmd/helm/doctor.go b/cmd/helm/doctor.go index 15f068a25..8568b6972 100644 --- a/cmd/helm/doctor.go +++ b/cmd/helm/doctor.go @@ -1,12 +1,26 @@ package main import ( + "github.com/codegangsta/cli" "github.com/deis/helm-dm/dm" "github.com/deis/helm-dm/format" "github.com/deis/helm-dm/kubectl" ) -func doctor() error { +func init() { + addCommands(doctorCmd()) +} + +func doctorCmd() cli.Command { + return cli.Command{ + Name: "doctor", + Usage: "Run a series of checks for necessary prerequisites.", + ArgsUsage: "", + Action: func(c *cli.Context) { run(c, doctor) }, + } +} + +func doctor(c *cli.Context) error { var runner kubectl.Runner runner = &kubectl.RealRunner{} if dm.IsInstalled(runner) { diff --git a/cmd/helm/get.go b/cmd/helm/get.go index 4fd0d5c5e..ddff95eac 100644 --- a/cmd/helm/get.go +++ b/cmd/helm/get.go @@ -7,6 +7,10 @@ import ( "github.com/deis/helm-dm/format" ) +func init() { + addCommands(getCmd()) +} + func getCmd() cli.Command { return cli.Command{ Name: "get", diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 268498ea6..55b6c0ebb 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -10,12 +10,18 @@ import ( var version = "0.0.1" +var commands []cli.Command + +func init() { + commands = cmds() +} + func main() { app := cli.NewApp() app.Name = "helm" app.Version = version app.Usage = `Deploy and manage packages.` - app.Commands = commands() + app.Commands = commands // TODO: make better app.Flags = []cli.Flag{ @@ -38,73 +44,8 @@ func main() { app.Run(os.Args) } -func commands() []cli.Command { +func cmds() []cli.Command { return []cli.Command{ - { - Name: "dm", - Usage: "Manage DM on Kubernetes", - Subcommands: []cli.Command{ - { - Name: "install", - Usage: "Install DM on Kubernetes.", - Description: ``, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Show what would be installed, but don't install anything.", - }, - }, - Action: func(c *cli.Context) { - if err := install(c.Bool("dry-run")); err != nil { - format.Err("%s (Run 'helm doctor' for more information)", err) - os.Exit(1) - } - }, - }, - { - Name: "uninstall", - Usage: "Uninstall the DM from Kubernetes.", - Description: ``, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Show what would be installed, but don't install anything.", - }, - }, - Action: func(c *cli.Context) { - if err := uninstall(c.Bool("dry-run")); err != nil { - format.Err("%s (Run 'helm doctor' for more information)", err) - os.Exit(1) - } - }, - }, - { - Name: "status", - Usage: "Show status of DM.", - Action: func(c *cli.Context) { - format.Err("Not yet implemented") - os.Exit(1) - }, - }, - { - Name: "target", - Usage: "Displays information about cluster.", - ArgsUsage: "", - Action: func(c *cli.Context) { - if err := target(c.Bool("dry-run")); err != nil { - format.Err("%s (Is the cluster running?)", err) - os.Exit(1) - } - }, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Only display the underlying kubectl commands.", - }, - }, - }, - }, - }, { Name: "init", Usage: "Initialize the client and install DM on Kubernetes.", @@ -122,68 +63,16 @@ func commands() []cli.Command { } }, }, - { - Name: "doctor", - Usage: "Run a series of checks for necessary prerequisites.", - ArgsUsage: "", - Action: func(c *cli.Context) { - if err := doctor(); err != nil { - format.Err("%s", err) - os.Exit(1) - } - }, - }, - { - Name: "create", - Usage: "Create a new local chart for editing.", - Action: func(c *cli.Context) { run(c, create) }, - }, - { - Name: "package", - Aliases: []string{"pack"}, - Usage: "Given a chart directory, package it into a release.", - Action: func(c *cli.Context) { run(c, pack) }, - }, - { - Name: "deploy", - Aliases: []string{"install"}, - Usage: "Deploy a chart into the cluster.", - Action: func(c *cli.Context) { run(c, deploy) }, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Only display the underlying kubectl commands.", - }, - cli.BoolFlag{ - Name: "stdin,i", - Usage: "Read a configuration from STDIN.", - }, - cli.StringFlag{ - Name: "name", - Usage: "Name of deployment, used for deploy and update commands (defaults to template name)", - }, - // TODO: I think there is a Generic flag type that we can implement parsing with. - cli.StringFlag{ - Name: "properties,p", - Usage: "A comma-separated list of key=value pairs: 'foo=bar,foo2=baz'.", - }, - cli.StringFlag{ - // FIXME: This is not right. It's sort of a half-baked forward - // port of dm.go. - Name: "repository", - Usage: "The default repository", - Value: "kubernetes/application-dm-templates", - }, - }, - }, { Name: "search", }, - listCmd(), - getCmd(), } } +func addCommands(cmds ...cli.Command) { + commands = append(commands, cmds...) +} + func run(c *cli.Context, f func(c *cli.Context) error) { if err := f(c); err != nil { os.Stderr.Write([]byte(err.Error())) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 70d2abb30..a3ea2f0df 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -5,6 +5,10 @@ import ( "github.com/deis/helm-dm/format" ) +func init() { + addCommands(listCmd()) +} + func listCmd() cli.Command { return cli.Command{ Name: "list", diff --git a/cmd/helm/pack.go b/cmd/helm/pack.go index 04ab391cf..dda22a291 100644 --- a/cmd/helm/pack.go +++ b/cmd/helm/pack.go @@ -10,6 +10,19 @@ import ( "github.com/kubernetes/deployment-manager/chart" ) +func init() { + addCommands(packageCmd()) +} + +func packageCmd() cli.Command { + return cli.Command{ + Name: "package", + Aliases: []string{"pack"}, + Usage: "Given a chart directory, package it into a release.", + Action: func(c *cli.Context) { run(c, pack) }, + } +} + func pack(cxt *cli.Context) error { args := cxt.Args() if len(args) < 1 {