diff --git a/action/repo/get.go b/action/repo/get.go index 0c4ebcf9..dec0cb14 100644 --- a/action/repo/get.go +++ b/action/repo/get.go @@ -12,7 +12,7 @@ import ( "github.com/sirupsen/logrus" ) -// Get captures a list of deployments based off the provided configuration. +// Get captures a list of repositories based off the provided configuration. func (c *Config) Get(client *vela.Client) error { logrus.Debug("executing get for repo configuration") diff --git a/action/worker/add.go b/action/worker/add.go new file mode 100644 index 00000000..b624c2b3 --- /dev/null +++ b/action/worker/add.go @@ -0,0 +1,69 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/internal/output" + + "github.com/go-vela/sdk-go/vela" + + "github.com/sirupsen/logrus" +) + +// Add creates a worker based off the provided configuration. +func (c *Config) Add(client *vela.Client) error { + logrus.Debug("executing add for worker configuration") + + // send API call to get a registration token for the given worker + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela#AdminWorkerService.RegisterToken + registerToken, _, err := client.Admin.Worker.RegisterToken(c.Hostname) + if err != nil || registerToken == nil { + return fmt.Errorf("unable to retrieve registration token: %w", err) + } + + logrus.Tracef("got registration token, adding worker %q", c.Hostname) + + // send API call to register a worker + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela#AdminWorkerService.Register + msg, _, err := client.Admin.Worker.Register(c.Address, registerToken.GetToken()) + if err != nil { + return fmt.Errorf("unable to register worker at %q: %w", c.Address, err) + } + + logrus.Tracef("worker %q registered", c.Hostname) + + // handle the output based off the provided configuration + switch c.Output { + case output.DriverDump: + // output the worker in dump format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump + return output.Dump(msg) + case output.DriverJSON: + // output the worker in JSON format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON + return output.JSON(msg) + case output.DriverSpew: + // output the worker in spew format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew + return output.Spew(msg) + case output.DriverYAML: + // output the worker in YAML format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML + return output.YAML(msg) + default: + // output the worker in stdout format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout + return output.Stdout(*msg) + } +} diff --git a/action/worker/add_test.go b/action/worker/add_test.go new file mode 100644 index 00000000..c46a5c03 --- /dev/null +++ b/action/worker/add_test.go @@ -0,0 +1,120 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/go-vela/server/mock/server" + "github.com/go-vela/worker/mock/worker" + + "github.com/go-vela/sdk-go/vela" +) + +func TestWorker_Config_Add(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + + // create mock server + s := httptest.NewServer(server.FakeHandler()) + + // create a vela client + client, err := vela.NewClient(s.URL, "vela", nil) + if err != nil { + t.Errorf("unable to create client: %v", err) + } + + // create mock worker server + w := httptest.NewServer(worker.FakeHandler()) + + // setup tests + tests := []struct { + failure bool + config *Config + }{ + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: w.URL, + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: w.URL, + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: w.URL, + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: w.URL, + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: w.URL, + Output: "yaml", + }, + }, + { + failure: true, + config: &Config{ + Action: "add", + Hostname: "", + Address: w.URL, + Output: "yaml", + }, + }, + { + failure: true, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "", + Output: "yaml", + }, + }, + } + + // run tests + for _, test := range tests { + err := test.config.Add(client) + + if test.failure { + if err == nil { + t.Errorf("Add should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("Add returned err: %v", err) + } + } +} diff --git a/action/worker/doc.go b/action/worker/doc.go new file mode 100644 index 00000000..d19588c8 --- /dev/null +++ b/action/worker/doc.go @@ -0,0 +1,10 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// Package worker provides the defined CLI worker actions for Vela. +// +// Usage: +// +// import "github.com/go-vela/cli/action/worker" +package worker diff --git a/action/worker/get.go b/action/worker/get.go new file mode 100644 index 00000000..a4ce55a2 --- /dev/null +++ b/action/worker/get.go @@ -0,0 +1,58 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "github.com/go-vela/cli/internal/output" + + "github.com/go-vela/sdk-go/vela" + + "github.com/sirupsen/logrus" +) + +// Get captures a list of workers based off the provided configuration. +func (c *Config) Get(client *vela.Client) error { + logrus.Debug("executing get for worker configuration") + + logrus.Tracef("capturing workers") + + // send API call to capture a list of workers + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#WorkerService.GetAll + workers, _, err := client.Worker.GetAll() + if err != nil { + return err + } + + // handle the output based off the provided configuration + switch c.Output { + case output.DriverDump: + // output the workers in dump format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump + return output.Dump(workers) + case output.DriverJSON: + // output the workers in JSON format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON + return output.JSON(workers) + case output.DriverSpew: + // output the workers in spew format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew + return output.Spew(workers) + case "wide": + // output the workers in wide table format + return wideTable(workers) + case output.DriverYAML: + // output the workers in YAML format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML + return output.YAML(workers) + default: + // output the workers in table format + return table(workers) + } +} diff --git a/action/worker/get_test.go b/action/worker/get_test.go new file mode 100644 index 00000000..8c33104d --- /dev/null +++ b/action/worker/get_test.go @@ -0,0 +1,92 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "net/http/httptest" + "testing" + + "github.com/go-vela/server/mock/server" + + "github.com/go-vela/sdk-go/vela" +) + +func TestWorker_Config_Get(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // create a vela client + client, err := vela.NewClient(s.URL, "vela", nil) + if err != nil { + t.Errorf("unable to create client: %v", err) + } + + // setup tests + tests := []struct { + failure bool + config *Config + }{ + { + failure: false, + config: &Config{ + Action: "get", + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Output: "wide", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Output: "yaml", + }, + }, + // TODO: mock doesn't have failure for listing workers + } + + // run tests + for _, test := range tests { + err := test.config.Get(client) + + if test.failure { + if err == nil { + t.Errorf("Get should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("Get returned err: %v", err) + } + } +} diff --git a/action/worker/table.go b/action/worker/table.go new file mode 100644 index 00000000..f40d49b1 --- /dev/null +++ b/action/worker/table.go @@ -0,0 +1,109 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "time" + + "github.com/go-vela/cli/internal/output" + "github.com/go-vela/types/library" + + "github.com/dustin/go-humanize" + "github.com/gosuri/uitable" + "github.com/sirupsen/logrus" +) + +// table is a helper function to output the +// provided workers in a table format with +// a specific set of fields displayed. +func table(workers *[]library.Worker) error { + logrus.Debug("creating table for list of workers") + + // create a new table + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#New + table := uitable.New() + + // set column width for table to 50 + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table + table.MaxColWidth = 50 + + // ensure the table is always wrapped + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table + table.Wrap = true + + logrus.Trace("adding headers to worker table") + + // set of worker fields we display in a table + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow + table.AddRow("HOSTNAME", "ACTIVE", "ROUTES", "LAST_CHECKED_IN") + + // iterate through all workers in the list + for _, w := range *workers { + logrus.Tracef("adding worker %s to worker table", w.GetHostname()) + + // add a row to the table with the specified values + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow + table.AddRow(w.GetHostname(), w.GetActive(), w.GetRoutes(), w.GetLastCheckedIn()) + } + + // output the table in stdout format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout + return output.Stdout(table) +} + +// wideTable is a helper function to output the +// provided workers in a wide table format with +// a specific set of fields displayed. +func wideTable(workers *[]library.Worker) error { + logrus.Debug("creating wide table for list of workers") + + // create new wide table + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#New + table := uitable.New() + + // set column width for wide table to 200 + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table + table.MaxColWidth = 200 + + // ensure the wide table is always wrapped + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table + table.Wrap = true + + logrus.Trace("adding headers to wide worker table") + + // set of worker fields we display in a wide table + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow + table.AddRow("HOSTNAME", "ADDRESS", "ACTIVE", "ROUTES", "LAST_CHECKED_IN", "BUILD_LIMIT") + + // iterate through all workers in the list + for _, w := range *workers { + logrus.Tracef("adding worker %s to wide worker table", w.GetHostname()) + + // calculate last checked in timestamp in human readable form + // + // https://pkg.go.dev/github.com/dustin/go-humanize?tab=doc#Time + c := humanize.Time(time.Unix(w.GetLastCheckedIn(), 0)) + + // add a row to the table with the specified values + // + // https://pkg.go.dev/github.com/gosuri/uitable?tab=doc#Table.AddRow + table.AddRow(w.GetHostname(), w.GetAddress(), w.GetActive(), w.GetRoutes(), c, w.GetBuildLimit()) + } + + // output the wide table in stdout format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout + return output.Stdout(table) +} diff --git a/action/worker/table_test.go b/action/worker/table_test.go new file mode 100644 index 00000000..f68df362 --- /dev/null +++ b/action/worker/table_test.go @@ -0,0 +1,114 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "testing" + + "github.com/go-vela/types/library" +) + +func TestWorker_table(t *testing.T) { + // setup types + w1 := testWorker() + w1.SetID(1) + + w2 := testWorker() + w2.SetID(2) + w2.SetHostname("MyWorker2") + w2.SetAddress("myworker2.example.com") + w2.SetRoutes([]string{"this", "that"}) + + // setup tests + tests := []struct { + failure bool + workers *[]library.Worker + }{ + { + failure: false, + workers: &[]library.Worker{ + *w1, + *w2, + }, + }, + } + + // run tests + for _, test := range tests { + err := table(test.workers) + + if test.failure { + if err == nil { + t.Errorf("table should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("table returned err: %v", err) + } + } +} + +func TestWorker_wideTable(t *testing.T) { + // setup types + w1 := testWorker() + w1.SetID(1) + w1.SetHostname("MyWorker") + + w2 := testWorker() + w2.SetID(2) + w2.SetHostname("MyWorker2") + w2.SetAddress("myworker2.example.com") + w2.SetRoutes([]string{"this", "that"}) + + // setup tests + tests := []struct { + failure bool + workers *[]library.Worker + }{ + { + failure: false, + workers: &[]library.Worker{ + *w1, + *w2, + }, + }, + } + + // run tests + for _, test := range tests { + err := wideTable(test.workers) + + if test.failure { + if err == nil { + t.Errorf("wideTable should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("wideTable returned err: %v", err) + } + } +} + +// testWorker is a test helper function to create a Worker +// type with all fields set to a fake value. +func testWorker() *library.Worker { + w := new(library.Worker) + + w.SetID(1) + w.SetActive(true) + w.SetBuildLimit(1) + w.SetAddress("myworker.example.com") + w.SetRoutes([]string{"small", "large"}) + w.SetHostname("MyWorker") + w.SetLastCheckedIn(int64(4)) + + return w +} diff --git a/action/worker/update.go b/action/worker/update.go new file mode 100644 index 00000000..5bd0d27e --- /dev/null +++ b/action/worker/update.go @@ -0,0 +1,70 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "github.com/go-vela/cli/internal/output" + + "github.com/go-vela/sdk-go/vela" + + "github.com/go-vela/types/library" + + "github.com/sirupsen/logrus" +) + +// Update modifies a worker based off the provided configuration. +func (c *Config) Update(client *vela.Client) error { + logrus.Debug("executing update for worker configuration") + + // create the worker object + // + // https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Worker + w := &library.Worker{ + Hostname: vela.String(c.Hostname), + Address: vela.String(c.Address), + Active: vela.Bool(c.Active), + Routes: vela.Strings(c.Routes), + BuildLimit: vela.Int64(c.BuildLimit), + } + + logrus.Tracef("updating worker %s", c.Hostname) + + // send API call to modify a worker + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#WorkerService.Update + worker, _, err := client.Worker.Update(c.Hostname, w) + if err != nil { + return err + } + + // handle the output based off the provided configuration + switch c.Output { + case output.DriverDump: + // output the worker in dump format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump + return output.Dump(worker) + case output.DriverJSON: + // output the worker in JSON format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON + return output.JSON(worker) + case output.DriverSpew: + // output the worker in spew format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew + return output.Spew(worker) + case output.DriverYAML: + // output the worker in YAML format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML + return output.YAML(worker) + default: + // output the worker in stdout format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout + return output.Stdout(worker) + } +} diff --git a/action/worker/update_test.go b/action/worker/update_test.go new file mode 100644 index 00000000..cf7cd672 --- /dev/null +++ b/action/worker/update_test.go @@ -0,0 +1,121 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "net/http/httptest" + "testing" + + "github.com/go-vela/server/mock/server" + + "github.com/go-vela/sdk-go/vela" +) + +func TestWorker_Config_Update(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // create a vela client + client, err := vela.NewClient(s.URL, "vela", nil) + if err != nil { + t.Errorf("unable to create client: %v", err) + } + + // setup tests + tests := []struct { + failure bool + config *Config + }{ + { + failure: false, + config: &Config{ + Action: "update", + Hostname: "MyWorker", + Address: "myworker.example.com", + Routes: []string{"large", "small"}, + BuildLimit: 1, + Active: true, + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "update", + Hostname: "MyWorker", + Address: "myworker.example.com", + Routes: []string{"large", "small"}, + BuildLimit: 1, + Active: true, + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "update", + Hostname: "MyWorker", + Address: "myworker.example.com", + Routes: []string{"large", "small"}, + BuildLimit: 1, + Active: true, + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "update", + Hostname: "MyWorker", + Address: "myworker.example.com", + Routes: []string{"large", "small"}, + BuildLimit: 1, + Active: true, + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "update", + Hostname: "MyWorker", + Address: "myworker.example.com", + Routes: []string{"large", "small"}, + BuildLimit: 1, + Active: true, + Output: "yaml", + }, + }, + { + failure: true, + config: &Config{ + Action: "update", + Hostname: "0", + Address: "myworker.example.com", + Routes: []string{"large", "small"}, + BuildLimit: 1, + Active: true, + Output: "", + }, + }, + } + + // run tests + for _, test := range tests { + err := test.config.Update(client) + + if test.failure { + if err == nil { + t.Errorf("Update should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("Update returned err: %v", err) + } + } +} diff --git a/action/worker/validate.go b/action/worker/validate.go new file mode 100644 index 00000000..13a81572 --- /dev/null +++ b/action/worker/validate.go @@ -0,0 +1,29 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/internal" + "github.com/sirupsen/logrus" +) + +// Validate verifies the configuration provided. +func (c *Config) Validate() error { + logrus.Debug("validating worker configuration") + + // we need address for adding a worker + if c.Action == internal.ActionAdd && len(c.Address) == 0 { + return fmt.Errorf("no worker address provided") + } + + // anything other than "get" action needs hostname + if c.Action != internal.ActionGet && len(c.Hostname) == 0 { + return fmt.Errorf("no worker hostname provided") + } + + return nil +} diff --git a/action/worker/validate_test.go b/action/worker/validate_test.go new file mode 100644 index 00000000..cc61fedc --- /dev/null +++ b/action/worker/validate_test.go @@ -0,0 +1,109 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "testing" +) + +func TestWorker_Config_Validate(t *testing.T) { + // setup tests + tests := []struct { + failure bool + config *Config + }{ + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "myworker.example.com", + Output: "", + }, + }, + { + failure: true, + config: &Config{ + Action: "add", + Hostname: "", + Address: "myworker.example.com", + Output: "", + }, + }, + { + failure: true, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "", + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + Output: "", + }, + }, + { + failure: true, + config: &Config{ + Action: "view", + Hostname: "", + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "update", + Hostname: "MyWorker", + Address: "myworker.example.com", + Active: true, + BuildLimit: 1, + Routes: []string{"large", "small"}, + Output: "", + }, + }, + { + failure: true, + config: &Config{ + Action: "update", + Hostname: "", + Address: "myworker.example.com", + Active: true, + BuildLimit: 1, + Routes: []string{"large", "small"}, + Output: "", + }, + }, + } + + // run tests + for _, test := range tests { + err := test.config.Validate() + + if test.failure { + if err == nil { + t.Errorf("Validate should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("Validate returned err: %v", err) + } + } +} diff --git a/action/worker/view.go b/action/worker/view.go new file mode 100644 index 00000000..aa2c00c5 --- /dev/null +++ b/action/worker/view.go @@ -0,0 +1,71 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/internal/output" + + "github.com/go-vela/sdk-go/vela" + + "github.com/sirupsen/logrus" +) + +// View inspects a worker based off the provided configuration. +// Based on the configuration, it will either return details of +// a worker or view the registration token for the given worker. +func (c *Config) View(client *vela.Client) error { + logrus.Debug("executing view for worker configuration") + + logrus.Tracef("inspecting worker with hostname %s", c.Hostname) + + var ( + response any + err error + ) + + // handle RegistrationToken flag + if c.RegistrationToken { + response, _, err = client.Admin.Worker.RegisterToken(c.Hostname) + if err != nil { + return fmt.Errorf("unable to retrieve registration token for worker %q: %w", c.Hostname, err) + } + } else { + response, _, err = client.Worker.Get(c.Hostname) + if err != nil { + return fmt.Errorf("unable to retrieve information for worker %q: %w", c.Hostname, err) + } + } + + // handle the output based off the provided configuration + switch c.Output { + case output.DriverDump: + // output the worker in dump format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump + return output.Dump(response) + case output.DriverJSON: + // output the worker in JSON format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON + return output.JSON(response) + case output.DriverSpew: + // output the worker in spew format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew + return output.Spew(response) + case output.DriverYAML: + // output the worker in YAML format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML + return output.YAML(response) + default: + // output the worker in stdout format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout + return output.Stdout(response) + } +} diff --git a/action/worker/view_test.go b/action/worker/view_test.go new file mode 100644 index 00000000..d8df2b03 --- /dev/null +++ b/action/worker/view_test.go @@ -0,0 +1,115 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "net/http/httptest" + "testing" + + "github.com/go-vela/server/mock/server" + + "github.com/go-vela/sdk-go/vela" +) + +func TestWorker_Config_View(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // create a vela client + client, err := vela.NewClient(s.URL, "vela", nil) + if err != nil { + t.Errorf("unable to create client: %v", err) + } + + // setup tests + tests := []struct { + failure bool + config *Config + }{ + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + Output: "yaml", + }, + }, + { + failure: false, + config: &Config{ + Action: "view", + Hostname: "MyWorker", + RegistrationToken: true, + Output: "yaml", + }, + }, + { + failure: true, + config: &Config{ + Action: "view", + Hostname: "0", + Output: "", + }, + }, + { + failure: true, + config: &Config{ + Action: "view", + Hostname: "", + Output: "", + RegistrationToken: true, + }, + }, + } + + // run tests + for _, test := range tests { + err := test.config.View(client) + + if test.failure { + if err == nil { + t.Errorf("View should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("View returned err: %v", err) + } + } +} diff --git a/action/worker/worker.go b/action/worker/worker.go new file mode 100644 index 00000000..f2794efd --- /dev/null +++ b/action/worker/worker.go @@ -0,0 +1,18 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +// Config represents the configuration necessary +// to perform worker related requests with Vela. +type Config struct { + Action string + Address string + Hostname string + Active bool + Routes []string + BuildLimit int64 + RegistrationToken bool + Output string +} diff --git a/cmd/vela-cli/add.go b/cmd/vela-cli/add.go index 4cbd3bad..2b8d609a 100644 --- a/cmd/vela-cli/add.go +++ b/cmd/vela-cli/add.go @@ -8,6 +8,7 @@ import ( "github.com/go-vela/cli/command/deployment" "github.com/go-vela/cli/command/repo" "github.com/go-vela/cli/command/secret" + "github.com/go-vela/cli/command/worker" "github.com/urfave/cli/v2" ) @@ -34,5 +35,10 @@ var addCmds = &cli.Command{ // // https://pkg.go.dev/github.com/go-vela/cli/command/secret?tab=doc#CommandAdd secret.CommandAdd, + + // add the sub command for creating a worker + // + // https://pkg.go.dev/github.com/go-vela/cli/command/worker?tab=doc#CommandAdd + worker.CommandAdd, }, } diff --git a/cmd/vela-cli/get.go b/cmd/vela-cli/get.go index 7094e9b5..97bec67f 100644 --- a/cmd/vela-cli/get.go +++ b/cmd/vela-cli/get.go @@ -14,6 +14,7 @@ import ( "github.com/go-vela/cli/command/secret" "github.com/go-vela/cli/command/service" "github.com/go-vela/cli/command/step" + "github.com/go-vela/cli/command/worker" "github.com/urfave/cli/v2" ) @@ -71,5 +72,10 @@ var getCmds = &cli.Command{ // // https://pkg.go.dev/github.com/go-vela/cli/command/step?tab=doc#CommandGet step.CommandGet, + + // add the sub command for getting a list of workers + // + // https://pkg.go.dev/github.com/go-vela/cli/command/worker?tab=doc#CommandGet + worker.CommandGet, }, } diff --git a/cmd/vela-cli/update.go b/cmd/vela-cli/update.go index 161596f8..d89ed294 100644 --- a/cmd/vela-cli/update.go +++ b/cmd/vela-cli/update.go @@ -8,6 +8,7 @@ import ( "github.com/go-vela/cli/command/config" "github.com/go-vela/cli/command/repo" "github.com/go-vela/cli/command/secret" + "github.com/go-vela/cli/command/worker" "github.com/urfave/cli/v2" ) @@ -35,5 +36,10 @@ var updateCmds = &cli.Command{ // // https://pkg.go.dev/github.com/go-vela/cli/command/secret?tab=doc#CommandUpdate secret.CommandUpdate, + + // add the sub command for modifying a worker + // + // https://pkg.go.dev/github.com/go-vela/cli/command/worker?tab=doc#CommandUpdate + worker.CommandUpdate, }, } diff --git a/cmd/vela-cli/view.go b/cmd/vela-cli/view.go index 51dfac32..b585390e 100644 --- a/cmd/vela-cli/view.go +++ b/cmd/vela-cli/view.go @@ -15,6 +15,7 @@ import ( "github.com/go-vela/cli/command/secret" "github.com/go-vela/cli/command/service" "github.com/go-vela/cli/command/step" + "github.com/go-vela/cli/command/worker" "github.com/urfave/cli/v2" ) @@ -77,5 +78,10 @@ var viewCmds = &cli.Command{ // // https://pkg.go.dev/github.com/go-vela/cli/command/step?tab=doc#CommandView step.CommandView, + + // add the sub command for viewing a worker + // + // https://pkg.go.dev/github.com/go-vela/cli/command/worker?tab=doc#CommandView + worker.CommandView, }, } diff --git a/command/worker/add.go b/command/worker/add.go new file mode 100644 index 00000000..766206d2 --- /dev/null +++ b/command/worker/add.go @@ -0,0 +1,102 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +//nolint:dupl // ignore similar code with update +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/action" + "github.com/go-vela/cli/action/worker" + "github.com/go-vela/cli/internal" + "github.com/go-vela/cli/internal/client" + + "github.com/urfave/cli/v2" +) + +// CommandAdd defines the command for adding a worker. +var CommandAdd = &cli.Command{ + Name: "worker", + Description: "(Platform Admin Only) Use this command to add a worker.", + Usage: "Add a new worker from the provided configuration", + Action: add, + Flags: []cli.Flag{ + + // Worker Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_ADDRESS", "WORKER_ADDRESS"}, + Name: internal.FlagWorkerAddress, + Aliases: []string{"wa"}, + Usage: "provide the address of the worker", + }, + + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_HOSTNAME", "WORKER_HOSTNAME"}, + Name: internal.FlagWorkerHostname, + Aliases: []string{"wh"}, + Usage: "provide the hostname of the worker", + }, + + // Output Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_OUTPUT", "WORKER_OUTPUT"}, + Name: internal.FlagOutput, + Aliases: []string{"op"}, + Usage: "format the output in json, spew or yaml", + }, + }, + CustomHelpTemplate: fmt.Sprintf(`%s +EXAMPLES: + 1. Add a worker reachable at the provided address. + $ {{.HelpName}} --worker.hostname MyWorker --worker.address myworker.example.com + +DOCUMENTATION: + + https://go-vela.github.io/docs/reference/cli/worker/add/ +`, cli.CommandHelpTemplate), +} + +// helper function to capture the provided input +// and create the object used to create a worker. +func add(c *cli.Context) error { + // load variables from the config file + err := action.Load(c) + if err != nil { + return err + } + + // parse the Vela client from the context + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse + client, err := client.Parse(c) + if err != nil { + return err + } + + // create the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config + w := &worker.Config{ + Action: internal.ActionAdd, + Address: c.String(internal.FlagWorkerAddress), + Hostname: c.String(internal.FlagWorkerHostname), + Output: c.String(internal.FlagOutput), + } + + // validate worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Validate + err = w.Validate() + if err != nil { + return err + } + + // execute the add call for the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Add + return w.Add(client) +} diff --git a/command/worker/add_test.go b/command/worker/add_test.go new file mode 100644 index 00000000..e11f4be7 --- /dev/null +++ b/command/worker/add_test.go @@ -0,0 +1,75 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "flag" + "net/http/httptest" + "testing" + + "github.com/go-vela/cli/test" + "github.com/go-vela/server/mock/server" + "github.com/go-vela/worker/mock/worker" + + "github.com/urfave/cli/v2" +) + +func TestWorker_Add(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // create mock worker server + w := httptest.NewServer(worker.FakeHandler()) + + // setup flags + authSet := flag.NewFlagSet("test", 0) + authSet.String("api.addr", s.URL, "doc") + authSet.String("api.token.access", test.TestTokenGood, "doc") + authSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + + fullSet := flag.NewFlagSet("test", 0) + fullSet.String("api.addr", s.URL, "doc") + fullSet.String("api.token.access", test.TestTokenGood, "doc") + fullSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + fullSet.String("worker.hostname", "MyWorker", "doc") + fullSet.String("worker.address", w.URL, "doc") + fullSet.String("output", "json", "doc") + + // setup tests + tests := []struct { + failure bool + set *flag.FlagSet + }{ + { + failure: false, + set: fullSet, + }, + { + failure: true, + set: authSet, + }, + { + failure: true, + set: flag.NewFlagSet("test", 0), + }, + } + + // run tests + for _, test := range tests { + err := add(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil)) + + if test.failure { + if err == nil { + t.Errorf("add should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("add returned err: %v", err) + } + } +} diff --git a/command/worker/doc.go b/command/worker/doc.go new file mode 100644 index 00000000..65a53a28 --- /dev/null +++ b/command/worker/doc.go @@ -0,0 +1,10 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// Package worker provides the defined worker CLI command for Vela. +// +// Usage: +// +// import "github.com/go-vela/cli/command/worker" +package worker diff --git a/command/worker/get.go b/command/worker/get.go new file mode 100644 index 00000000..d9126cc7 --- /dev/null +++ b/command/worker/get.go @@ -0,0 +1,93 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/action" + "github.com/go-vela/cli/action/worker" + "github.com/go-vela/cli/internal" + "github.com/go-vela/cli/internal/client" + + "github.com/urfave/cli/v2" +) + +// CommandGet defines the command for capturing a list of workers. +var CommandGet = &cli.Command{ + Name: "worker", + Aliases: []string{"workers"}, + Description: "Use this command to get a list of workers.", + Usage: "Display a list of workers", + Action: get, + Flags: []cli.Flag{ + + // Output Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_OUTPUT", "WORKER_OUTPUT"}, + Name: internal.FlagOutput, + Aliases: []string{"op"}, + Usage: "format the output in wide, json, spew or yaml", + }, + }, + CustomHelpTemplate: fmt.Sprintf(`%s +EXAMPLES: + 1. Get a list of workers. + $ {{.HelpName}} + 2. Get a list of workers with wide view output. + $ {{.HelpName}} --output wide + 3. Get a list of workers with yaml output. + $ {{.HelpName}} --output yaml + 4. Get a list of workers with json output. + $ {{.HelpName}} --output json + 5. Get a list of workers when config or environment variables are set. + $ {{.HelpName}} + +DOCUMENTATION: + + https://go-vela.github.io/docs/reference/cli/worker/get/ +`, cli.CommandHelpTemplate), +} + +// helper function to capture the provided input +// and create the object used to capture a list +// of workers. +func get(c *cli.Context) error { + // load variables from the config file + err := action.Load(c) + if err != nil { + return err + } + + // parse the Vela client from the context + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse + client, err := client.Parse(c) + if err != nil { + return err + } + + // create the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config + w := &worker.Config{ + Action: internal.ActionGet, + Output: c.String(internal.FlagOutput), + } + + // validate worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Validate + err = w.Validate() + if err != nil { + return err + } + + // execute the get call for the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Get + return w.Get(client) +} diff --git a/command/worker/get_test.go b/command/worker/get_test.go new file mode 100644 index 00000000..10620fb4 --- /dev/null +++ b/command/worker/get_test.go @@ -0,0 +1,60 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "flag" + "net/http/httptest" + "testing" + + "github.com/go-vela/cli/test" + "github.com/go-vela/server/mock/server" + + "github.com/urfave/cli/v2" +) + +func TestWorker_Get(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // setup flags + fullSet := flag.NewFlagSet("test", 0) + fullSet.String("api.addr", s.URL, "doc") + fullSet.String("api.token.access", test.TestTokenGood, "doc") + fullSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + fullSet.String("output", "json", "doc") + + // setup tests + tests := []struct { + failure bool + set *flag.FlagSet + }{ + { + failure: false, + set: fullSet, + }, + { + failure: true, + set: flag.NewFlagSet("test", 0), + }, + } + + // run tests + for _, test := range tests { + err := get(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil)) + + if test.failure { + if err == nil { + t.Errorf("get should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("get returned err: %v", err) + } + } +} diff --git a/command/worker/update.go b/command/worker/update.go new file mode 100644 index 00000000..6ab70c9d --- /dev/null +++ b/command/worker/update.go @@ -0,0 +1,119 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/action" + "github.com/go-vela/cli/action/worker" + "github.com/go-vela/cli/internal" + "github.com/go-vela/cli/internal/client" + + "github.com/urfave/cli/v2" +) + +// CommandUpdate defines the command for modifying a worker. +var CommandUpdate = &cli.Command{ + Name: "worker", + Description: "(Platform Admin Only) Use this command to update a worker.", + Usage: "Update a worker from the provided configuration", + Action: update, + Flags: []cli.Flag{ + + // Worker Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_ACTIVE", "WORKER_ACTIVE"}, + Name: "active", + Aliases: []string{"a"}, + Usage: "current status of the worker", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_ADDRESS", "WORKER_ADDRESS"}, + Name: internal.FlagWorkerAddress, + Aliases: []string{"wa"}, + Usage: "provide the address of the worker", + }, + &cli.IntFlag{ + EnvVars: []string{"VELA_WORKER_BUILD_LIMIT", "WORKER_BUILD_LIMIT"}, + Name: "build-limit", + Aliases: []string{"bl"}, + Usage: "build limit for the worker", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_HOSTNAME", "WORKER_HOSTNAME"}, + Name: internal.FlagWorkerHostname, + Aliases: []string{"wh"}, + Usage: "provide the hostname of the worker", + }, + &cli.StringSliceFlag{ + EnvVars: []string{"VELA_WORKER_ROUTES", "WORKER_ROUTES"}, + Name: "routes", + Aliases: []string{"r"}, + Usage: "route assignment for the worker", + }, + + // Output Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_OUTPUT", "WORKER_OUTPUT"}, + Name: internal.FlagOutput, + Aliases: []string{"op"}, + Usage: "format the output in json, spew or yaml", + }, + }, + CustomHelpTemplate: fmt.Sprintf(`%s +EXAMPLES: + 1. Update a worker to change its build limit to 2. + $ {{.HelpName}} --worker.hostname MyWorker --build-limit 2 + 2. Update a worker with custom routes. + $ {{.HelpName}} --worker.hostname MyWorker --route large --route small + +DOCUMENTATION: + + https://go-vela.github.io/docs/reference/cli/worker/update/ +`, cli.CommandHelpTemplate), +} + +// helper function to capture the provided input +// and create the object used to modify a worker. +func update(c *cli.Context) error { + // load variables from the config file + err := action.Load(c) + if err != nil { + return err + } + + // parse the Vela client from the context + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse + client, err := client.Parse(c) + if err != nil { + return err + } + + // create the worker configuration + w := &worker.Config{ + Hostname: c.String(internal.FlagWorkerHostname), + Address: c.String(internal.FlagWorkerAddress), + Active: c.Bool("active"), + Routes: c.StringSlice("routes"), + BuildLimit: c.Int64("build-limit"), + } + + // validate worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Validate + err = w.Validate() + if err != nil { + return err + } + + // execute the update call for the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Update + return w.Update(client) +} diff --git a/command/worker/update_test.go b/command/worker/update_test.go new file mode 100644 index 00000000..97629d0f --- /dev/null +++ b/command/worker/update_test.go @@ -0,0 +1,70 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "flag" + "net/http/httptest" + "testing" + + "github.com/go-vela/cli/test" + "github.com/go-vela/server/mock/server" + + "github.com/urfave/cli/v2" +) + +func TestWorker_Update(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // setup flags + authSet := flag.NewFlagSet("test", 0) + authSet.String("api.addr", s.URL, "doc") + authSet.String("api.token.access", test.TestTokenGood, "doc") + authSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + + fullSet := flag.NewFlagSet("test", 0) + fullSet.String("api.addr", s.URL, "doc") + fullSet.String("api.token.access", test.TestTokenGood, "doc") + fullSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + fullSet.String("worker.hostname", "MyWorker", "doc") + fullSet.String("output", "json", "doc") + + // setup tests + tests := []struct { + failure bool + set *flag.FlagSet + }{ + { + failure: false, + set: fullSet, + }, + { + failure: true, + set: authSet, + }, + { + failure: true, + set: flag.NewFlagSet("test", 0), + }, + } + + // run tests + for _, test := range tests { + err := update(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil)) + + if test.failure { + if err == nil { + t.Errorf("update should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("update returned err: %v", err) + } + } +} diff --git a/command/worker/view.go b/command/worker/view.go new file mode 100644 index 00000000..b63ceeba --- /dev/null +++ b/command/worker/view.go @@ -0,0 +1,110 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "fmt" + + "github.com/go-vela/cli/action" + "github.com/go-vela/cli/action/worker" + "github.com/go-vela/cli/internal" + "github.com/go-vela/cli/internal/client" + + "github.com/urfave/cli/v2" +) + +// CommandView defines the command for inspecting a worker. +var CommandView = &cli.Command{ + Name: "worker", + Description: "Use this command to view a worker.", + Usage: "View details of the provided worker", + Action: view, + Flags: []cli.Flag{ + + // Worker Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_HOSTNAME", "WORKER_HOSTNAME"}, + Name: internal.FlagWorkerHostname, + Aliases: []string{"wh"}, + Usage: "provide the hostname of the worker", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_WORKER_REGISTRATION_TOKEN", "WORKER_REGISTRATION_TOKEN"}, + Name: internal.FlagWorkerRegistrationToken, + Aliases: []string{"wr"}, + Usage: "toggle to show the registration token for the worker", + Value: "false", + }, + + // Output Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_OUTPUT", "WORKER_OUTPUT"}, + Name: internal.FlagOutput, + Aliases: []string{"op"}, + Usage: "format the output in json, spew or yaml", + Value: "yaml", + }, + }, + CustomHelpTemplate: fmt.Sprintf(`%s +EXAMPLES: + 1. View details of a worker. + $ {{.HelpName}} --worker.hostname MyWorker + 2. View registration token for a worker. + $ {{.HelpName}} --worker.hostname MyWorker --worker.registration.token true + 3. View details of a worker with json output. + $ {{.HelpName}} --worker.hostname MyWorker --output json + 4. View details of a worker when config or environment variables are set. + $ {{.HelpName}} + +DOCUMENTATION: + + https://go-vela.github.io/docs/reference/cli/worker/view/ +`, cli.CommandHelpTemplate), +} + +// helper function to capture the provided input +// and create the object used to inspect a worker. +// +//nolint:dupl // ignore similar code with chown, get, remove and repair +func view(c *cli.Context) error { + // load variables from the config file + err := action.Load(c) + if err != nil { + return err + } + + // parse the Vela client from the context + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse + client, err := client.Parse(c) + if err != nil { + return err + } + + // create the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config + w := &worker.Config{ + Action: internal.ActionView, + Hostname: c.String(internal.FlagWorkerHostname), + RegistrationToken: c.Bool(internal.FlagWorkerRegistrationToken), + Output: c.String(internal.FlagOutput), + } + + // validate worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Validate + err = w.Validate() + if err != nil { + return err + } + + // execute the view call for the worker configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.View + return w.View(client) +} diff --git a/command/worker/view_test.go b/command/worker/view_test.go new file mode 100644 index 00000000..198a6f76 --- /dev/null +++ b/command/worker/view_test.go @@ -0,0 +1,80 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "flag" + "net/http/httptest" + "testing" + + "github.com/go-vela/cli/test" + "github.com/go-vela/server/mock/server" + + "github.com/urfave/cli/v2" +) + +func TestWorker_View(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // setup flags + authSet := flag.NewFlagSet("test", 0) + authSet.String("api.addr", s.URL, "doc") + authSet.String("api.token.access", test.TestTokenGood, "doc") + authSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + + fullSet := flag.NewFlagSet("test", 0) + fullSet.String("api.addr", s.URL, "doc") + fullSet.String("api.token.access", test.TestTokenGood, "doc") + fullSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + fullSet.String("worker.hostname", "MyWorker", "doc") + fullSet.String("output", "json", "doc") + + noHostSet := flag.NewFlagSet("test", 0) + noHostSet.String("api.addr", s.URL, "doc") + noHostSet.String("api.token.access", test.TestTokenGood, "doc") + noHostSet.String("api.token.refresh", "superSecretRefreshToken", "doc") + noHostSet.String("output", "json", "doc") + + // setup tests + tests := []struct { + failure bool + set *flag.FlagSet + }{ + { + failure: false, + set: fullSet, + }, + { + failure: true, + set: authSet, + }, + { + failure: true, + set: noHostSet, + }, + { + failure: true, + set: flag.NewFlagSet("test", 0), + }, + } + + // run tests + for _, test := range tests { + err := view(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil)) + + if test.failure { + if err == nil { + t.Errorf("view should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("view returned err: %v", err) + } + } +} diff --git a/go.mod b/go.mod index 5fd3fff3..41976cb7 100644 --- a/go.mod +++ b/go.mod @@ -8,9 +8,10 @@ require ( github.com/cli/browser v1.1.0 github.com/davecgh/go-spew v1.1.1 github.com/dustin/go-humanize v1.0.1 + github.com/gin-gonic/gin v1.9.0 github.com/go-git/go-git/v5 v5.5.2 github.com/go-vela/sdk-go v0.18.2-0.20230407134447-c34cd778f44a - github.com/go-vela/server v0.18.2-0.20230405140822-34164d0412e2 + github.com/go-vela/server v0.18.2-0.20230407143935-3335f6a0134a github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c github.com/go-vela/worker v0.18.2-0.20230406165141-c76c2e460786 github.com/golang-jwt/jwt/v4 v4.5.0 @@ -49,7 +50,6 @@ require ( github.com/fatih/color v1.10.0 // indirect github.com/ghodss/yaml v1.0.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.9.0 // indirect github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/go-billy/v5 v5.4.0 // indirect github.com/go-logr/logr v1.2.3 // indirect diff --git a/go.sum b/go.sum index 98bc85d1..3fbd46ba 100644 --- a/go.sum +++ b/go.sum @@ -159,8 +159,8 @@ github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyh github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= github.com/go-vela/sdk-go v0.18.2-0.20230407134447-c34cd778f44a h1:OCOrVvYm0QzOFyUy095aWlOjAzM7WBlhepqpxsOLKjc= github.com/go-vela/sdk-go v0.18.2-0.20230407134447-c34cd778f44a/go.mod h1:fBWXDlvDaGhKgV5/gzTLR5MR6+4Um0RG25tlu/xOQHo= -github.com/go-vela/server v0.18.2-0.20230405140822-34164d0412e2 h1:KsxVnbFhpe8QcuqfA6/4UTDIMiKUkCCS3t3Egn52lns= -github.com/go-vela/server v0.18.2-0.20230405140822-34164d0412e2/go.mod h1:b+7XeGHO4ynIinY9mpWb6ye9psdwHpsAqMWy5oC+zJ0= +github.com/go-vela/server v0.18.2-0.20230407143935-3335f6a0134a h1:PNED4EzAg3i6NbVpPHP/hNshPR0LNlzVmrCDx01L9fI= +github.com/go-vela/server v0.18.2-0.20230407143935-3335f6a0134a/go.mod h1:b+7XeGHO4ynIinY9mpWb6ye9psdwHpsAqMWy5oC+zJ0= github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c h1:lnCL1knUGvgZQG4YBHSs/CZnxNBfqFUBlGhyq9LO9uk= github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c/go.mod h1:6MzMhLaXKSZ9wiJveieqnBd2+4ZMS7yv7+POGSITyS8= github.com/go-vela/worker v0.18.2-0.20230406165141-c76c2e460786 h1:cE3B/pCDJzr8CsFd+c1nGVviB+9VmW9nkZn69IrMlj0= diff --git a/internal/internal.go b/internal/internal.go index 97a47a1e..ce1715d5 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -136,6 +136,21 @@ const ( FlagStep = "step" ) +// worker flag keys. +const ( + // FlagWorkerAddress defines the key for the + // flag when setting the worker address. + FlagWorkerAddress = "worker.address" + + // FlagWorkerHostname defines the key for the + // flag when setting the worker hostname. + FlagWorkerHostname = "worker.hostname" + + // FlagWorkerRegistrationToken defines the key for the + // flag when viewing the worker registration token. + FlagWorkerRegistrationToken = "worker.registration.token" +) + // list of defined CLI actions. const ( // ActionAdd defines the action for creating a resource.