From 0b1efbf007e661a01d1a21b04d0c1e4786ff696c Mon Sep 17 00:00:00 2001 From: Lucas Jenss <lucas@x3ro.de> Date: Sun, 17 Jan 2016 23:36:14 +0100 Subject: [PATCH] Change unregister command to accept runner name --- commands/config.go | 15 +++++++++++++++ commands/unregister.go | 21 ++++++++++++++++----- docs/commands/README.md | 18 ++++++++++++++---- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/commands/config.go b/commands/config.go index 91d00a5d5..d9863f4f1 100644 --- a/commands/config.go +++ b/commands/config.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/network" "os" @@ -49,6 +50,20 @@ func (c *configOptions) touchConfig() error { return nil } +func (c *configOptions) RunnerByName(name string) (*common.RunnerConfig, error) { + if c.config == nil { + return nil, fmt.Errorf("Config has not been loaded") + } + + for _, runner := range c.config.Runners { + if runner.Name == name { + return runner, nil + } + } + + return nil, fmt.Errorf("Could not find a runner with the name '%s'", name) +} + func init() { configFile := os.Getenv("CONFIG_FILE") if configFile == "" { diff --git a/commands/unregister.go b/commands/unregister.go index 660e63e2c..0487777bb 100644 --- a/commands/unregister.go +++ b/commands/unregister.go @@ -12,18 +12,29 @@ type UnregisterCommand struct { configOptions common.RunnerCredentials network common.Network + Name string `toml:"name" json:"name" short:"n" long:"name" description:"Name of the runner you wish to unregister"` } func (c *UnregisterCommand) Execute(context *cli.Context) { userModeWarning(false) - if !c.network.DeleteRunner(c.RunnerCredentials) { - log.Fatalln("Failed to delete runner") - } - err := c.loadConfig() if err != nil { - log.Warningln(err) + log.Fatalln(err) + return + } + + if len(c.Name) > 0 { + runnerConfig, err := c.RunnerByName(c.Name) + if err != nil { + log.Fatalln(err) + return + } + c.RunnerCredentials = runnerConfig.RunnerCredentials + } + + if !c.network.DeleteRunner(c.RunnerCredentials) { + log.Fatalln("Failed to delete runner", c.Name) return } diff --git a/docs/commands/README.md b/docs/commands/README.md index 99b19745c..680c3c2f8 100644 --- a/docs/commands/README.md +++ b/docs/commands/README.md @@ -288,22 +288,32 @@ gitlab-runner verify --delete ### gitlab-runner unregister -This command allows to unregister one of the registered runners. It expects to -enter a full URL and the runner's token. First get the runner's details by +This command allows to unregister one of the registered runners. It expects either +a full URL and the runner's token, or the runner's name. First get the runner's details by executing `gitlab-runner list`: ```bash test-runner Executor=shell Token=t0k3n URL=http://gitlab.example.com/ci/ ``` -Then use this information to unregister it, using the following command. +Then use this information to unregister it, using one of the following commands. >**Warning:** This operation cannot be undone, it will update the configuration file, so make sure to have a backup of `config.toml` before executing it. +#### By URL and token: + +```bash +gitlab-runner unregister --url http://gitlab.example.com/ci/ --token t0k3n +``` + +#### By name: + +> **Note:** If there is more than one runner with the given name, only the first one will be removed + ```bash -gitlab-runner unregister -u http://gitlab.example.com/ci/ -t t0k3n +gitlab-runner unregister --name test-runner ``` ## Service-related commands -- GitLab