From d1134b415eeb9b3b3081fb149184120154bd7c2f Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Tue, 28 Mar 2023 11:55:49 -0500 Subject: [PATCH 01/18] feat: add worker management (wip) --- action/worker/add.go | 78 +++++++++++++++++++++ action/worker/add_test.go | 94 +++++++++++++++++++++++++ action/worker/doc.go | 10 +++ action/worker/get.go | 66 ++++++++++++++++++ action/worker/get_test.go | 103 ++++++++++++++++++++++++++++ action/worker/table.go | 109 +++++++++++++++++++++++++++++ action/worker/table_test.go | 114 +++++++++++++++++++++++++++++++ action/worker/update.go | 71 +++++++++++++++++++ action/worker/update_test.go | 121 +++++++++++++++++++++++++++++++++ action/worker/validate.go | 29 ++++++++ action/worker/validate_test.go | 100 +++++++++++++++++++++++++++ action/worker/view.go | 70 +++++++++++++++++++ action/worker/view_test.go | 106 +++++++++++++++++++++++++++++ action/worker/worker.go | 20 ++++++ cmd/vela-cli/add.go | 6 ++ cmd/vela-cli/get.go | 6 ++ cmd/vela-cli/update.go | 6 ++ cmd/vela-cli/view.go | 6 ++ command/worker/add.go | 102 +++++++++++++++++++++++++++ command/worker/add_test.go | 71 +++++++++++++++++++ command/worker/doc.go | 10 +++ command/worker/get.go | 114 +++++++++++++++++++++++++++++++ command/worker/get_test.go | 62 +++++++++++++++++ command/worker/update.go | 119 ++++++++++++++++++++++++++++++++ command/worker/update_test.go | 70 +++++++++++++++++++ command/worker/view.go | 110 ++++++++++++++++++++++++++++++ command/worker/view_test.go | 70 +++++++++++++++++++ internal/internal.go | 15 ++++ 28 files changed, 1858 insertions(+) create mode 100644 action/worker/add.go create mode 100644 action/worker/add_test.go create mode 100644 action/worker/doc.go create mode 100644 action/worker/get.go create mode 100644 action/worker/get_test.go create mode 100644 action/worker/table.go create mode 100644 action/worker/table_test.go create mode 100644 action/worker/update.go create mode 100644 action/worker/update_test.go create mode 100644 action/worker/validate.go create mode 100644 action/worker/validate_test.go create mode 100644 action/worker/view.go create mode 100644 action/worker/view_test.go create mode 100644 action/worker/worker.go create mode 100644 command/worker/add.go create mode 100644 command/worker/add_test.go create mode 100644 command/worker/doc.go create mode 100644 command/worker/get.go create mode 100644 command/worker/get_test.go create mode 100644 command/worker/update.go create mode 100644 command/worker/update_test.go create mode 100644 command/worker/view.go create mode 100644 command/worker/view_test.go diff --git a/action/worker/add.go b/action/worker/add.go new file mode 100644 index 00000000..5a5ec5a5 --- /dev/null +++ b/action/worker/add.go @@ -0,0 +1,78 @@ +// 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/go-vela/types/library" + + "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") + + // 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), + } + + registerToken, _, err := client.Admin.Worker.RegisterToken(c.Hostname) + if err != nil || registerToken == nil { + return fmt.Errorf("unable to retrieve registration token: %w", err) + } + + // set the registration token as the authentication + // for the next request to add a worker + client.Authentication.SetTokenAuth(*registerToken.Token) + + logrus.Tracef("adding worker %s", c.Hostname) + + // send API call to add a worker + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#WorkerService.Add + worker, _, err := client.Worker.Add(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/add_test.go b/action/worker/add_test.go new file mode 100644 index 00000000..0782d394 --- /dev/null +++ b/action/worker/add_test.go @@ -0,0 +1,94 @@ +// 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_Add(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: "add", + Hostname: "MyWorker", + Address: "myworker.example.com", + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "myworker.example.com", + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "myworker.example.com", + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "myworker.example.com", + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "myworker.example.com", + 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..b180ebcd --- /dev/null +++ b/action/worker/get.go @@ -0,0 +1,66 @@ +// 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") + + // set the pagination options for list of workers + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#ListOptions + opts := &vela.ListOptions{ + Page: c.Page, + PerPage: c.PerPage, + } + + 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(opts) + 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..16698780 --- /dev/null +++ b/action/worker/get_test.go @@ -0,0 +1,103 @@ +// 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", + Page: 1, + PerPage: 10, + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Page: 1, + PerPage: 10, + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Page: 1, + PerPage: 10, + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Page: 1, + PerPage: 10, + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Page: 1, + PerPage: 10, + Output: "wide", + }, + }, + { + failure: false, + config: &Config{ + Action: "get", + Page: 1, + PerPage: 10, + Output: "yaml", + }, + }, + } + + // 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..4c9aaa7b --- /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", "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.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..eeea09c3 --- /dev/null +++ b/action/worker/update.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. + +//nolint:dupl // ignore similar code among actions +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..234a112c --- /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: "MyWorker", + 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..25c1d2dc --- /dev/null +++ b/action/worker/validate_test.go @@ -0,0 +1,100 @@ +// 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: 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..5343db6c --- /dev/null +++ b/action/worker/view.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 ( + "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..e317bbd5 --- /dev/null +++ b/action/worker/view_test.go @@ -0,0 +1,106 @@ +// 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: "", + Output: "", + }, + }, + } + + // 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..225a7068 --- /dev/null +++ b/action/worker/worker.go @@ -0,0 +1,20 @@ +// 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 + Page int + PerPage int + 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..8a31e18c 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 worker + // + // 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..f137f473 --- /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: "(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 with the provided hostname. + $ {{.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..7cf62a3c --- /dev/null +++ b/command/worker/add_test.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 ( + "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_Add(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("worker.address", "myworker.example.com", "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..0b8a1a58 --- /dev/null +++ b/command/worker/get.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 ( + "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", + Description: "(Admin Only) Use this command to get a list of workers.", + Usage: "Display a list 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 json, spew or yaml", + Value: "yaml", + }, + + // Pagination Flags + + &cli.IntFlag{ + EnvVars: []string{"VELA_PAGE", "WORKER_PAGE"}, + Name: internal.FlagPage, + Aliases: []string{"p"}, + Usage: "print a specific page of workers", + Value: 1, + }, + &cli.IntFlag{ + EnvVars: []string{"VELA_PER_PAGE", "WORKER_PER_PAGE"}, + Name: internal.FlagPerPage, + Aliases: []string{"pp"}, + Usage: "number of workers to print per page", + Value: 10, + }, + }, + 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. +// +//nolint:dupl // ignore similar code with chown, get, remove and repair +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.ActionView, + Page: c.Int(internal.FlagPage), + PerPage: c.Int(internal.FlagPerPage), + 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.Get(client) +} diff --git a/command/worker/get_test.go b/command/worker/get_test.go new file mode 100644 index 00000000..9d6a5fd0 --- /dev/null +++ b/command/worker/get_test.go @@ -0,0 +1,62 @@ +// 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.Int("page", 1, "doc") + fullSet.Int("per.page", 10, "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..23ea735e --- /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: "(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..71c9f6bb --- /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: "(Admin Only) 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 + 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..e9e5fd8e --- /dev/null +++ b/command/worker/view_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_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") + + // 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 := 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/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. From b7203a7a4507555fcf9694848adb698016ff10cf Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Tue, 28 Mar 2023 11:56:16 -0500 Subject: [PATCH 02/18] fix: function comment --- action/repo/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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") From 13e167abdc9a824775f8975231346065336b5900 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Wed, 29 Mar 2023 16:00:56 -0500 Subject: [PATCH 03/18] tweaks --- action/worker/add_test.go | 1 + action/worker/get.go | 10 +--------- action/worker/get_test.go | 37 +++++++++++++----------------------- action/worker/update_test.go | 2 +- action/worker/view_test.go | 2 +- action/worker/worker.go | 2 -- command/worker/get.go | 23 ++-------------------- command/worker/get_test.go | 2 -- 8 files changed, 19 insertions(+), 60 deletions(-) diff --git a/action/worker/add_test.go b/action/worker/add_test.go index 0782d394..06c60fd1 100644 --- a/action/worker/add_test.go +++ b/action/worker/add_test.go @@ -73,6 +73,7 @@ func TestWorker_Config_Add(t *testing.T) { Output: "yaml", }, }, + // TODO: mock doesn't have failure for worker creation } // run tests diff --git a/action/worker/get.go b/action/worker/get.go index b180ebcd..a4ce55a2 100644 --- a/action/worker/get.go +++ b/action/worker/get.go @@ -16,20 +16,12 @@ import ( func (c *Config) Get(client *vela.Client) error { logrus.Debug("executing get for worker configuration") - // set the pagination options for list of workers - // - // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#ListOptions - opts := &vela.ListOptions{ - Page: c.Page, - PerPage: c.PerPage, - } - 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(opts) + workers, _, err := client.Worker.GetAll() if err != nil { return err } diff --git a/action/worker/get_test.go b/action/worker/get_test.go index 16698780..8c33104d 100644 --- a/action/worker/get_test.go +++ b/action/worker/get_test.go @@ -31,57 +31,46 @@ func TestWorker_Config_Get(t *testing.T) { { failure: false, config: &Config{ - Action: "get", - Page: 1, - PerPage: 10, - Output: "", + Action: "get", + Output: "", }, }, { failure: false, config: &Config{ - Action: "get", - Page: 1, - PerPage: 10, - Output: "dump", + Action: "get", + Output: "dump", }, }, { failure: false, config: &Config{ - Action: "get", - Page: 1, - PerPage: 10, - Output: "json", + Action: "get", + Output: "json", }, }, { failure: false, config: &Config{ - Action: "get", - Page: 1, - PerPage: 10, - Output: "spew", + Action: "get", + Output: "spew", }, }, { failure: false, config: &Config{ - Action: "get", - Page: 1, - PerPage: 10, - Output: "wide", + Action: "get", + Output: "wide", }, }, { failure: false, config: &Config{ - Action: "get", - Page: 1, - PerPage: 10, - Output: "yaml", + Action: "get", + Output: "yaml", }, }, + // TODO: mock doesn't have failure for listing workers } // run tests diff --git a/action/worker/update_test.go b/action/worker/update_test.go index 234a112c..cf7cd672 100644 --- a/action/worker/update_test.go +++ b/action/worker/update_test.go @@ -92,7 +92,7 @@ func TestWorker_Config_Update(t *testing.T) { failure: true, config: &Config{ Action: "update", - Hostname: "MyWorker", + Hostname: "0", Address: "myworker.example.com", Routes: []string{"large", "small"}, BuildLimit: 1, diff --git a/action/worker/view_test.go b/action/worker/view_test.go index e317bbd5..dda0ddc2 100644 --- a/action/worker/view_test.go +++ b/action/worker/view_test.go @@ -81,7 +81,7 @@ func TestWorker_Config_View(t *testing.T) { failure: true, config: &Config{ Action: "view", - Hostname: "", + Hostname: "0", Output: "", }, }, diff --git a/action/worker/worker.go b/action/worker/worker.go index 225a7068..f2794efd 100644 --- a/action/worker/worker.go +++ b/action/worker/worker.go @@ -14,7 +14,5 @@ type Config struct { Routes []string BuildLimit int64 RegistrationToken bool - Page int - PerPage int Output string } diff --git a/command/worker/get.go b/command/worker/get.go index 0b8a1a58..fd2e8d2a 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -32,23 +32,6 @@ var CommandGet = &cli.Command{ Usage: "format the output in json, spew or yaml", Value: "yaml", }, - - // Pagination Flags - - &cli.IntFlag{ - EnvVars: []string{"VELA_PAGE", "WORKER_PAGE"}, - Name: internal.FlagPage, - Aliases: []string{"p"}, - Usage: "print a specific page of workers", - Value: 1, - }, - &cli.IntFlag{ - EnvVars: []string{"VELA_PER_PAGE", "WORKER_PER_PAGE"}, - Name: internal.FlagPerPage, - Aliases: []string{"pp"}, - Usage: "number of workers to print per page", - Value: 10, - }, }, CustomHelpTemplate: fmt.Sprintf(`%s EXAMPLES: @@ -93,10 +76,8 @@ func get(c *cli.Context) error { // // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config w := &worker.Config{ - Action: internal.ActionView, - Page: c.Int(internal.FlagPage), - PerPage: c.Int(internal.FlagPerPage), - Output: c.String(internal.FlagOutput), + Action: internal.ActionGet, + Output: c.String(internal.FlagOutput), } // validate worker configuration diff --git a/command/worker/get_test.go b/command/worker/get_test.go index 9d6a5fd0..10620fb4 100644 --- a/command/worker/get_test.go +++ b/command/worker/get_test.go @@ -24,8 +24,6 @@ func TestWorker_Get(t *testing.T) { fullSet.String("api.addr", s.URL, "doc") fullSet.String("api.token.access", test.TestTokenGood, "doc") fullSet.String("api.token.refresh", "superSecretRefreshToken", "doc") - fullSet.Int("page", 1, "doc") - fullSet.Int("per.page", 10, "doc") fullSet.String("output", "json", "doc") // setup tests From 0b38cdf556c075ada6b133b39115edab4de4fd7b Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Wed, 5 Apr 2023 13:49:49 -0500 Subject: [PATCH 04/18] fixes --- action/worker/add.go | 37 ++++++++++++++----------------------- action/worker/add_test.go | 24 +++++++++++++++++++----- action/worker/table.go | 4 ++-- command/worker/add.go | 10 +++++++--- command/worker/get.go | 1 - go.mod | 12 +++++++----- go.sum | 18 ++++++++---------- 7 files changed, 57 insertions(+), 49 deletions(-) diff --git a/action/worker/add.go b/action/worker/add.go index 5a5ec5a5..b624c2b3 100644 --- a/action/worker/add.go +++ b/action/worker/add.go @@ -11,8 +11,6 @@ import ( "github.com/go-vela/sdk-go/vela" - "github.com/go-vela/types/library" - "github.com/sirupsen/logrus" ) @@ -20,59 +18,52 @@ import ( func (c *Config) Add(client *vela.Client) error { logrus.Debug("executing add for worker configuration") - // create the worker object + // send API call to get a registration token for the given worker // - // 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), - } - + // 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) } - // set the registration token as the authentication - // for the next request to add a worker - client.Authentication.SetTokenAuth(*registerToken.Token) + logrus.Tracef("got registration token, adding worker %q", c.Hostname) - logrus.Tracef("adding worker %s", c.Hostname) - - // send API call to add a worker + // send API call to register a worker // - // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#WorkerService.Add - worker, _, err := client.Worker.Add(w) + // 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 err + 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(worker) + 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(worker) + 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(worker) + 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(worker) + 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(worker) + return output.Stdout(*msg) } } diff --git a/action/worker/add_test.go b/action/worker/add_test.go index 06c60fd1..1b6e1746 100644 --- a/action/worker/add_test.go +++ b/action/worker/add_test.go @@ -5,15 +5,20 @@ package worker import ( + "net/http" "net/http/httptest" "testing" + "github.com/gin-gonic/gin" "github.com/go-vela/server/mock/server" "github.com/go-vela/sdk-go/vela" ) func TestWorker_Config_Add(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + // setup test server s := httptest.NewServer(server.FakeHandler()) @@ -23,6 +28,15 @@ func TestWorker_Config_Add(t *testing.T) { t.Errorf("unable to create client: %v", err) } + // set up new gin instance for fake worker + e := gin.New() + + // mock endpoint for worker register call + e.GET("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) + + // create a new test server + w := httptest.NewServer(e) + // setup tests tests := []struct { failure bool @@ -33,7 +47,7 @@ func TestWorker_Config_Add(t *testing.T) { config: &Config{ Action: "add", Hostname: "MyWorker", - Address: "myworker.example.com", + Address: w.URL, Output: "", }, }, @@ -42,7 +56,7 @@ func TestWorker_Config_Add(t *testing.T) { config: &Config{ Action: "add", Hostname: "MyWorker", - Address: "myworker.example.com", + Address: w.URL, Output: "dump", }, }, @@ -51,7 +65,7 @@ func TestWorker_Config_Add(t *testing.T) { config: &Config{ Action: "add", Hostname: "MyWorker", - Address: "myworker.example.com", + Address: w.URL, Output: "json", }, }, @@ -60,7 +74,7 @@ func TestWorker_Config_Add(t *testing.T) { config: &Config{ Action: "add", Hostname: "MyWorker", - Address: "myworker.example.com", + Address: w.URL, Output: "spew", }, }, @@ -69,7 +83,7 @@ func TestWorker_Config_Add(t *testing.T) { config: &Config{ Action: "add", Hostname: "MyWorker", - Address: "myworker.example.com", + Address: w.URL, Output: "yaml", }, }, diff --git a/action/worker/table.go b/action/worker/table.go index 4c9aaa7b..f40d49b1 100644 --- a/action/worker/table.go +++ b/action/worker/table.go @@ -85,7 +85,7 @@ func wideTable(workers *[]library.Worker) error { // 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", "ACTIVE", "ROUTES", "LAST_CHECKED_IN", "BUILD_LIMIT") + table.AddRow("HOSTNAME", "ADDRESS", "ACTIVE", "ROUTES", "LAST_CHECKED_IN", "BUILD_LIMIT") // iterate through all workers in the list for _, w := range *workers { @@ -99,7 +99,7 @@ func wideTable(workers *[]library.Worker) error { // 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(), c, w.GetBuildLimit()) + table.AddRow(w.GetHostname(), w.GetAddress(), w.GetActive(), w.GetRoutes(), c, w.GetBuildLimit()) } // output the wide table in stdout format diff --git a/command/worker/add.go b/command/worker/add.go index f137f473..45436d15 100644 --- a/command/worker/add.go +++ b/command/worker/add.go @@ -13,6 +13,7 @@ import ( "github.com/go-vela/cli/internal" "github.com/go-vela/cli/internal/client" + "github.com/google/uuid" "github.com/urfave/cli/v2" ) @@ -37,7 +38,10 @@ var CommandAdd = &cli.Command{ EnvVars: []string{"VELA_WORKER_HOSTNAME", "WORKER_HOSTNAME"}, Name: internal.FlagWorkerHostname, Aliases: []string{"wh"}, - Usage: "provide the hostname of the worker", + Usage: "provide the hostname of the worker (defaults is random ID)", + // there is no current enforcement on passing a valid hostname + // if none is supplied, use a random id. + Value: uuid.NewString(), }, // Output Flags @@ -51,8 +55,8 @@ var CommandAdd = &cli.Command{ }, CustomHelpTemplate: fmt.Sprintf(`%s EXAMPLES: - 1. Add a worker with the provided hostname. - $ {{.HelpName}} --worker.hostname MyWorker --worker.address myworker.example.com + 1. Add a worker reachable at the give address. + $ {{.HelpName}} --worker.address myworker.example.com DOCUMENTATION: diff --git a/command/worker/get.go b/command/worker/get.go index fd2e8d2a..08f8630d 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -30,7 +30,6 @@ var CommandGet = &cli.Command{ Name: internal.FlagOutput, Aliases: []string{"op"}, Usage: "format the output in json, spew or yaml", - Value: "yaml", }, }, CustomHelpTemplate: fmt.Sprintf(`%s diff --git a/go.mod b/go.mod index cc314c68..6f8adbf0 100644 --- a/go.mod +++ b/go.mod @@ -2,17 +2,19 @@ module github.com/go-vela/cli go 1.19 +replace github.com/go-vela/sdk-go => ../sdk-go // feat/add-worker-registration branch + require ( github.com/Masterminds/semver/v3 v3.2.0 - github.com/buildkite/yaml v0.0.0-20210326113714-4a3f40911396 + github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 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/go-git/go-git/v5 v5.5.2 - github.com/go-vela/sdk-go v0.18.1 - github.com/go-vela/server v0.18.1 - github.com/go-vela/types v0.18.1 - github.com/go-vela/worker v0.18.1 + github.com/go-vela/sdk-go v0.18.2-0.20230327141933-e8d38c73b1bb + github.com/go-vela/server v0.18.2-0.20230331001256-f8a9aa116391 + github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c + github.com/go-vela/worker v0.18.2-0.20230404201355-6c146e5199b9 github.com/golang-jwt/jwt/v4 v4.5.0 github.com/gosuri/uitable v0.0.4 github.com/manifoldco/promptui v0.9.0 diff --git a/go.sum b/go.sum index 290f9b21..c362fb07 100644 --- a/go.sum +++ b/go.sum @@ -60,8 +60,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/buildkite/yaml v0.0.0-20210326113714-4a3f40911396 h1:qLN32md48xyTEqw6XEZMyNMre7njm0XXvDrea6NVwOM= -github.com/buildkite/yaml v0.0.0-20210326113714-4a3f40911396/go.mod h1:AV5wtJnn1/CRaRGlJ8xspkMWfKXV0/pkJVgGleTIrfk= +github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 h1:Zfkih+Opdv9y5AOob+8iMsaMYnans+Ozrkb8wiPHbj0= +github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835/go.mod h1:AV5wtJnn1/CRaRGlJ8xspkMWfKXV0/pkJVgGleTIrfk= github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA= @@ -157,14 +157,12 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= -github.com/go-vela/sdk-go v0.18.1 h1:qsm8XWjr9btNDL8c58JC93sstRUybL/TklWgeeft860= -github.com/go-vela/sdk-go v0.18.1/go.mod h1:QmfXBAdJ9prgE78TK13XJI8YjvGZA5hc+h79CbvgYGU= -github.com/go-vela/server v0.18.1 h1:INd+nwLh0c+WA+8diIh4scLkByGBGZHiyVd5doLSolQ= -github.com/go-vela/server v0.18.1/go.mod h1:WyJEXyJYYASfqN9PDuHqlBTbhsSRIzOn1E7tM2phZMA= -github.com/go-vela/types v0.18.1 h1:V/luHLnCEaJhD1m9PZCZicIasg8Op6MCK+utkz+gQiU= -github.com/go-vela/types v0.18.1/go.mod h1:6MzMhLaXKSZ9wiJveieqnBd2+4ZMS7yv7+POGSITyS8= -github.com/go-vela/worker v0.18.1 h1:2dIbi0LtI59apny6yR4xVbjTMZdDnmrpedrPQKO6F3s= -github.com/go-vela/worker v0.18.1/go.mod h1:3HtiTN5vzmnljsEGW65IikXEop/iL1ekZr3coyGysXA= +github.com/go-vela/server v0.18.2-0.20230331001256-f8a9aa116391 h1:wqRHZ+1xig1zAp8NDq4SNfwzI4NJ382WvN5n3Vrq2IU= +github.com/go-vela/server v0.18.2-0.20230331001256-f8a9aa116391/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.20230404201355-6c146e5199b9 h1:8GRg5Zd6zHR6CTHyt08XOltZvPWIpG07+Gzg/96aflI= +github.com/go-vela/worker v0.18.2-0.20230404201355-6c146e5199b9/go.mod h1:KsIrUbQiDMtpmnoJSna4zFfA1LZB8OxlRSlZ2EJ/stU= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= From 6cd76ec260db884c0b4f31a333376bf0b20f5d92 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Wed, 5 Apr 2023 14:48:31 -0500 Subject: [PATCH 05/18] tweak view command --- command/worker/view.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/command/worker/view.go b/command/worker/view.go index 71c9f6bb..caef9dec 100644 --- a/command/worker/view.go +++ b/command/worker/view.go @@ -11,6 +11,7 @@ import ( "github.com/go-vela/cli/action/worker" "github.com/go-vela/cli/internal" "github.com/go-vela/cli/internal/client" + "github.com/google/uuid" "github.com/urfave/cli/v2" ) @@ -30,6 +31,9 @@ var CommandView = &cli.Command{ Name: internal.FlagWorkerHostname, Aliases: []string{"wh"}, Usage: "provide the hostname of the worker", + // there is no current enforcement on passing a valid hostname + // if none is supplied, use a random id. + Value: uuid.NewString(), }, &cli.StringFlag{ EnvVars: []string{"VELA_WORKER_REGISTRATION_TOKEN", "WORKER_REGISTRATION_TOKEN"}, @@ -54,7 +58,7 @@ 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 + $ {{.HelpName}} --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. From e89bd068bb5a864008583d5f2b739727b33a0ea4 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:11:56 -0500 Subject: [PATCH 06/18] fix tests --- action/worker/add_test.go | 2 +- command/worker/add.go | 2 +- command/worker/add_test.go | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/action/worker/add_test.go b/action/worker/add_test.go index 1b6e1746..fae93d54 100644 --- a/action/worker/add_test.go +++ b/action/worker/add_test.go @@ -32,7 +32,7 @@ func TestWorker_Config_Add(t *testing.T) { e := gin.New() // mock endpoint for worker register call - e.GET("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) + e.POST("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) // create a new test server w := httptest.NewServer(e) diff --git a/command/worker/add.go b/command/worker/add.go index 45436d15..cb8722ca 100644 --- a/command/worker/add.go +++ b/command/worker/add.go @@ -55,7 +55,7 @@ var CommandAdd = &cli.Command{ }, CustomHelpTemplate: fmt.Sprintf(`%s EXAMPLES: - 1. Add a worker reachable at the give address. + 1. Add a worker reachable at the provided address. $ {{.HelpName}} --worker.address myworker.example.com DOCUMENTATION: diff --git a/command/worker/add_test.go b/command/worker/add_test.go index 7cf62a3c..3bb462c6 100644 --- a/command/worker/add_test.go +++ b/command/worker/add_test.go @@ -6,9 +6,11 @@ package worker import ( "flag" + "net/http" "net/http/httptest" "testing" + "github.com/gin-gonic/gin" "github.com/go-vela/cli/test" "github.com/go-vela/server/mock/server" @@ -19,6 +21,15 @@ func TestWorker_Add(t *testing.T) { // setup test server s := httptest.NewServer(server.FakeHandler()) + // set up new gin instance for fake worker + e := gin.New() + + // mock endpoint for worker register call + e.GET("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) + + // create a new test server + w := httptest.NewServer(e) + // setup flags authSet := flag.NewFlagSet("test", 0) authSet.String("api.addr", s.URL, "doc") @@ -30,7 +41,7 @@ func TestWorker_Add(t *testing.T) { 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", "myworker.example.com", "doc") + fullSet.String("worker.address", w.URL, "doc") fullSet.String("output", "json", "doc") // setup tests From 6da3b65f9c74aceddf58d89610887ba9d2ae03fa Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:14:54 -0500 Subject: [PATCH 07/18] fix test --- command/worker/add_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/worker/add_test.go b/command/worker/add_test.go index 3bb462c6..b6d03f5b 100644 --- a/command/worker/add_test.go +++ b/command/worker/add_test.go @@ -25,7 +25,7 @@ func TestWorker_Add(t *testing.T) { e := gin.New() // mock endpoint for worker register call - e.GET("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) + e.POST("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) // create a new test server w := httptest.NewServer(e) From 75899e0a119f897f95b7439b713256c9561cb09a Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 6 Apr 2023 17:39:01 -0500 Subject: [PATCH 08/18] fix up tests --- action/worker/add_test.go | 33 ++++++++++++++++++++++----------- action/worker/validate_test.go | 9 +++++++++ action/worker/view_test.go | 9 +++++++++ command/worker/add.go | 8 ++------ command/worker/add_test.go | 13 +++---------- command/worker/view.go | 6 +----- command/worker/view_test.go | 10 ++++++++++ go.mod | 9 +++++---- go.sum | 16 +++++++++------- 9 files changed, 70 insertions(+), 43 deletions(-) diff --git a/action/worker/add_test.go b/action/worker/add_test.go index fae93d54..c46a5c03 100644 --- a/action/worker/add_test.go +++ b/action/worker/add_test.go @@ -5,12 +5,12 @@ package worker import ( - "net/http" "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" ) @@ -19,7 +19,7 @@ func TestWorker_Config_Add(t *testing.T) { // setup context gin.SetMode(gin.TestMode) - // setup test server + // create mock server s := httptest.NewServer(server.FakeHandler()) // create a vela client @@ -28,14 +28,8 @@ func TestWorker_Config_Add(t *testing.T) { t.Errorf("unable to create client: %v", err) } - // set up new gin instance for fake worker - e := gin.New() - - // mock endpoint for worker register call - e.POST("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) - - // create a new test server - w := httptest.NewServer(e) + // create mock worker server + w := httptest.NewServer(worker.FakeHandler()) // setup tests tests := []struct { @@ -87,7 +81,24 @@ func TestWorker_Config_Add(t *testing.T) { Output: "yaml", }, }, - // TODO: mock doesn't have failure for worker creation + { + 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 diff --git a/action/worker/validate_test.go b/action/worker/validate_test.go index 25c1d2dc..cc61fedc 100644 --- a/action/worker/validate_test.go +++ b/action/worker/validate_test.go @@ -32,6 +32,15 @@ func TestWorker_Config_Validate(t *testing.T) { Output: "", }, }, + { + failure: true, + config: &Config{ + Action: "add", + Hostname: "MyWorker", + Address: "", + Output: "", + }, + }, { failure: false, config: &Config{ diff --git a/action/worker/view_test.go b/action/worker/view_test.go index dda0ddc2..d8df2b03 100644 --- a/action/worker/view_test.go +++ b/action/worker/view_test.go @@ -85,6 +85,15 @@ func TestWorker_Config_View(t *testing.T) { Output: "", }, }, + { + failure: true, + config: &Config{ + Action: "view", + Hostname: "", + Output: "", + RegistrationToken: true, + }, + }, } // run tests diff --git a/command/worker/add.go b/command/worker/add.go index cb8722ca..112c9ebe 100644 --- a/command/worker/add.go +++ b/command/worker/add.go @@ -13,7 +13,6 @@ import ( "github.com/go-vela/cli/internal" "github.com/go-vela/cli/internal/client" - "github.com/google/uuid" "github.com/urfave/cli/v2" ) @@ -38,10 +37,7 @@ var CommandAdd = &cli.Command{ EnvVars: []string{"VELA_WORKER_HOSTNAME", "WORKER_HOSTNAME"}, Name: internal.FlagWorkerHostname, Aliases: []string{"wh"}, - Usage: "provide the hostname of the worker (defaults is random ID)", - // there is no current enforcement on passing a valid hostname - // if none is supplied, use a random id. - Value: uuid.NewString(), + Usage: "provide the hostname of the worker", }, // Output Flags @@ -56,7 +52,7 @@ var CommandAdd = &cli.Command{ CustomHelpTemplate: fmt.Sprintf(`%s EXAMPLES: 1. Add a worker reachable at the provided address. - $ {{.HelpName}} --worker.address myworker.example.com + $ {{.HelpName}} --worker.hostname MyWorker --worker.address myworker.example.com DOCUMENTATION: diff --git a/command/worker/add_test.go b/command/worker/add_test.go index b6d03f5b..e11f4be7 100644 --- a/command/worker/add_test.go +++ b/command/worker/add_test.go @@ -6,13 +6,12 @@ package worker import ( "flag" - "net/http" "net/http/httptest" "testing" - "github.com/gin-gonic/gin" "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" ) @@ -21,14 +20,8 @@ func TestWorker_Add(t *testing.T) { // setup test server s := httptest.NewServer(server.FakeHandler()) - // set up new gin instance for fake worker - e := gin.New() - - // mock endpoint for worker register call - e.POST("/register", func(c *gin.Context) { c.JSON(http.StatusOK, "worker registered successfully") }) - - // create a new test server - w := httptest.NewServer(e) + // create mock worker server + w := httptest.NewServer(worker.FakeHandler()) // setup flags authSet := flag.NewFlagSet("test", 0) diff --git a/command/worker/view.go b/command/worker/view.go index caef9dec..05a983b8 100644 --- a/command/worker/view.go +++ b/command/worker/view.go @@ -11,7 +11,6 @@ import ( "github.com/go-vela/cli/action/worker" "github.com/go-vela/cli/internal" "github.com/go-vela/cli/internal/client" - "github.com/google/uuid" "github.com/urfave/cli/v2" ) @@ -31,9 +30,6 @@ var CommandView = &cli.Command{ Name: internal.FlagWorkerHostname, Aliases: []string{"wh"}, Usage: "provide the hostname of the worker", - // there is no current enforcement on passing a valid hostname - // if none is supplied, use a random id. - Value: uuid.NewString(), }, &cli.StringFlag{ EnvVars: []string{"VELA_WORKER_REGISTRATION_TOKEN", "WORKER_REGISTRATION_TOKEN"}, @@ -58,7 +54,7 @@ EXAMPLES: 1. View details of a worker. $ {{.HelpName}} --worker.hostname MyWorker 2. View registration token for a worker. - $ {{.HelpName}} --worker.registration.token true + $ {{.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. diff --git a/command/worker/view_test.go b/command/worker/view_test.go index e9e5fd8e..198a6f76 100644 --- a/command/worker/view_test.go +++ b/command/worker/view_test.go @@ -32,6 +32,12 @@ func TestWorker_View(t *testing.T) { 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 @@ -45,6 +51,10 @@ func TestWorker_View(t *testing.T) { failure: true, set: authSet, }, + { + failure: true, + set: noHostSet, + }, { failure: true, set: flag.NewFlagSet("test", 0), diff --git a/go.mod b/go.mod index 6f8adbf0..2f14abd5 100644 --- a/go.mod +++ b/go.mod @@ -10,11 +10,12 @@ 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.20230327141933-e8d38c73b1bb - github.com/go-vela/server v0.18.2-0.20230331001256-f8a9aa116391 + github.com/go-vela/server v0.18.2-0.20230405140822-34164d0412e2 github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c - github.com/go-vela/worker v0.18.2-0.20230404201355-6c146e5199b9 + github.com/go-vela/worker v0.18.2-0.20230406165141-c76c2e460786 github.com/golang-jwt/jwt/v4 v4.5.0 github.com/gosuri/uitable v0.0.4 github.com/manifoldco/promptui v0.9.0 @@ -51,7 +52,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 @@ -104,6 +104,7 @@ require ( github.com/skeema/knownhosts v1.1.0 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.9 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect @@ -114,7 +115,7 @@ require ( golang.org/x/net v0.7.0 // indirect golang.org/x/oauth2 v0.5.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.5.0 // indirect + golang.org/x/sys v0.6.0 // indirect golang.org/x/term v0.5.0 // indirect golang.org/x/text v0.7.0 // indirect golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect diff --git a/go.sum b/go.sum index c362fb07..86a3f173 100644 --- a/go.sum +++ b/go.sum @@ -157,12 +157,12 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= -github.com/go-vela/server v0.18.2-0.20230331001256-f8a9aa116391 h1:wqRHZ+1xig1zAp8NDq4SNfwzI4NJ382WvN5n3Vrq2IU= -github.com/go-vela/server v0.18.2-0.20230331001256-f8a9aa116391/go.mod h1:b+7XeGHO4ynIinY9mpWb6ye9psdwHpsAqMWy5oC+zJ0= +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/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.20230404201355-6c146e5199b9 h1:8GRg5Zd6zHR6CTHyt08XOltZvPWIpG07+Gzg/96aflI= -github.com/go-vela/worker v0.18.2-0.20230404201355-6c146e5199b9/go.mod h1:KsIrUbQiDMtpmnoJSna4zFfA1LZB8OxlRSlZ2EJ/stU= +github.com/go-vela/worker v0.18.2-0.20230406165141-c76c2e460786 h1:cE3B/pCDJzr8CsFd+c1nGVviB+9VmW9nkZn69IrMlj0= +github.com/go-vela/worker v0.18.2-0.20230406165141-c76c2e460786/go.mod h1:IjSc7hc9uGSSw/TtPntIFbR25ftmiThJrQ588BgtYFk= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= @@ -370,8 +370,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI= +github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU= @@ -558,8 +560,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= From 5f59c961d625bc2e3f679c8481058b44405a3a5e Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:22:56 -0500 Subject: [PATCH 09/18] misc fixes --- command/worker/add.go | 2 +- command/worker/get.go | 2 +- command/worker/update.go | 2 +- command/worker/view.go | 2 +- go.mod | 4 ++-- go.sum | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/command/worker/add.go b/command/worker/add.go index 112c9ebe..766206d2 100644 --- a/command/worker/add.go +++ b/command/worker/add.go @@ -19,7 +19,7 @@ import ( // CommandAdd defines the command for adding a worker. var CommandAdd = &cli.Command{ Name: "worker", - Description: "(Admin Only) Use this command to add a 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{ diff --git a/command/worker/get.go b/command/worker/get.go index 08f8630d..d88c2d41 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -18,7 +18,7 @@ import ( // CommandGet defines the command for capturing a list of workers. var CommandGet = &cli.Command{ Name: "worker", - Description: "(Admin Only) Use this command to get a list of workers.", + Description: "Use this command to get a list of workers.", Usage: "Display a list workers", Action: get, Flags: []cli.Flag{ diff --git a/command/worker/update.go b/command/worker/update.go index 23ea735e..6ab70c9d 100644 --- a/command/worker/update.go +++ b/command/worker/update.go @@ -18,7 +18,7 @@ import ( // CommandUpdate defines the command for modifying a worker. var CommandUpdate = &cli.Command{ Name: "worker", - Description: "(Admin Only) Use this command to update a 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{ diff --git a/command/worker/view.go b/command/worker/view.go index 05a983b8..b63ceeba 100644 --- a/command/worker/view.go +++ b/command/worker/view.go @@ -18,7 +18,7 @@ import ( // CommandView defines the command for inspecting a worker. var CommandView = &cli.Command{ Name: "worker", - Description: "(Admin Only) Use this command to view a worker.", + Description: "Use this command to view a worker.", Usage: "View details of the provided worker", Action: view, Flags: []cli.Flag{ diff --git a/go.mod b/go.mod index 2f14abd5..e31afd84 100644 --- a/go.mod +++ b/go.mod @@ -12,8 +12,8 @@ require ( 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.20230327141933-e8d38c73b1bb - github.com/go-vela/server v0.18.2-0.20230405140822-34164d0412e2 + github.com/go-vela/sdk-go v0.18.2-0.20230407134447-c34cd778f44a + 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 diff --git a/go.sum b/go.sum index 86a3f173..6c81e9f6 100644 --- a/go.sum +++ b/go.sum @@ -157,8 +157,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= -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= From b14c03a636d133e2ef17cdb6dafd89f4d420c318 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:24:55 -0500 Subject: [PATCH 10/18] remove replace from go.mod --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index e31afd84..41976cb7 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/go-vela/cli go 1.19 -replace github.com/go-vela/sdk-go => ../sdk-go // feat/add-worker-registration branch - require ( github.com/Masterminds/semver/v3 v3.2.0 github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 From 5d21e1adacbf74e897d09d219e7a5dc58e7486c4 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:36:12 -0500 Subject: [PATCH 11/18] make clean --- go.sum | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.sum b/go.sum index 6c81e9f6..3fbd46ba 100644 --- a/go.sum +++ b/go.sum @@ -157,6 +157,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= 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.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= From faa3b79f4527034a6c0df79cb33d1a8228d26754 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:57:43 -0500 Subject: [PATCH 12/18] the linter said so --- action/worker/update.go | 1 - action/worker/view.go | 1 + command/worker/get.go | 2 -- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/action/worker/update.go b/action/worker/update.go index eeea09c3..5bd0d27e 100644 --- a/action/worker/update.go +++ b/action/worker/update.go @@ -2,7 +2,6 @@ // // Use of this source code is governed by the LICENSE file in this repository. -//nolint:dupl // ignore similar code among actions package worker import ( diff --git a/action/worker/view.go b/action/worker/view.go index 5343db6c..aa2c00c5 100644 --- a/action/worker/view.go +++ b/action/worker/view.go @@ -6,6 +6,7 @@ package worker import ( "fmt" + "github.com/go-vela/cli/internal/output" "github.com/go-vela/sdk-go/vela" diff --git a/command/worker/get.go b/command/worker/get.go index d88c2d41..635629b7 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -54,8 +54,6 @@ DOCUMENTATION: // helper function to capture the provided input // and create the object used to capture a list // of workers. -// -//nolint:dupl // ignore similar code with chown, get, remove and repair func get(c *cli.Context) error { // load variables from the config file err := action.Load(c) From e7ed402ce79bd51f63517e2e5854a70d763b1197 Mon Sep 17 00:00:00 2001 From: David May <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:15:59 -0500 Subject: [PATCH 13/18] Update cmd/vela-cli/get.go Co-authored-by: dave vader <48764154+plyr4@users.noreply.github.com> --- cmd/vela-cli/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/vela-cli/get.go b/cmd/vela-cli/get.go index 8a31e18c..97bec67f 100644 --- a/cmd/vela-cli/get.go +++ b/cmd/vela-cli/get.go @@ -73,7 +73,7 @@ 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 worker + // 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, From 26cf883c1abe6e3e7111b676fdee87e581d483e1 Mon Sep 17 00:00:00 2001 From: David May <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:16:10 -0500 Subject: [PATCH 14/18] Update command/worker/get.go Co-authored-by: dave vader <48764154+plyr4@users.noreply.github.com> --- command/worker/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/worker/get.go b/command/worker/get.go index 635629b7..7c4968e0 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -19,7 +19,7 @@ import ( var CommandGet = &cli.Command{ Name: "worker", Description: "Use this command to get a list of workers.", - Usage: "Display a list workers", + Usage: "Display a list of workers", Action: get, Flags: []cli.Flag{ From bb2b6595f9b21f8b4c1a157872b502eb4e96229e Mon Sep 17 00:00:00 2001 From: David May <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:16:21 -0500 Subject: [PATCH 15/18] Update command/worker/get.go Co-authored-by: dave vader <48764154+plyr4@users.noreply.github.com> --- command/worker/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/worker/get.go b/command/worker/get.go index 7c4968e0..1305b0e2 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -29,7 +29,7 @@ var CommandGet = &cli.Command{ EnvVars: []string{"VELA_OUTPUT", "WORKER_OUTPUT"}, Name: internal.FlagOutput, Aliases: []string{"op"}, - Usage: "format the output in json, spew or yaml", + Usage: "format the output in wide, json, spew or yaml", }, }, CustomHelpTemplate: fmt.Sprintf(`%s From 90c7036eda1b3207dcfd92561c5f4bf949824a77 Mon Sep 17 00:00:00 2001 From: David May <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:16:29 -0500 Subject: [PATCH 16/18] Update command/worker/get.go Co-authored-by: dave vader <48764154+plyr4@users.noreply.github.com> --- command/worker/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/worker/get.go b/command/worker/get.go index 1305b0e2..40c0f0b3 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -85,7 +85,7 @@ func get(c *cli.Context) error { return err } - // execute the view call for the worker configuration + // execute the get call for the worker configuration // // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.View return w.Get(client) From 699731018f9c10b51db7ce8db36aeceec4d009f5 Mon Sep 17 00:00:00 2001 From: David May <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:16:37 -0500 Subject: [PATCH 17/18] Update command/worker/get.go Co-authored-by: dave vader <48764154+plyr4@users.noreply.github.com> --- command/worker/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/worker/get.go b/command/worker/get.go index 40c0f0b3..5937a547 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -87,6 +87,6 @@ func get(c *cli.Context) error { // execute the get call for the worker configuration // - // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.View + // https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Get return w.Get(client) } From 6d2e4b604af61e89dfcb388f8450458f85f1b074 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 13 Apr 2023 15:21:55 -0500 Subject: [PATCH 18/18] fix: allow plural of worker --- command/worker/get.go | 1 + 1 file changed, 1 insertion(+) diff --git a/command/worker/get.go b/command/worker/get.go index 5937a547..d9126cc7 100644 --- a/command/worker/get.go +++ b/command/worker/get.go @@ -18,6 +18,7 @@ import ( // 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,