diff --git a/action/action.go b/action/action.go index fcca2364..5e3e05fd 100644 --- a/action/action.go +++ b/action/action.go @@ -9,6 +9,9 @@ const ( // addAction defines the action for creating a resource. addAction = "add" + // cancelAction defines the action for canceling of a resource. + cancelAction = "cancel" + // chownAction defines the action for changing ownership of a resource. chownAction = "chown" diff --git a/action/build/cancel.go b/action/build/cancel.go new file mode 100644 index 00000000..a3a40d0d --- /dev/null +++ b/action/build/cancel.go @@ -0,0 +1,57 @@ +// Copyright (c) 2020 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package build + +import ( + "github.com/go-vela/cli/internal/output" + + "github.com/go-vela/sdk-go/vela" + + "github.com/sirupsen/logrus" +) + +// Cancel cancels a build based off the provided configuration. +func (c *Config) Cancel(client *vela.Client) error { + logrus.Debug("executing cancel for build configuration") + + logrus.Tracef("canceling build %s/%s/%d", c.Org, c.Repo, c.Number) + + // send API call to cancel a build + // + // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#BuildService.Cancel + build, _, err := client.Build.Cancel(c.Org, c.Repo, c.Number) + if err != nil { + return err + } + + // handle the output based off the provided configuration + switch c.Output { + case output.DriverDump: + // output the build in dump format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump + return output.Dump(build) + case output.DriverJSON: + // output the build in JSON format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON + return output.JSON(build) + case output.DriverSpew: + // output the build in spew format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew + return output.Spew(build) + case output.DriverYAML: + // output the build in YAML format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML + return output.YAML(&build) + default: + // output the build in stdout format + // + // https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout + return output.Stdout(build) + } +} diff --git a/action/build/cancel_test.go b/action/build/cancel_test.go new file mode 100644 index 00000000..ab329463 --- /dev/null +++ b/action/build/cancel_test.go @@ -0,0 +1,111 @@ +// Copyright (c) 2020 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// Package build contains resources for managing builds +// nolint:dupl // code is similar to action/build_restart.go, but is not duplicative +package build + +import ( + "net/http/httptest" + "testing" + + "github.com/go-vela/mock/server" + + "github.com/go-vela/sdk-go/vela" +) + +func TestBuild_Config_Cancel(t *testing.T) { + // setup test server + s := httptest.NewServer(server.FakeHandler()) + + // create a vela client + client, err := vela.NewClient(s.URL, "", 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: "cancel", + Org: "github", + Repo: "octocat", + Number: 1, + Output: "", + }, + }, + { + failure: false, + config: &Config{ + Action: "cancel", + Org: "github", + Repo: "octocat", + Number: 1, + Output: "dump", + }, + }, + { + failure: false, + config: &Config{ + Action: "cancel", + Org: "github", + Repo: "octocat", + Number: 1, + Output: "json", + }, + }, + { + failure: false, + config: &Config{ + Action: "cancel", + Org: "github", + Repo: "octocat", + Number: 1, + Output: "spew", + }, + }, + { + failure: false, + config: &Config{ + Action: "cancel", + Org: "github", + Repo: "octocat", + Number: 1, + Output: "yaml", + }, + }, + { + failure: true, + config: &Config{ + Action: "cancel", + Org: "github", + Repo: "octocat", + Number: 0, + Output: "", + }, + }, + } + + // run tests + for _, test := range tests { + err := test.config.Cancel(client) + + if test.failure { + if err == nil { + t.Errorf("Cancel should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("Cancel returned err: %v", err) + } + } +} diff --git a/action/build_cancel.go b/action/build_cancel.go new file mode 100644 index 00000000..ccb6f853 --- /dev/null +++ b/action/build_cancel.go @@ -0,0 +1,114 @@ +// Copyright (c) 2020 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// Package action contains all cli actions +// nolint:dupl // code is similar to action/build_restart.go, but is not duplicative +package action + +import ( + "fmt" + + "github.com/go-vela/cli/action/build" + "github.com/go-vela/cli/internal" + "github.com/go-vela/cli/internal/client" + + "github.com/urfave/cli/v2" +) + +// BuildCancel defines the command for canceling a build. +var BuildCancel = &cli.Command{ + Name: internal.FlagBuild, + Description: "Use this command to cancel a build.", + Usage: "Cancel the provided build", + Action: buildCancel, + Flags: []cli.Flag{ + + // Repo Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_ORG", "BUILD_ORG"}, + Name: internal.FlagOrg, + Aliases: []string{"o"}, + Usage: "provide the organization for the build", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_REPO", "BUILD_REPO"}, + Name: internal.FlagRepo, + Aliases: []string{"r"}, + Usage: "provide the repository for the build", + }, + + // Build Flags + + &cli.IntFlag{ + EnvVars: []string{"VELA_BUILD", "BUILD_NUMBER"}, + Name: internal.FlagBuild, + Aliases: []string{"b"}, + Usage: "provide the number for the build", + }, + + // Output Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_OUTPUT", "BUILD_OUTPUT"}, + Name: internal.FlagOutput, + Aliases: []string{"op"}, + Usage: "format the output in json, spew or yaml", + }, + }, + CustomHelpTemplate: fmt.Sprintf(`%s +EXAMPLES: + 1. Cancel existing build for a repository. + $ {{.HelpName}} --org MyOrg --repo MyRepo --build 1 + 2. Cancel existing build for a repository when config or environment variables are set. + $ {{.HelpName}} --build 1 + +DOCUMENTATION: + + https://go-vela.github.io/docs/cli/build/cancel/ +`, cli.CommandHelpTemplate), +} + +// helper function to capture the provided +// input and create the object used to +// cancel a build. +func buildCancel(c *cli.Context) error { + // load variables from the config file + err := 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 build configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config + b := &build.Config{ + Action: cancelAction, + Org: c.String(internal.FlagOrg), + Repo: c.String(internal.FlagRepo), + Number: c.Int(internal.FlagBuild), + Output: c.String(internal.FlagOutput), + } + + // validate build configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config.Validate + err = b.Validate() + if err != nil { + return err + } + + // execute the cancel call for the build configuration + // + // https://pkg.go.dev/github.com/go-vela/cli/action/build?tab=doc#Config.Cancel + return b.Cancel(client) +} diff --git a/action/build_cancel_test.go b/action/build_cancel_test.go new file mode 100644 index 00000000..0b81d5a2 --- /dev/null +++ b/action/build_cancel_test.go @@ -0,0 +1,74 @@ +// Copyright (c) 2020 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// Package action contains all cli actions +// nolint:dupl // code is similar to action/build_restart.go, but is not duplicative +package action + +import ( + "flag" + "net/http/httptest" + "testing" + + "github.com/go-vela/cli/test" + "github.com/go-vela/mock/server" + + "github.com/urfave/cli/v2" +) + +func TestAction_BuildCancel(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("org", "github", "doc") + fullSet.String("repo", "octocat", "doc") + fullSet.Int("build", 1, "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 := buildCancel(cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, test.set, nil)) + + if test.failure { + if err == nil { + t.Errorf("buildCancel should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("buildCancel returned err: %v", err) + } + } +} diff --git a/cmd/vela-cli/cancel.go b/cmd/vela-cli/cancel.go new file mode 100644 index 00000000..82a6776a --- /dev/null +++ b/cmd/vela-cli/cancel.go @@ -0,0 +1,27 @@ +// Copyright (c) 2020 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package main + +import ( + "github.com/go-vela/cli/action" + + "github.com/urfave/cli/v2" +) + +// cancelCmds defines the commands for canceling resources. +var cancelCmds = &cli.Command{ + Name: "cancel", + Category: "Resource Management", + Aliases: []string{"cx"}, + Description: "Use this command to cancel a resource for Vela.", + Usage: "Cancel a resource for Vela via subcommands", + UseShortOptionHandling: true, + Subcommands: []*cli.Command{ + // add the sub command for canceling a build + // + // https://pkg.go.dev/github.com/go-vela/cli/action?tab=doc#BuildCancel + action.BuildCancel, + }, +} diff --git a/cmd/vela-cli/main.go b/cmd/vela-cli/main.go index bb30ea36..74469405 100644 --- a/cmd/vela-cli/main.go +++ b/cmd/vela-cli/main.go @@ -48,6 +48,7 @@ func main() { action.Login, action.Version, addCmds, + cancelCmds, chownCmds, compileCmds, execCmds, diff --git a/go.mod b/go.mod index 5da6249f..ac3b7d0b 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,8 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dustin/go-humanize v1.0.0 github.com/go-vela/compiler v0.6.1-0.20210111222408-a8b5012a5943 - github.com/go-vela/mock v0.6.1-0.20210111203055-2629f560e9b7 - github.com/go-vela/sdk-go v0.6.1-0.20210111215046-d4ccd4904b94 + github.com/go-vela/mock v0.6.1-0.20210112201653-ecad0dfe0e48 + github.com/go-vela/sdk-go v0.6.1-0.20210112212028-77fd54952b28 github.com/go-vela/types v0.6.1-0.20210111181528-d3bb371e9ec6 github.com/gosuri/uitable v0.0.4 github.com/lunixbochs/vtclean v1.0.0 // indirect diff --git a/go.sum b/go.sum index 8df68e0a..5299fbef 100644 --- a/go.sum +++ b/go.sum @@ -122,10 +122,10 @@ github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7a github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-vela/compiler v0.6.1-0.20210111222408-a8b5012a5943 h1:u5SnyLxzI/3dXUJYADUMqQxPuS1urlXOz9KX8Uf7Ezg= github.com/go-vela/compiler v0.6.1-0.20210111222408-a8b5012a5943/go.mod h1:yvHes2dsuuqKS4izs+iKySiQsbfo4DCvnuAUSk93pbA= -github.com/go-vela/mock v0.6.1-0.20210111203055-2629f560e9b7 h1:X8MlnMw6ipcRHnLWm4IR0+HD3YVzKVEiVGgWd+iImtQ= -github.com/go-vela/mock v0.6.1-0.20210111203055-2629f560e9b7/go.mod h1:AYp7yyYW10fRmUCfnUj7QdpAdxor7mDFn163R7ni4Dk= -github.com/go-vela/sdk-go v0.6.1-0.20210111215046-d4ccd4904b94 h1:4gf7MAd9QxfL1N5AYKp+QOAXShRO4v8Mdr+/zgzx9l8= -github.com/go-vela/sdk-go v0.6.1-0.20210111215046-d4ccd4904b94/go.mod h1:+RRcqqGGg2fYUuOYL5Jg1iNsQ8+cSA3u+tb+pB1xEwY= +github.com/go-vela/mock v0.6.1-0.20210112201653-ecad0dfe0e48 h1:OBMFP3jxFCLeeWYYbW9QzOoixMlEdw63yDDzrLIKSkU= +github.com/go-vela/mock v0.6.1-0.20210112201653-ecad0dfe0e48/go.mod h1:AYp7yyYW10fRmUCfnUj7QdpAdxor7mDFn163R7ni4Dk= +github.com/go-vela/sdk-go v0.6.1-0.20210112212028-77fd54952b28 h1:Ya2UexsPMFf8RrCGm+A9v4G5rlIQf5Y/UP5WXLPMh3o= +github.com/go-vela/sdk-go v0.6.1-0.20210112212028-77fd54952b28/go.mod h1:hQZBdM8HBnSRrUjDh5CMufTFZCqnTvbND0x+qT+CreQ= github.com/go-vela/types v0.6.1-0.20210111181528-d3bb371e9ec6 h1:zmdwRI81qWFTw7H37nIzu/Y3UoSD5cih0UwjeO3kI3o= github.com/go-vela/types v0.6.1-0.20210111181528-d3bb371e9ec6/go.mod h1:ATtwTwp2l4jI4GUmw840xHZgHmg8FbZPo4g0azImggA= github.com/goccy/go-yaml v1.8.4 h1:AOEdR7aQgbgwHznGe3BLkDQVujxCPUpHOZZcQcp8Y3M=