Skip to content

Commit

Permalink
Put the Config in a new env package [#67]
Browse files Browse the repository at this point in the history
I'd like to be able to make calls like NewUpgrade(cfg) rather than
Upgrade{...}.Prepare, but I wouldn't be able to define a NewUpgrade
function while Config is in the helm package; there would be a circular
import when Plan tried to import run.
  • Loading branch information
ErinCall committed Jan 14, 2020
1 parent 8a9cf23 commit 16117ee
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
3 changes: 2 additions & 1 deletion cmd/drone-helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"os"

_ "github.com/joho/godotenv/autoload"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/helm"
)

func main() {
cfg, err := helm.NewConfig(os.Stdout, os.Stderr)
cfg, err := env.NewConfig(os.Stdout, os.Stderr)

if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion internal/helm/config.go → internal/env/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helm
package env

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helm
package env

import (
"fmt"
Expand Down
21 changes: 11 additions & 10 deletions internal/helm/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helm

import (
"fmt"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/run"
"os"
)
Expand All @@ -20,12 +21,12 @@ type Step interface {
// A Plan is a series of steps to perform.
type Plan struct {
steps []Step
cfg Config
cfg env.Config
runCfg run.Config
}

// NewPlan makes a plan for running a helm operation.
func NewPlan(cfg Config) (*Plan, error) {
func NewPlan(cfg env.Config) (*Plan, error) {
p := Plan{
cfg: cfg,
runCfg: run.Config{
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewPlan(cfg Config) (*Plan, error) {

// determineSteps is primarily for the tests' convenience: it allows testing the "which stuff should
// we do" logic without building a config that meets all the steps' requirements.
func determineSteps(cfg Config) *func(Config) []Step {
func determineSteps(cfg env.Config) *func(env.Config) []Step {
switch cfg.Command {
case "upgrade":
return &upgrade
Expand Down Expand Up @@ -91,7 +92,7 @@ func (p *Plan) Execute() error {
return nil
}

var upgrade = func(cfg Config) []Step {
var upgrade = func(cfg env.Config) []Step {
steps := initKube(cfg)
steps = append(steps, addRepos(cfg)...)
if cfg.UpdateDependencies {
Expand All @@ -116,7 +117,7 @@ var upgrade = func(cfg Config) []Step {
return steps
}

var uninstall = func(cfg Config) []Step {
var uninstall = func(cfg env.Config) []Step {
steps := initKube(cfg)
if cfg.UpdateDependencies {
steps = append(steps, depUpdate(cfg)...)
Expand All @@ -130,7 +131,7 @@ var uninstall = func(cfg Config) []Step {
return steps
}

var lint = func(cfg Config) []Step {
var lint = func(cfg env.Config) []Step {
steps := addRepos(cfg)
if cfg.UpdateDependencies {
steps = append(steps, depUpdate(cfg)...)
Expand All @@ -146,14 +147,14 @@ var lint = func(cfg Config) []Step {
return steps
}

var help = func(cfg Config) []Step {
var help = func(cfg env.Config) []Step {
help := &run.Help{
HelmCommand: cfg.Command,
}
return []Step{help}
}

func initKube(cfg Config) []Step {
func initKube(cfg env.Config) []Step {
return []Step{
&run.InitKube{
SkipTLSVerify: cfg.SkipTLSVerify,
Expand All @@ -167,7 +168,7 @@ func initKube(cfg Config) []Step {
}
}

func addRepos(cfg Config) []Step {
func addRepos(cfg env.Config) []Step {
steps := make([]Step, 0)
for _, repo := range cfg.AddRepos {
steps = append(steps, &run.AddRepo{
Expand All @@ -178,7 +179,7 @@ func addRepos(cfg Config) []Step {
return steps
}

func depUpdate(cfg Config) []Step {
func depUpdate(cfg env.Config) []Step {
return []Step{
&run.DepUpdate{
Chart: cfg.Chart,
Expand Down
45 changes: 23 additions & 22 deletions internal/helm/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/run"
)

Expand All @@ -25,14 +26,14 @@ func (suite *PlanTestSuite) TestNewPlan() {
stepTwo := NewMockStep(ctrl)

origHelp := help
help = func(cfg Config) []Step {
help = func(cfg env.Config) []Step {
return []Step{stepOne, stepTwo}
}
defer func() { help = origHelp }()

stdout := strings.Builder{}
stderr := strings.Builder{}
cfg := Config{
cfg := env.Config{
Command: "help",
Debug: false,
Namespace: "outer",
Expand Down Expand Up @@ -65,12 +66,12 @@ func (suite *PlanTestSuite) TestNewPlanAbortsOnError() {
stepTwo := NewMockStep(ctrl)

origHelp := help
help = func(cfg Config) []Step {
help = func(cfg env.Config) []Step {
return []Step{stepOne, stepTwo}
}
defer func() { help = origHelp }()

cfg := Config{
cfg := env.Config{
Command: "help",
}

Expand Down Expand Up @@ -129,7 +130,7 @@ func (suite *PlanTestSuite) TestExecuteAbortsOnError() {
}

func (suite *PlanTestSuite) TestUpgrade() {
cfg := Config{
cfg := env.Config{
ChartVersion: "seventeen",
DryRun: true,
Wait: true,
Expand Down Expand Up @@ -172,7 +173,7 @@ func (suite *PlanTestSuite) TestUpgrade() {
}

func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
}
steps := upgrade(cfg)
Expand All @@ -182,7 +183,7 @@ func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() {
}

func (suite *PlanTestSuite) TestUpgradeWithAddRepos() {
cfg := Config{
cfg := env.Config{
AddRepos: []string{
"machine=https://github.com/harold_finch/themachine",
},
Expand All @@ -193,7 +194,7 @@ func (suite *PlanTestSuite) TestUpgradeWithAddRepos() {
}

func (suite *PlanTestSuite) TestUninstall() {
cfg := Config{
cfg := env.Config{
KubeToken: "b2YgbXkgYWZmZWN0aW9u",
SkipTLSVerify: true,
Certificate: "cHJvY2xhaW1zIHdvbmRlcmZ1bCBmcmllbmRzaGlw",
Expand Down Expand Up @@ -233,7 +234,7 @@ func (suite *PlanTestSuite) TestUninstall() {
}

func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
}
steps := uninstall(cfg)
Expand All @@ -243,7 +244,7 @@ func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
}

func (suite *PlanTestSuite) TestInitKube() {
cfg := Config{
cfg := env.Config{
KubeToken: "cXVlZXIgY2hhcmFjdGVyCg==",
SkipTLSVerify: true,
Certificate: "b2Ygd29rZW5lc3MK",
Expand All @@ -269,7 +270,7 @@ func (suite *PlanTestSuite) TestInitKube() {
}

func (suite *PlanTestSuite) TestDepUpdate() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
Chart: "scatterplot",
}
Expand All @@ -286,7 +287,7 @@ func (suite *PlanTestSuite) TestDepUpdate() {
}

func (suite *PlanTestSuite) TestAddRepos() {
cfg := Config{
cfg := env.Config{
AddRepos: []string{
"first=https://add.repos/one",
"second=https://add.repos/two",
Expand All @@ -304,7 +305,7 @@ func (suite *PlanTestSuite) TestAddRepos() {
}

func (suite *PlanTestSuite) TestLint() {
cfg := Config{
cfg := env.Config{
Chart: "./flow",
Values: "steadfastness,forthrightness",
StringValues: "tensile_strength,flexibility",
Expand All @@ -326,7 +327,7 @@ func (suite *PlanTestSuite) TestLint() {
}

func (suite *PlanTestSuite) TestLintWithUpdateDependencies() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
}
steps := lint(cfg)
Expand All @@ -335,7 +336,7 @@ func (suite *PlanTestSuite) TestLintWithUpdateDependencies() {
}

func (suite *PlanTestSuite) TestLintWithAddRepos() {
cfg := Config{
cfg := env.Config{
AddRepos: []string{"friendczar=https://github.com/logan_pierce/friendczar"},
}
steps := lint(cfg)
Expand All @@ -344,15 +345,15 @@ func (suite *PlanTestSuite) TestLintWithAddRepos() {
}

func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
cfg := Config{
cfg := env.Config{
Command: "upgrade",
}
stepsMaker := determineSteps(cfg)
suite.Same(&upgrade, stepsMaker)
}

func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
cfg := Config{}
cfg := env.Config{}

upgradeEvents := []string{"push", "tag", "deployment", "pull_request", "promote", "rollback"}
for _, event := range upgradeEvents {
Expand All @@ -363,7 +364,7 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
}

func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() {
cfg := Config{
cfg := env.Config{
Command: "uninstall",
}
stepsMaker := determineSteps(cfg)
Expand All @@ -372,23 +373,23 @@ func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() {

// helm_command = delete is provided as an alias for backward-compatibility with drone-helm
func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
cfg := Config{
cfg := env.Config{
Command: "delete",
}
stepsMaker := determineSteps(cfg)
suite.Same(&uninstall, stepsMaker)
}

func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() {
cfg := Config{
cfg := env.Config{
DroneEvent: "delete",
}
stepsMaker := determineSteps(cfg)
suite.Same(&uninstall, stepsMaker)
}

func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
cfg := Config{
cfg := env.Config{
Command: "lint",
}

Expand All @@ -397,7 +398,7 @@ func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
}

func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
cfg := Config{
cfg := env.Config{
Command: "help",
}

Expand Down

0 comments on commit 16117ee

Please sign in to comment.