Put the Config in a new env package [#67]

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.
This commit is contained in:
Erin Call 2020-01-14 10:32:20 -08:00
parent 8a9cf23ab9
commit 16117eea2f
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
5 changed files with 38 additions and 35 deletions

View file

@ -5,11 +5,12 @@ import (
"os" "os"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/helm" "github.com/pelotech/drone-helm3/internal/helm"
) )
func main() { func main() {
cfg, err := helm.NewConfig(os.Stdout, os.Stderr) cfg, err := env.NewConfig(os.Stdout, os.Stderr)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) fmt.Fprintf(os.Stderr, "%s\n", err.Error())

View file

@ -1,4 +1,4 @@
package helm package env
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package helm package env
import ( import (
"fmt" "fmt"

View file

@ -2,6 +2,7 @@ package helm
import ( import (
"fmt" "fmt"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/run" "github.com/pelotech/drone-helm3/internal/run"
"os" "os"
) )
@ -20,12 +21,12 @@ type Step interface {
// A Plan is a series of steps to perform. // A Plan is a series of steps to perform.
type Plan struct { type Plan struct {
steps []Step steps []Step
cfg Config cfg env.Config
runCfg run.Config runCfg run.Config
} }
// NewPlan makes a plan for running a helm operation. // NewPlan makes a plan for running a helm operation.
func NewPlan(cfg Config) (*Plan, error) { func NewPlan(cfg env.Config) (*Plan, error) {
p := Plan{ p := Plan{
cfg: cfg, cfg: cfg,
runCfg: run.Config{ runCfg: run.Config{
@ -54,7 +55,7 @@ func NewPlan(cfg Config) (*Plan, error) {
// determineSteps is primarily for the tests' convenience: it allows testing the "which stuff should // 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. // 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 { switch cfg.Command {
case "upgrade": case "upgrade":
return &upgrade return &upgrade
@ -91,7 +92,7 @@ func (p *Plan) Execute() error {
return nil return nil
} }
var upgrade = func(cfg Config) []Step { var upgrade = func(cfg env.Config) []Step {
steps := initKube(cfg) steps := initKube(cfg)
steps = append(steps, addRepos(cfg)...) steps = append(steps, addRepos(cfg)...)
if cfg.UpdateDependencies { if cfg.UpdateDependencies {
@ -116,7 +117,7 @@ var upgrade = func(cfg Config) []Step {
return steps return steps
} }
var uninstall = func(cfg Config) []Step { var uninstall = func(cfg env.Config) []Step {
steps := initKube(cfg) steps := initKube(cfg)
if cfg.UpdateDependencies { if cfg.UpdateDependencies {
steps = append(steps, depUpdate(cfg)...) steps = append(steps, depUpdate(cfg)...)
@ -130,7 +131,7 @@ var uninstall = func(cfg Config) []Step {
return steps return steps
} }
var lint = func(cfg Config) []Step { var lint = func(cfg env.Config) []Step {
steps := addRepos(cfg) steps := addRepos(cfg)
if cfg.UpdateDependencies { if cfg.UpdateDependencies {
steps = append(steps, depUpdate(cfg)...) steps = append(steps, depUpdate(cfg)...)
@ -146,14 +147,14 @@ var lint = func(cfg Config) []Step {
return steps return steps
} }
var help = func(cfg Config) []Step { var help = func(cfg env.Config) []Step {
help := &run.Help{ help := &run.Help{
HelmCommand: cfg.Command, HelmCommand: cfg.Command,
} }
return []Step{help} return []Step{help}
} }
func initKube(cfg Config) []Step { func initKube(cfg env.Config) []Step {
return []Step{ return []Step{
&run.InitKube{ &run.InitKube{
SkipTLSVerify: cfg.SkipTLSVerify, SkipTLSVerify: cfg.SkipTLSVerify,
@ -167,7 +168,7 @@ func initKube(cfg Config) []Step {
} }
} }
func addRepos(cfg Config) []Step { func addRepos(cfg env.Config) []Step {
steps := make([]Step, 0) steps := make([]Step, 0)
for _, repo := range cfg.AddRepos { for _, repo := range cfg.AddRepos {
steps = append(steps, &run.AddRepo{ steps = append(steps, &run.AddRepo{
@ -178,7 +179,7 @@ func addRepos(cfg Config) []Step {
return steps return steps
} }
func depUpdate(cfg Config) []Step { func depUpdate(cfg env.Config) []Step {
return []Step{ return []Step{
&run.DepUpdate{ &run.DepUpdate{
Chart: cfg.Chart, Chart: cfg.Chart,

View file

@ -7,6 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/run" "github.com/pelotech/drone-helm3/internal/run"
) )
@ -25,14 +26,14 @@ func (suite *PlanTestSuite) TestNewPlan() {
stepTwo := NewMockStep(ctrl) stepTwo := NewMockStep(ctrl)
origHelp := help origHelp := help
help = func(cfg Config) []Step { help = func(cfg env.Config) []Step {
return []Step{stepOne, stepTwo} return []Step{stepOne, stepTwo}
} }
defer func() { help = origHelp }() defer func() { help = origHelp }()
stdout := strings.Builder{} stdout := strings.Builder{}
stderr := strings.Builder{} stderr := strings.Builder{}
cfg := Config{ cfg := env.Config{
Command: "help", Command: "help",
Debug: false, Debug: false,
Namespace: "outer", Namespace: "outer",
@ -65,12 +66,12 @@ func (suite *PlanTestSuite) TestNewPlanAbortsOnError() {
stepTwo := NewMockStep(ctrl) stepTwo := NewMockStep(ctrl)
origHelp := help origHelp := help
help = func(cfg Config) []Step { help = func(cfg env.Config) []Step {
return []Step{stepOne, stepTwo} return []Step{stepOne, stepTwo}
} }
defer func() { help = origHelp }() defer func() { help = origHelp }()
cfg := Config{ cfg := env.Config{
Command: "help", Command: "help",
} }
@ -129,7 +130,7 @@ func (suite *PlanTestSuite) TestExecuteAbortsOnError() {
} }
func (suite *PlanTestSuite) TestUpgrade() { func (suite *PlanTestSuite) TestUpgrade() {
cfg := Config{ cfg := env.Config{
ChartVersion: "seventeen", ChartVersion: "seventeen",
DryRun: true, DryRun: true,
Wait: true, Wait: true,
@ -172,7 +173,7 @@ func (suite *PlanTestSuite) TestUpgrade() {
} }
func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() { func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() {
cfg := Config{ cfg := env.Config{
UpdateDependencies: true, UpdateDependencies: true,
} }
steps := upgrade(cfg) steps := upgrade(cfg)
@ -182,7 +183,7 @@ func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() {
} }
func (suite *PlanTestSuite) TestUpgradeWithAddRepos() { func (suite *PlanTestSuite) TestUpgradeWithAddRepos() {
cfg := Config{ cfg := env.Config{
AddRepos: []string{ AddRepos: []string{
"machine=https://github.com/harold_finch/themachine", "machine=https://github.com/harold_finch/themachine",
}, },
@ -193,7 +194,7 @@ func (suite *PlanTestSuite) TestUpgradeWithAddRepos() {
} }
func (suite *PlanTestSuite) TestUninstall() { func (suite *PlanTestSuite) TestUninstall() {
cfg := Config{ cfg := env.Config{
KubeToken: "b2YgbXkgYWZmZWN0aW9u", KubeToken: "b2YgbXkgYWZmZWN0aW9u",
SkipTLSVerify: true, SkipTLSVerify: true,
Certificate: "cHJvY2xhaW1zIHdvbmRlcmZ1bCBmcmllbmRzaGlw", Certificate: "cHJvY2xhaW1zIHdvbmRlcmZ1bCBmcmllbmRzaGlw",
@ -233,7 +234,7 @@ func (suite *PlanTestSuite) TestUninstall() {
} }
func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() { func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
cfg := Config{ cfg := env.Config{
UpdateDependencies: true, UpdateDependencies: true,
} }
steps := uninstall(cfg) steps := uninstall(cfg)
@ -243,7 +244,7 @@ func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
} }
func (suite *PlanTestSuite) TestInitKube() { func (suite *PlanTestSuite) TestInitKube() {
cfg := Config{ cfg := env.Config{
KubeToken: "cXVlZXIgY2hhcmFjdGVyCg==", KubeToken: "cXVlZXIgY2hhcmFjdGVyCg==",
SkipTLSVerify: true, SkipTLSVerify: true,
Certificate: "b2Ygd29rZW5lc3MK", Certificate: "b2Ygd29rZW5lc3MK",
@ -269,7 +270,7 @@ func (suite *PlanTestSuite) TestInitKube() {
} }
func (suite *PlanTestSuite) TestDepUpdate() { func (suite *PlanTestSuite) TestDepUpdate() {
cfg := Config{ cfg := env.Config{
UpdateDependencies: true, UpdateDependencies: true,
Chart: "scatterplot", Chart: "scatterplot",
} }
@ -286,7 +287,7 @@ func (suite *PlanTestSuite) TestDepUpdate() {
} }
func (suite *PlanTestSuite) TestAddRepos() { func (suite *PlanTestSuite) TestAddRepos() {
cfg := Config{ cfg := env.Config{
AddRepos: []string{ AddRepos: []string{
"first=https://add.repos/one", "first=https://add.repos/one",
"second=https://add.repos/two", "second=https://add.repos/two",
@ -304,7 +305,7 @@ func (suite *PlanTestSuite) TestAddRepos() {
} }
func (suite *PlanTestSuite) TestLint() { func (suite *PlanTestSuite) TestLint() {
cfg := Config{ cfg := env.Config{
Chart: "./flow", Chart: "./flow",
Values: "steadfastness,forthrightness", Values: "steadfastness,forthrightness",
StringValues: "tensile_strength,flexibility", StringValues: "tensile_strength,flexibility",
@ -326,7 +327,7 @@ func (suite *PlanTestSuite) TestLint() {
} }
func (suite *PlanTestSuite) TestLintWithUpdateDependencies() { func (suite *PlanTestSuite) TestLintWithUpdateDependencies() {
cfg := Config{ cfg := env.Config{
UpdateDependencies: true, UpdateDependencies: true,
} }
steps := lint(cfg) steps := lint(cfg)
@ -335,7 +336,7 @@ func (suite *PlanTestSuite) TestLintWithUpdateDependencies() {
} }
func (suite *PlanTestSuite) TestLintWithAddRepos() { func (suite *PlanTestSuite) TestLintWithAddRepos() {
cfg := Config{ cfg := env.Config{
AddRepos: []string{"friendczar=https://github.com/logan_pierce/friendczar"}, AddRepos: []string{"friendczar=https://github.com/logan_pierce/friendczar"},
} }
steps := lint(cfg) steps := lint(cfg)
@ -344,7 +345,7 @@ func (suite *PlanTestSuite) TestLintWithAddRepos() {
} }
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() { func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
cfg := Config{ cfg := env.Config{
Command: "upgrade", Command: "upgrade",
} }
stepsMaker := determineSteps(cfg) stepsMaker := determineSteps(cfg)
@ -352,7 +353,7 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
} }
func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() { func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
cfg := Config{} cfg := env.Config{}
upgradeEvents := []string{"push", "tag", "deployment", "pull_request", "promote", "rollback"} upgradeEvents := []string{"push", "tag", "deployment", "pull_request", "promote", "rollback"}
for _, event := range upgradeEvents { for _, event := range upgradeEvents {
@ -363,7 +364,7 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
} }
func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() { func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() {
cfg := Config{ cfg := env.Config{
Command: "uninstall", Command: "uninstall",
} }
stepsMaker := determineSteps(cfg) stepsMaker := determineSteps(cfg)
@ -372,7 +373,7 @@ func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() {
// helm_command = delete is provided as an alias for backward-compatibility with drone-helm // helm_command = delete is provided as an alias for backward-compatibility with drone-helm
func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() { func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
cfg := Config{ cfg := env.Config{
Command: "delete", Command: "delete",
} }
stepsMaker := determineSteps(cfg) stepsMaker := determineSteps(cfg)
@ -380,7 +381,7 @@ func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
} }
func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() { func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() {
cfg := Config{ cfg := env.Config{
DroneEvent: "delete", DroneEvent: "delete",
} }
stepsMaker := determineSteps(cfg) stepsMaker := determineSteps(cfg)
@ -388,7 +389,7 @@ func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() {
} }
func (suite *PlanTestSuite) TestDeterminePlanLintCommand() { func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
cfg := Config{ cfg := env.Config{
Command: "lint", Command: "lint",
} }
@ -397,7 +398,7 @@ func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
} }
func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() { func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
cfg := Config{ cfg := env.Config{
Command: "help", Command: "help",
} }