Skip to content

Commit

Permalink
feat: self-updater (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored May 30, 2024
1 parent 56f169b commit 2d3c22b
Show file tree
Hide file tree
Showing 30 changed files with 609 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --clean
version: "~> 1"
args: release --clean --snapshot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25 changes: 5 additions & 20 deletions cli/cage/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,23 @@ import (
"golang.org/x/xerrors"
)

type CageCommands interface {
Commands(
envars *cage.Envars,
) []*cli.Command
}

type cageCommands struct {
type CageCommands struct {
Prompt *prompt.Prompter
cageCliProvier cageCliProvier
}

func NewCageCommands(
stdin io.Reader,
cageCliProvier cageCliProvier,
) CageCommands {
return &cageCommands{
) *CageCommands {
return &CageCommands{
Prompt: prompt.NewPrompter(stdin),
cageCliProvier: cageCliProvier,
}
}

type cageCliProvier = func(envars *cage.Envars) (cage.Cage, error)

func (c *cageCommands) Commands(envars *cage.Envars) []*cli.Command {
return []*cli.Command{
c.Up(envars),
c.RollOut(envars),
c.Run(envars),
c.Recreate(envars),
}
}

func DefalutCageCliProvider(envars *cage.Envars) (cage.Cage, error) {
conf, err := config.LoadDefaultConfig(
context.Background(),
Expand All @@ -62,7 +47,7 @@ func DefalutCageCliProvider(envars *cage.Envars) (cage.Cage, error) {
return cagecli, nil
}

func (c *cageCommands) requireArgs(
func (c *CageCommands) requireArgs(
ctx *cli.Context,
minArgs int,
maxArgs int,
Expand All @@ -77,7 +62,7 @@ func (c *cageCommands) requireArgs(
return
}

func (c *cageCommands) setupCage(
func (c *CageCommands) setupCage(
envars *cage.Envars,
dir string,
) (cage.Cage, error) {
Expand Down
11 changes: 9 additions & 2 deletions cli/cage/commands/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ func TestCommands(t *testing.T) {
stdin := strings.NewReader(input)
cagecli := mock_cage.NewMockCage(ctrl)
app := cli.NewApp()
app.Commands = commands.NewCageCommands(stdin, func(envars *cage.Envars) (cage.Cage, error) {
cmds := commands.NewCageCommands(stdin, func(envars *cage.Envars) (cage.Cage, error) {
return cagecli, nil
}).Commands(&cage.Envars{CI: input == ""})
})
envars := cage.Envars{CI: input == ""}
app.Commands = []*cli.Command{
cmds.Up(&envars),
cmds.RollOut(&envars),
cmds.Run(&envars),
cmds.Recreate(&envars),
}
return app, cagecli
}
t.Run("rollout", func(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions cli/cage/commands/export_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cli/cage/commands/recreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/urfave/cli/v2"
)

func (c *cageCommands) Recreate(
func (c *CageCommands) Recreate(
envars *cage.Envars,
) *cli.Command {
return &cli.Command{
Expand Down
2 changes: 1 addition & 1 deletion cli/cage/commands/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/urfave/cli/v2"
)

func (c *cageCommands) RollOut(
func (c *CageCommands) RollOut(
envars *cage.Envars,
) *cli.Command {
return &cli.Command{
Expand Down
2 changes: 1 addition & 1 deletion cli/cage/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/urfave/cli/v2"
)

func (c *cageCommands) Run(
func (c *CageCommands) Run(
envars *cage.Envars,
) *cli.Command {
return &cli.Command{
Expand Down
2 changes: 1 addition & 1 deletion cli/cage/commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/urfave/cli/v2"
)

func (c *cageCommands) Up(
func (c *CageCommands) Up(
envars *cage.Envars,
) *cli.Command {
return &cli.Command{
Expand Down
28 changes: 28 additions & 0 deletions cli/cage/commands/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package commands

import (
"github.com/loilo-inc/canarycage/cli/cage/upgrade"
"github.com/urfave/cli/v2"
)

func (c *CageCommands) Upgrade(
upgrader upgrade.Upgrader,
) *cli.Command {
var preRelease bool
return &cli.Command{
Name: "upgrade",
Usage: "upgrade cage binary with the latest version",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "pre-release",
Usage: "include pre-release versions",
Destination: &preRelease,
},
},
Action: func(ctx *cli.Context) error {
return upgrader.Upgrade(&upgrade.Input{
PreRelease: preRelease,
})
},
}
}
43 changes: 43 additions & 0 deletions cli/cage/commands/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commands_test

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/loilo-inc/canarycage/cli/cage/commands"
"github.com/loilo-inc/canarycage/cli/cage/upgrade"
"github.com/loilo-inc/canarycage/mocks/mock_upgrade"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)

func TestUpgrade(t *testing.T) {
t.Run("Upgrade", func(t *testing.T) {
app := cli.NewApp()
ctrl := gomock.NewController(t)
u := mock_upgrade.NewMockUpgrader(ctrl)
u.EXPECT().Upgrade(
gomock.Eq(&upgrade.Input{}),
).Return(nil)
cmds := commands.NewCageCommands(nil, nil)
app.Commands = []*cli.Command{
cmds.Upgrade(u),
}
err := app.Run([]string{"cage", "upgrade"})
assert.NoError(t, err)
})
t.Run("Upgrade with pre-release", func(t *testing.T) {
app := cli.NewApp()
ctrl := gomock.NewController(t)
u := mock_upgrade.NewMockUpgrader(ctrl)
u.EXPECT().Upgrade(
gomock.Eq(&upgrade.Input{PreRelease: true}),
).Return(nil)
cmds := commands.NewCageCommands(nil, nil)
app.Commands = []*cli.Command{
cmds.Upgrade(u),
}
err := app.Run([]string{"cage", "upgrade", "--pre-release"})
assert.NoError(t, err)
})
}
19 changes: 17 additions & 2 deletions cli/cage/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
package main

import (
"fmt"
"log"
"os"

cage "github.com/loilo-inc/canarycage"
"github.com/loilo-inc/canarycage/cli/cage/commands"
"github.com/loilo-inc/canarycage/cli/cage/upgrade"
"github.com/urfave/cli/v2"
)

// set by goreleaser
var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
app := cli.NewApp()
app.Name = "canarycage"
app.Version = "4.0.0-rc1"
app.Version = fmt.Sprintf("%s (commit: %s, date: %s)", version, commit, date)
app.Usage = "A deployment tool for AWS ECS"
app.Description = "A deployment tool for AWS ECS"
envars := cage.Envars{}
cmds := commands.NewCageCommands(os.Stdin, commands.DefalutCageCliProvider)
app.Commands = cmds.Commands(&envars)
app.Commands = []*cli.Command{
cmds.Up(&envars),
cmds.RollOut(&envars),
cmds.Run(&envars),
cmds.Recreate(&envars),
cmds.Upgrade(upgrade.NewUpgrader(version)),
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "ci",
Expand Down
3 changes: 3 additions & 0 deletions cli/cage/upgrade/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package upgrade

type ExportedUpgrader = upgrader
1 change: 1 addition & 0 deletions cli/cage/upgrade/testdata/0.1.0/cage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
e00aeaebd63dc17194891514411426aa50ec51b1095fe85df927106184c71b47 canarycage_darwin_arm64.zip
e00aeaebd63dc17194891514411426aa50ec51b1095fe85df927106184c71b47 canarycage_linux_amd64.zip
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions cli/cage/upgrade/testdata/0.2.0/cage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4ec51ab19c8663c7a90d42eaab7e46e84fc0a227341b70a1ac7a24a311bc3e5a canarycage_darwin_arm64.zip
4ec51ab19c8663c7a90d42eaab7e46e84fc0a227341b70a1ac7a24a311bc3e5a canarycage_linux_amd64.zip
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions cli/cage/upgrade/testdata/0.2.1-rc1/cage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.1-rc1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
867eaaeb40483a0ed532fa6474691134c8a5f6c99468e70a3603d65e4018a185 canarycage_darwin_arm64.zip
867eaaeb40483a0ed532fa6474691134c8a5f6c99468e70a3603d65e4018a185 canarycage_linux_amd64.zip
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 2d3c22b

Please sign in to comment.