Actual drone-invokable helm commands
This commit is contained in:
parent
e77f6842b9
commit
238ede6f9e
|
@ -8,6 +8,7 @@ steps:
|
|||
image: golang:1.13
|
||||
commands:
|
||||
- go vet ./...
|
||||
- go test ./...
|
||||
- name: build
|
||||
image: golang:1.13
|
||||
commands:
|
||||
|
|
|
@ -2,25 +2,23 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli"
|
||||
"os"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"github.com/pelotech/drone-helm3/internal/run"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_ = fmt.Println
|
||||
_ = os.Exit
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "helm plugin"
|
||||
app.Usage = "helm plugin"
|
||||
app.Action = run
|
||||
app.Action = execute
|
||||
app.Version = "0.0.1α"
|
||||
app.Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "echo, e",
|
||||
Usage: "this text'll be ech'll",
|
||||
EnvVar: "ECHO",
|
||||
Name: "helm_command",
|
||||
Usage: "Helm command to execute",
|
||||
EnvVar: "PLUGIN_HELM_COMMAND,HELM_COMMAND",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -29,7 +27,19 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
func run(c *cli.Context) error {
|
||||
fmt.Println(c.String("echo"))
|
||||
|
||||
func execute(c *cli.Context) error {
|
||||
switch c.String("helm_command") {
|
||||
case "upgrade":
|
||||
run.Upgrade()
|
||||
case "help":
|
||||
run.Help()
|
||||
default:
|
||||
switch os.Getenv("DRONE_BUILD_EVENT") {
|
||||
case "push", "tag", "deployment", "pull_request", "promote", "rollback":
|
||||
run.Upgrade()
|
||||
default:
|
||||
run.Help()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -4,5 +4,6 @@ go 1.13
|
|||
|
||||
require (
|
||||
github.com/golang/mock v1.3.1
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/urfave/cli v1.22.0
|
||||
)
|
||||
|
|
7
go.sum
7
go.sum
|
@ -1,10 +1,17 @@
|
|||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
|
||||
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
|
||||
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/urfave/cli v1.22.0 h1:8nz/RUUotroXnOpYzT/Fy3sBp+2XEbXaY641/s3nbFI=
|
||||
github.com/urfave/cli v1.22.0/go.mod h1:b3D7uWrF2GilkNgYpgcg6J+JMUw7ehmNkE8sZdliGLc=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"syscall"
|
||||
)
|
||||
|
||||
const HELM_BIN = "/usr/bin/helm"
|
||||
|
||||
// The cmd interface provides a generic form of exec.Cmd so that it can be mocked out in tests.
|
||||
type cmd interface {
|
||||
// methods that exist on exec.Cmd
|
||||
|
@ -41,8 +43,10 @@ type execCmd struct {
|
|||
*exec.Cmd
|
||||
}
|
||||
|
||||
var Command = func() cmd {
|
||||
return &execCmd{}
|
||||
var Command = func(path string, args ...string) cmd {
|
||||
return &execCmd{
|
||||
Cmd: exec.Command(path, args...),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *execCmd) Path(p string) { c.Cmd.Path = p }
|
||||
|
|
15
internal/run/help.go
Normal file
15
internal/run/help.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func Help(args ...string) error {
|
||||
args = append([]string{"help"}, args...)
|
||||
|
||||
cmd := Command(HELM_BIN, args...)
|
||||
cmd.Stdout(os.Stdout)
|
||||
cmd.Stderr(os.Stderr)
|
||||
|
||||
return cmd.Run()
|
||||
}
|
32
internal/run/help_test.go
Normal file
32
internal/run/help_test.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHelp(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
mCmd := NewMockcmd(ctrl)
|
||||
originalCommand := Command
|
||||
|
||||
Command = func(path string, args ...string) cmd {
|
||||
assert.Equal(t, HELM_BIN, path)
|
||||
assert.Equal(t, []string{"help", "arg1", "arg2"}, args)
|
||||
return mCmd
|
||||
}
|
||||
defer func() { Command = originalCommand }()
|
||||
|
||||
mCmd.EXPECT().
|
||||
Stdout(gomock.Any())
|
||||
mCmd.EXPECT().
|
||||
Stderr(gomock.Any())
|
||||
mCmd.EXPECT().
|
||||
Run().
|
||||
Times(1)
|
||||
|
||||
Help("arg1", "arg2")
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package run
|
||||
|
||||
import ()
|
||||
|
||||
const HELM_BIN = "/usr/bin/helm"
|
||||
|
||||
func Install(args ...string) error {
|
||||
cmd := Command()
|
||||
cmd.Path(HELM_BIN)
|
||||
|
||||
args = append([]string{"install"}, args...)
|
||||
cmd.Args(args)
|
||||
|
||||
return cmd.Run()
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"github.com/golang/mock/gomock"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInstall(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
mCmd := NewMockcmd(ctrl)
|
||||
originalCommand := Command
|
||||
Command = func() cmd { return mCmd }
|
||||
defer func() { Command = originalCommand }()
|
||||
|
||||
mCmd.EXPECT().
|
||||
Path(HELM_BIN)
|
||||
mCmd.EXPECT().
|
||||
Args(gomock.Eq([]string{"install", "arg1", "arg2"}))
|
||||
mCmd.EXPECT().
|
||||
Run().
|
||||
Times(1)
|
||||
|
||||
Install("arg1", "arg2")
|
||||
}
|
15
internal/run/upgrade.go
Normal file
15
internal/run/upgrade.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func Upgrade(args ...string) error {
|
||||
args = append([]string{"upgrade"}, args...)
|
||||
cmd := Command(HELM_BIN, args...)
|
||||
|
||||
cmd.Stdout(os.Stdout)
|
||||
cmd.Stderr(os.Stderr)
|
||||
|
||||
return cmd.Run()
|
||||
}
|
33
internal/run/upgrade_test.go
Normal file
33
internal/run/upgrade_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUpgrade(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
mCmd := NewMockcmd(ctrl)
|
||||
originalCommand := Command
|
||||
|
||||
Command = func(path string, args ...string) cmd {
|
||||
assert.Equal(t, HELM_BIN, path)
|
||||
assert.Equal(t, []string{"upgrade", "arg1", "arg2"}, args)
|
||||
|
||||
return mCmd
|
||||
}
|
||||
defer func() { Command = originalCommand }()
|
||||
|
||||
mCmd.EXPECT().
|
||||
Stdout(gomock.Any())
|
||||
mCmd.EXPECT().
|
||||
Stderr(gomock.Any())
|
||||
mCmd.EXPECT().
|
||||
Run().
|
||||
Times(1)
|
||||
|
||||
Upgrade("arg1", "arg2")
|
||||
}
|
Loading…
Reference in a new issue