Merge pull request #2115 from dgageot/more-cleanup-for-more-tests

Add CommandLine interface for commands
This commit is contained in:
David Gageot 2015-11-05 06:20:36 +01:00
commit 29a0ef6853
19 changed files with 70 additions and 63 deletions

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/persist" "github.com/docker/machine/libmachine/persist"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
@ -16,7 +15,7 @@ var (
errTooManyArguments = errors.New("Error: Too many arguments given") errTooManyArguments = errors.New("Error: Too many arguments given")
) )
func cmdActive(c *cli.Context) error { func cmdActive(c CommandLine) error {
if len(c.Args()) > 0 { if len(c.Args()) > 0 {
return errTooManyArguments return errTooManyArguments
} }

View File

@ -26,6 +26,39 @@ var (
ErrExpectedOneMachine = errors.New("Error: Expected one machine name as an argument") ErrExpectedOneMachine = errors.New("Error: Expected one machine name as an argument")
) )
// CommandLine contains all the information passed to the commands on the command line.
type CommandLine interface {
ShowHelp()
Application() *cli.App
Args() cli.Args
Bool(name string) bool
String(name string) string
StringSlice(name string) []string
GlobalString(name string) string
FlagNames() (names []string)
Generic(name string) interface{}
}
type contextCommandLine struct {
*cli.Context
}
func (c *contextCommandLine) ShowHelp() {
cli.ShowCommandHelp(c.Context, c.Command.Name)
}
func (c *contextCommandLine) Application() *cli.App {
return c.App
}
func newPluginDriver(driverName string, rawContent []byte) (drivers.Driver, error) { func newPluginDriver(driverName string, rawContent []byte) (drivers.Driver, error) {
d, err := rpcdriver.NewRpcClientDriver(rawContent, driverName) d, err := rpcdriver.NewRpcClientDriver(rawContent, driverName)
if err != nil { if err != nil {
@ -39,9 +72,9 @@ func newPluginDriver(driverName string, rawContent []byte) (drivers.Driver, erro
return d, nil return d, nil
} }
func fatalOnError(command func(context *cli.Context) error) func(context *cli.Context) { func fatalOnError(command func(commandLine CommandLine) error) func(context *cli.Context) {
return func(context *cli.Context) { return func(context *cli.Context) {
if err := command(context); err != nil { if err := command(&contextCommandLine{context}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
@ -60,7 +93,7 @@ func confirmInput(msg string) (bool, error) {
return confirmed, nil return confirmed, nil
} }
func getStore(c *cli.Context) persist.Store { func getStore(c CommandLine) persist.Store {
certInfo := getCertPathInfoFromContext(c) certInfo := getCertPathInfoFromContext(c)
return &persist.Filestore{ return &persist.Filestore{
Path: c.GlobalString("storage-path"), Path: c.GlobalString("storage-path"),
@ -115,7 +148,7 @@ func saveHost(store persist.Store, h *host.Host) error {
return nil return nil
} }
func getFirstArgHost(c *cli.Context) (*host.Host, error) { func getFirstArgHost(c CommandLine) (*host.Host, error) {
store := getStore(c) store := getStore(c)
hostName := c.Args().First() hostName := c.Args().First()
@ -139,7 +172,7 @@ func getFirstArgHost(c *cli.Context) (*host.Host, error) {
return h, nil return h, nil
} }
func getHostsFromContext(c *cli.Context) ([]*host.Host, error) { func getHostsFromContext(c CommandLine) ([]*host.Host, error) {
store := getStore(c) store := getStore(c)
hosts := []*host.Host{} hosts := []*host.Host{}
@ -413,7 +446,7 @@ func consolidateErrs(errs []error) error {
return errors.New(strings.TrimSpace(finalErr)) return errors.New(strings.TrimSpace(finalErr))
} }
func runActionWithContext(actionName string, c *cli.Context) error { func runActionWithContext(actionName string, c CommandLine) error {
store := getStore(c) store := getStore(c)
hosts, err := getHostsFromContext(c) hosts, err := getHostsFromContext(c)
@ -442,7 +475,7 @@ func runActionWithContext(actionName string, c *cli.Context) error {
// codegangsta/cli will not set the cert paths if the storage-path is set to // codegangsta/cli will not set the cert paths if the storage-path is set to
// something different so we cannot use the paths in the global options. le // something different so we cannot use the paths in the global options. le
// sigh. // sigh.
func getCertPathInfoFromContext(c *cli.Context) cert.CertPathInfo { func getCertPathInfoFromContext(c CommandLine) cert.CertPathInfo {
caCertPath := c.GlobalString("tls-ca-cert") caCertPath := c.GlobalString("tls-ca-cert")
caKeyPath := c.GlobalString("tls-ca-key") caKeyPath := c.GlobalString("tls-ca-key")
clientCertPath := c.GlobalString("tls-client-cert") clientCertPath := c.GlobalString("tls-client-cert")

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/auth" "github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/cert" "github.com/docker/machine/libmachine/cert"
"github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/host"
@ -27,7 +26,7 @@ Be advised that this will trigger a Docker daemon restart which will stop runnin
`, e.hostUrl, e.wrappedErr) `, e.hostUrl, e.wrappedErr)
} }
func cmdConfig(c *cli.Context) error { func cmdConfig(c CommandLine) error {
// Ensure that log messages always go to stderr when this command is // Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell) // being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr) log.SetOutWriter(os.Stderr)
@ -54,7 +53,7 @@ func cmdConfig(c *cli.Context) error {
return nil return nil
} }
func runConnectionBoilerplate(h *host.Host, c *cli.Context) (string, *auth.AuthOptions, error) { func runConnectionBoilerplate(h *host.Host, c CommandLine) (string, *auth.AuthOptions, error) {
hostState, err := h.Driver.GetState() hostState, err := h.Driver.GetState()
if err != nil { if err != nil {
// TODO: This is a common operation and should have a commonly // TODO: This is a common operation and should have a commonly

View File

@ -118,7 +118,7 @@ var (
} }
) )
func cmdCreateInner(c *cli.Context) error { func cmdCreateInner(c CommandLine) error {
if len(c.Args()) > 1 { if len(c.Args()) > 1 {
return fmt.Errorf("Invalid command line. Found extra arguments %v", c.Args()[1:]) return fmt.Errorf("Invalid command line. Found extra arguments %v", c.Args()[1:])
} }
@ -136,7 +136,7 @@ func cmdCreateInner(c *cli.Context) error {
} }
if name == "" { if name == "" {
cli.ShowCommandHelp(c, "create") c.ShowHelp()
return errNoMachineName return errNoMachineName
} }
@ -270,16 +270,15 @@ func flagHackLookup(flagName string) string {
return "" return ""
} }
func cmdCreateOuter(c *cli.Context) error { func cmdCreateOuter(c CommandLine) error {
const ( const (
flagLookupMachineName = "flag-lookup" flagLookupMachineName = "flag-lookup"
) )
driverName := flagHackLookup("--driver") driverName := flagHackLookup("--driver")
// We didn't recognize the driver name. // We didn't recognize the driver name.
if driverName == "" { if driverName == "" {
cli.ShowCommandHelp(c, "create") c.ShowHelp()
return nil // ? return nil // ?
} }
@ -314,8 +313,8 @@ func cmdCreateOuter(c *cli.Context) error {
return fmt.Errorf("Error trying to convert provided driver flags to cli flags: %s", err) return fmt.Errorf("Error trying to convert provided driver flags to cli flags: %s", err)
} }
for i := range c.App.Commands { for i := range c.Application().Commands {
cmd := &c.App.Commands[i] cmd := &c.Application().Commands[i]
if cmd.HasName("create") { if cmd.HasName("create") {
cmd = addDriverFlagsToCommand(cliFlags, cmd) cmd = addDriverFlagsToCommand(cliFlags, cmd)
} }
@ -327,10 +326,10 @@ func cmdCreateOuter(c *cli.Context) error {
} }
} }
return c.App.Run(os.Args) return c.Application().Run(os.Args)
} }
func getDriverOpts(c *cli.Context, mcnflags []mcnflag.Flag) drivers.DriverOptions { func getDriverOpts(c CommandLine, mcnflags []mcnflag.Flag) drivers.DriverOptions {
// TODO: This function is pretty damn YOLO and would benefit from some // TODO: This function is pretty damn YOLO and would benefit from some
// sanity checking around types and assertions. // sanity checking around types and assertions.
// //

View File

@ -8,7 +8,6 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/docker/machine/cli"
"github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
) )
@ -34,7 +33,7 @@ type ShellConfig struct {
NoProxyValue string NoProxyValue string
} }
func cmdEnv(c *cli.Context) error { func cmdEnv(c CommandLine) error {
// Ensure that log messages always go to stderr when this command is // Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell) // being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr) log.SetOutWriter(os.Stderr)

View File

@ -5,8 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"text/template" "text/template"
"github.com/docker/machine/cli"
) )
var funcMap = template.FuncMap{ var funcMap = template.FuncMap{
@ -20,9 +18,9 @@ var funcMap = template.FuncMap{
}, },
} }
func cmdInspect(c *cli.Context) error { func cmdInspect(c CommandLine) error {
if len(c.Args()) == 0 { if len(c.Args()) == 0 {
cli.ShowCommandHelp(c, "inspect") c.ShowHelp()
return ErrExpectedOneMachine return ErrExpectedOneMachine
} }

View File

@ -1,7 +1,5 @@
package commands package commands
import "github.com/docker/machine/cli" func cmdIP(c CommandLine) error {
func cmdIP(c *cli.Context) error {
return runActionWithContext("ip", c) return runActionWithContext("ip", c)
} }

View File

@ -1,7 +1,5 @@
package commands package commands
import "github.com/docker/machine/cli" func cmdKill(c CommandLine) error {
func cmdKill(c *cli.Context) error {
return runActionWithContext("kill", c) return runActionWithContext("kill", c)
} }

View File

@ -10,7 +10,6 @@ import (
"text/tabwriter" "text/tabwriter"
"time" "time"
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
@ -40,7 +39,7 @@ type HostListItem struct {
SwarmOptions *swarm.SwarmOptions SwarmOptions *swarm.SwarmOptions
} }
func cmdLs(c *cli.Context) error { func cmdLs(c CommandLine) error {
quiet := c.Bool("quiet") quiet := c.Bool("quiet")
filters, err := parseFilters(c.StringSlice("filter")) filters, err := parseFilters(c.StringSlice("filter"))
if err != nil { if err != nil {

View File

@ -1,11 +1,10 @@
package commands package commands
import ( import (
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
) )
func cmdRegenerateCerts(c *cli.Context) error { func cmdRegenerateCerts(c CommandLine) error {
if !c.Bool("force") { if !c.Bool("force") {
ok, err := confirmInput("Regenerate TLS machine certs? Warning: this is irreversible.") ok, err := confirmInput("Regenerate TLS machine certs? Warning: this is irreversible.")
if err != nil { if err != nil {

View File

@ -2,11 +2,9 @@ package commands
import ( import (
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"github.com/docker/machine/cli"
) )
func cmdRestart(c *cli.Context) error { func cmdRestart(c CommandLine) error {
if err := runActionWithContext("restart", c); err != nil { if err := runActionWithContext("restart", c); err != nil {
return err return err
} }

View File

@ -4,13 +4,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
) )
func cmdRm(c *cli.Context) error { func cmdRm(c CommandLine) error {
if len(c.Args()) == 0 { if len(c.Args()) == 0 {
cli.ShowCommandHelp(c, "rm") c.ShowHelp()
return errors.New("You must specify a machine name") return errors.New("You must specify a machine name")
} }

View File

@ -7,7 +7,6 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/persist" "github.com/docker/machine/libmachine/persist"
) )
@ -53,10 +52,10 @@ func (s *storeHostInfoLoader) load(name string) (HostInfo, error) {
return host.Driver, nil return host.Driver, nil
} }
func cmdScp(c *cli.Context) error { func cmdScp(c CommandLine) error {
args := c.Args() args := c.Args()
if len(args) != 2 { if len(args) != 2 {
cli.ShowCommandHelp(c, "scp") c.ShowHelp()
return errWrongNumberArguments return errWrongNumberArguments
} }

View File

@ -3,15 +3,14 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
) )
func cmdSSH(c *cli.Context) error { func cmdSSH(c CommandLine) error {
// Check for help flag -- Needed due to SkipFlagParsing // Check for help flag -- Needed due to SkipFlagParsing
for _, arg := range c.Args() { for _, arg := range c.Args() {
if arg == "-help" || arg == "--help" || arg == "-h" { if arg == "-help" || arg == "--help" || arg == "-h" {
cli.ShowCommandHelp(c, "ssh") c.ShowHelp()
return nil return nil
} }
} }

View File

@ -2,11 +2,9 @@ package commands
import ( import (
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"github.com/docker/machine/cli"
) )
func cmdStart(c *cli.Context) error { func cmdStart(c CommandLine) error {
if err := runActionWithContext("start", c); err != nil { if err := runActionWithContext("start", c); err != nil {
return err return err
} }

View File

@ -1,11 +1,10 @@
package commands package commands
import ( import (
"github.com/docker/machine/cli"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
) )
func cmdStatus(c *cli.Context) error { func cmdStatus(c CommandLine) error {
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
return ErrExpectedOneMachine return ErrExpectedOneMachine
} }

View File

@ -1,7 +1,5 @@
package commands package commands
import "github.com/docker/machine/cli" func cmdStop(c CommandLine) error {
func cmdStop(c *cli.Context) error {
return runActionWithContext("stop", c) return runActionWithContext("stop", c)
} }

View File

@ -1,7 +1,5 @@
package commands package commands
import "github.com/docker/machine/cli" func cmdUpgrade(c CommandLine) error {
func cmdUpgrade(c *cli.Context) error {
return runActionWithContext("upgrade", c) return runActionWithContext("upgrade", c)
} }

View File

@ -2,11 +2,9 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/docker/machine/cli"
) )
func cmdURL(c *cli.Context) error { func cmdURL(c CommandLine) error {
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
return ErrExpectedOneMachine return ErrExpectedOneMachine
} }