Skip to content

Commit

Permalink
fleetctl: convert cli to cobra
Browse files Browse the repository at this point in the history
Use Cobra (github.com/spf13/cobra) instead of cli
(github.com/codegangsta/cli), for better cmdline user interface.

* Create a wrapper runWrapper() to be used for cobra, to wrap around
  a normal run*() function into a prototype for cobra.Command.Run().
  It also sets a global variable cAPI for running a normal command.
* remove unnecessary code for codegangsta/cli from fleetctl.go.

Suggested-by: Jonathan Boulle <jonathanboulle@gmail.com>
Fixes: coreos#1453
Supersedes coreos#1570
  • Loading branch information
Dongsu Park committed May 20, 2016
1 parent 0779d25 commit 848d356
Show file tree
Hide file tree
Showing 18 changed files with 742 additions and 474 deletions.
24 changes: 12 additions & 12 deletions fleetctl/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ package main
import (
"fmt"

"github.com/codegangsta/cli"
"github.com/spf13/cobra"

"github.com/coreos/fleet/client"
"github.com/coreos/fleet/schema"
)

func NewCatCommand() cli.Command {
return cli.Command{
Name: "cat",
Usage: "Output the contents of a submitted unit",
ArgsUsage: "UNIT",
Description: `Outputs the unit file that is currently loaded in the cluster. Useful to verify the correct version of a unit is running.`,
Action: makeActionWrapper(runCatUnit),
}
var cmdCat = &cobra.Command{
Use: "cat UNIT",
Short: "Output the contents of a submitted unit",
Long: `Outputs the unit file that is currently loaded in the cluster. Useful to verify
the correct version of a unit is running.`,
Run: runWrapper(runCatUnit),
}

func init() {
cmdFleet.AddCommand(cmdCat)
}

func runCatUnit(c *cli.Context, cAPI client.API) (exit int) {
args := c.Args()
func runCatUnit(cCmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
stderr("One unit file must be provided")
return 1
Expand Down
31 changes: 15 additions & 16 deletions fleetctl/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ package main
import (
"time"

"github.com/codegangsta/cli"
"github.com/spf13/cobra"

"github.com/coreos/fleet/client"
)

func NewDestroyCommand() cli.Command {
return cli.Command{
Name: "destroy",
Usage: "Destroy one or more units in the cluster",
ArgsUsage: "UNIT...",
Description: `Completely remove one or more running or submitted units from the cluster.
var cmdDestroy = &cobra.Command{
Use: "destroy UNIT...",
Short: "Destroy one or more units in the cluster",
Long: `Completely remove one or more running or submitted units from the cluster.
Instructs systemd on the host machine to stop the unit, deferring to systemd
completely for any custom stop directives (i.e. ExecStop option in the unit
file).
Destroyed units are impossible to start unless re-submitted.`,
Action: makeActionWrapper(runDestroyUnits),
}
Run: runWrapper(runDestroyUnit),
}

func init() {
cmdFleet.AddCommand(cmdDestroy)
}

func runDestroyUnits(c *cli.Context, cAPI client.API) (exit int) {
args := c.Args()
func runDestroyUnit(cCmd *cobra.Command, args []string) (exit int) {
if len(args) == 0 {
stderr("No units given")
return 0
}

units, err := findUnits(args, cAPI)
units, err := findUnits(args)
if err != nil {
stderr("%v", err)
return 1
Expand All @@ -63,10 +63,10 @@ func runDestroyUnits(c *cli.Context, cAPI client.API) (exit int) {
continue
}

if c.Bool("no-block") {
attempts := c.Int("block-attempts")
if sharedFlags.NoBlock {
attempts := sharedFlags.BlockAttempts
retry := func() bool {
if c.Int("block-attempts") < 1 {
if sharedFlags.BlockAttempts < 1 {
return true
}
attempts--
Expand All @@ -93,6 +93,5 @@ func runDestroyUnits(c *cli.Context, cAPI client.API) (exit int) {

stdout("Destroyed %s", v.Name)
}

return
}
24 changes: 11 additions & 13 deletions fleetctl/fdforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,21 @@ import (
"net"
"os"

"github.com/codegangsta/cli"

"github.com/coreos/fleet/client"
"github.com/spf13/cobra"
)

func NewFDForwardCommand() cli.Command {
return cli.Command{
Name: "fd-forward",
Usage: "Proxy stdin and stdout to a unix domain socket",
ArgsUsage: "SOCKET",
Description: `fleetctl utilizes fd-forward when --tunnel is used and --endpoint is a unix socket. This command is not intended to be called by users directly.`,
Action: makeActionWrapper(runFDForward),
}
var cmdFDForward = &cobra.Command{
Use: "fd-forward SOCKET",
Short: "Proxy stdin and stdout to a unix domain socket",
Long: `fleetctl utilizes fd-forward when --tunnel is used and --endpoint is a unix socket. This command is not intended to be called by users directly.`,
Run: runWrapper(runFDForward),
}

func init() {
cmdFleet.AddCommand(cmdFDForward)
}

func runFDForward(c *cli.Context, cAPI client.API) (exit int) {
args := c.Args()
func runFDForward(cCmd *cobra.Command, args []string) (exit int) {
if len(args) != 1 {
stderr("Provide a single argument")
return 1
Expand Down
Loading

0 comments on commit 848d356

Please # to comment.