mirror of
https://github.com/status-im/consul.git
synced 2025-02-08 11:54:12 +00:00
command/services/deregister: -id flag for deletion
This commit is contained in:
parent
2f97a618dc
commit
3425f123ef
@ -4,6 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/api"
|
||||||
"github.com/hashicorp/consul/command/flags"
|
"github.com/hashicorp/consul/command/flags"
|
||||||
"github.com/hashicorp/consul/command/services"
|
"github.com/hashicorp/consul/command/services"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
@ -16,14 +17,17 @@ func New(ui cli.Ui) *cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type cmd struct {
|
type cmd struct {
|
||||||
UI cli.Ui
|
UI cli.Ui
|
||||||
flags *flag.FlagSet
|
flags *flag.FlagSet
|
||||||
http *flags.HTTPFlags
|
http *flags.HTTPFlags
|
||||||
help string
|
help string
|
||||||
|
flagId string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cmd) init() {
|
func (c *cmd) init() {
|
||||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
c.flags.StringVar(&c.flagId, "id", "",
|
||||||
|
"ID to delete. This must not be set if arguments are given.")
|
||||||
|
|
||||||
c.http = &flags.HTTPFlags{}
|
c.http = &flags.HTTPFlags{}
|
||||||
flags.Merge(c.flags, c.http.ClientFlags())
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
@ -38,15 +42,20 @@ func (c *cmd) Run(args []string) int {
|
|||||||
|
|
||||||
// Check for arg validation
|
// Check for arg validation
|
||||||
args = c.flags.Args()
|
args = c.flags.Args()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 && c.flagId == "" {
|
||||||
c.UI.Error("Service deregistration requires at least one argument.")
|
c.UI.Error("Service deregistration requires at least one argument or -id.")
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
svcs, err := services.ServicesFromFiles(args)
|
svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{
|
||||||
if err != nil {
|
ID: c.flagId}}
|
||||||
c.UI.Error(fmt.Sprintf("Error: %s", err))
|
if len(args) > 0 {
|
||||||
return 1
|
var err error
|
||||||
|
svcs, err = services.ServicesFromFiles(args)
|
||||||
|
if err != nil {
|
||||||
|
c.UI.Error(fmt.Sprintf("Error: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
|
@ -93,6 +93,36 @@ func TestCommand_File_nameOnly(t *testing.T) {
|
|||||||
require.NotNil(svcs["db"])
|
require.NotNil(svcs["db"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCommand_Flag(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
require := require.New(t)
|
||||||
|
a := agent.NewTestAgent(t.Name(), ``)
|
||||||
|
defer a.Shutdown()
|
||||||
|
client := a.Client()
|
||||||
|
|
||||||
|
// Register a service
|
||||||
|
require.NoError(client.Agent().ServiceRegister(&api.AgentServiceRegistration{
|
||||||
|
Name: "web"}))
|
||||||
|
require.NoError(client.Agent().ServiceRegister(&api.AgentServiceRegistration{
|
||||||
|
Name: "db"}))
|
||||||
|
|
||||||
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
|
"-id", "web",
|
||||||
|
}
|
||||||
|
|
||||||
|
require.Equal(0, c.Run(args), ui.ErrorWriter.String())
|
||||||
|
|
||||||
|
svcs, err := client.Agent().Services()
|
||||||
|
require.NoError(err)
|
||||||
|
require.Len(svcs, 1)
|
||||||
|
require.NotNil(svcs["db"])
|
||||||
|
}
|
||||||
|
|
||||||
func testFile(t *testing.T, suffix string) *os.File {
|
func testFile(t *testing.T, suffix string) *os.File {
|
||||||
f := testutil.TempFile(t, "register-test-file")
|
f := testutil.TempFile(t, "register-test-file")
|
||||||
if err := f.Close(); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user