command: add helpers
This commit is contained in:
parent
9958e90632
commit
4d5901c1ae
38
command/helpers.go
Normal file
38
command/helpers.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Environment variable used to control the HTTP address
|
||||||
|
// we connect to while using various commands. This may
|
||||||
|
// be overridden using the -http-addr flag.
|
||||||
|
HttpEnvVar = "NOMAD_HTTP_ADDR"
|
||||||
|
|
||||||
|
// DefaultHTTPAddr is the default address used for the
|
||||||
|
// HTTP address flag.
|
||||||
|
DefaultHttpAddr = "http://127.0.0.1:4646"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HttpAddrFlag is used to add the -http-addr flag to a flag
|
||||||
|
// set. Allows setting the value from an environment variable.
|
||||||
|
func HttpAddrFlag(f *flag.FlagSet) *string {
|
||||||
|
defaultAddr := os.Getenv(HttpEnvVar)
|
||||||
|
if defaultAddr == "" {
|
||||||
|
defaultAddr = DefaultHttpAddr
|
||||||
|
}
|
||||||
|
return f.String("http-addr", defaultAddr,
|
||||||
|
"HTTP address of the Nomad agent")
|
||||||
|
}
|
||||||
|
|
||||||
|
// HttpClient is used to get a new Nomad client using the
|
||||||
|
// given address.
|
||||||
|
func HttpClient(addr string) (*api.Client, error) {
|
||||||
|
conf := api.DefaultConfig()
|
||||||
|
conf.URL = addr
|
||||||
|
return api.NewClient(conf)
|
||||||
|
}
|
44
command/helpers_test.go
Normal file
44
command/helpers_test.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHelpers_HttpAddrFlag(t *testing.T) {
|
||||||
|
var addr *string
|
||||||
|
|
||||||
|
// Returns the default
|
||||||
|
flags := flag.NewFlagSet("test", flag.ContinueOnError)
|
||||||
|
addr = HttpAddrFlag(flags)
|
||||||
|
if err := flags.Parse([]string{}); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if *addr != DefaultHttpAddr {
|
||||||
|
t.Fatalf("expect %q, got: %q", DefaultHttpAddr, *addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns from the env var
|
||||||
|
if err := os.Setenv(HttpEnvVar, "http://127.0.0.1:1111"); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
flags = flag.NewFlagSet("test", flag.ContinueOnError)
|
||||||
|
addr = HttpAddrFlag(flags)
|
||||||
|
if err := flags.Parse([]string{}); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if *addr != "http://127.0.0.1:1111" {
|
||||||
|
t.Fatalf("expect %q, got: %q", "http://127.0.0.1:1111", *addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns from flag
|
||||||
|
flags = flag.NewFlagSet("test", flag.ContinueOnError)
|
||||||
|
addr = HttpAddrFlag(flags)
|
||||||
|
if err := flags.Parse([]string{"-http-addr", "http://127.0.0.1:2222"}); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if *addr != "http://127.0.0.1:2222" {
|
||||||
|
t.Fatalf("expect %q, got: %q", "http://127.0.0.1:2222", *addr)
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,6 +34,12 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"status": func() (cli.Command, error) {
|
||||||
|
return &command.StatusCommand{
|
||||||
|
Ui: meta.Ui,
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
|
||||||
"version": func() (cli.Command, error) {
|
"version": func() (cli.Command, error) {
|
||||||
ver := Version
|
ver := Version
|
||||||
rel := VersionPrerelease
|
rel := VersionPrerelease
|
||||||
|
|
Loading…
Reference in a new issue