Merge pull request #642 from aluzzardi/cli-cleanup

Cleanup: Move CLI related files to a subpackage.
This commit is contained in:
Victor Vieux 2015-04-20 12:00:47 -07:00
commit 279ce86c40
10 changed files with 135 additions and 129 deletions

123
cli/cli.go Normal file
View File

@ -0,0 +1,123 @@
package cli
import (
"fmt"
"os"
"path"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/swarm/discovery"
"github.com/docker/swarm/discovery/token"
"github.com/docker/swarm/version"
)
// Run the Swarm CLI.
func Run() {
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "a Docker-native clustering system"
app.Version = version.VERSION + " (" + version.GITCOMMIT + ")"
app.Author = ""
app.Email = ""
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: fmt.Sprintf("Log level (options: debug, info, warn, error, fatal, panic)"),
},
}
// logs
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "create",
ShortName: "c",
Usage: "create a cluster",
Action: func(c *cli.Context) {
discovery := &token.Discovery{}
discovery.Initialize("", 0)
token, err := discovery.CreateCluster()
if len(c.Args()) != 0 {
log.Fatalf("the `create` command takes no arguments. See '%s create --help'.", c.App.Name)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(token)
},
},
{
Name: "list",
ShortName: "l",
Usage: "list nodes in a cluster",
Action: func(c *cli.Context) {
dflag := getDiscovery(c)
if dflag == "" {
log.Fatalf("discovery required to list a cluster. See '%s list --help'.", c.App.Name)
}
d, err := discovery.New(dflag, 0)
if err != nil {
log.Fatal(err)
}
nodes, err := d.Fetch()
if err != nil {
log.Fatal(err)
}
for _, node := range nodes {
fmt.Println(node)
}
},
},
{
Name: "manage",
ShortName: "m",
Usage: "manage a docker cluster",
Flags: []cli.Flag{
flStore,
flStrategy, flFilter,
flHosts, flHeartBeat, flOverCommit,
flTLS, flTLSCaCert, flTLSCert, flTLSKey, flTLSVerify,
flEnableCors},
Action: manage,
},
{
Name: "join",
ShortName: "j",
Usage: "join a docker cluster",
Flags: []cli.Flag{flAddr, flHeartBeat},
Action: join,
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}

View File

@ -1,4 +1,4 @@
package main
package cli
import (
"os"

View File

@ -1,4 +1,4 @@
package main
package cli
import (
"os"

View File

@ -1,4 +1,4 @@
package main
package cli
import (
"regexp"

View File

@ -1,4 +1,4 @@
package main
package cli
import (
"testing"

View File

@ -1,4 +1,4 @@
package main
package cli
import (
"crypto/tls"

119
main.go
View File

@ -1,127 +1,16 @@
package main
import (
"fmt"
"os"
"path"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/swarm/discovery"
_ "github.com/docker/swarm/discovery/consul"
_ "github.com/docker/swarm/discovery/etcd"
_ "github.com/docker/swarm/discovery/file"
_ "github.com/docker/swarm/discovery/nodes"
"github.com/docker/swarm/discovery/token"
_ "github.com/docker/swarm/discovery/token"
_ "github.com/docker/swarm/discovery/zookeeper"
"github.com/docker/swarm/version"
"github.com/docker/swarm/cli"
)
func main() {
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "a Docker-native clustering system"
app.Version = version.VERSION + " (" + version.GITCOMMIT + ")"
app.Author = ""
app.Email = ""
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: fmt.Sprintf("Log level (options: debug, info, warn, error, fatal, panic)"),
},
}
// logs
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "create",
ShortName: "c",
Usage: "create a cluster",
Action: func(c *cli.Context) {
discovery := &token.Discovery{}
discovery.Initialize("", 0)
token, err := discovery.CreateCluster()
if len(c.Args()) != 0 {
log.Fatalf("the `create` command takes no arguments. See '%s create --help'.", c.App.Name)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(token)
},
},
{
Name: "list",
ShortName: "l",
Usage: "list nodes in a cluster",
Action: func(c *cli.Context) {
dflag := getDiscovery(c)
if dflag == "" {
log.Fatalf("discovery required to list a cluster. See '%s list --help'.", c.App.Name)
}
d, err := discovery.New(dflag, 0)
if err != nil {
log.Fatal(err)
}
nodes, err := d.Fetch()
if err != nil {
log.Fatal(err)
}
for _, node := range nodes {
fmt.Println(node)
}
},
},
{
Name: "manage",
ShortName: "m",
Usage: "manage a docker cluster",
Flags: []cli.Flag{
flStore,
flStrategy, flFilter,
flHosts, flHeartBeat, flOverCommit,
flTLS, flTLSCaCert, flTLSCert, flTLSKey, flTLSVerify,
flEnableCors},
Action: manage,
},
{
Name: "join",
ShortName: "j",
Usage: "join a docker cluster",
Flags: []cli.Flag{flAddr, flHeartBeat},
Action: join,
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
cli.Run()
}

View File

@ -4,8 +4,8 @@ MODE="mode: count"
ROOT=${TRAVIS_BUILD_DIR:-.}/../../..
# Grab the list of packages.
# Exclude the API from coverage as it will be covered by integration tests.
PACKAGES=`go list ./... | grep -v github.com/docker/swarm/api`
# Exclude the API and CLI from coverage as it will be covered by integration tests.
PACKAGES=`go list ./... | grep -v github.com/docker/swarm/api | grep -v github.com/docker/swarm/cli`
# Create the empty coverage file.
echo $MODE > goverage.report

View File

@ -21,12 +21,6 @@ Start by [installing]
(https://github.com/sstephenson/bats#installing-bats-from-source) *bats* on
your system.
The tests expect the *swarm* binary to be built and located at the root
directory of the repo:
```
$ godep go build
```
In order to run all integration tests, pass *bats* the test path:
```
$ bats test/integration

View File

@ -22,7 +22,7 @@ function join() {
# Run the swarm binary.
function swarm() {
${SWARM_ROOT}/swarm "$@"
godep go run "${SWARM_ROOT}/main.go" "$@"
}
# Waits until the given docker engine API becomes reachable.