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"
_ "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())

View file

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

View file

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

View file

@ -2,6 +2,7 @@ package helm
import (
"fmt"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/run"
"os"
)
@ -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{
@ -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
@ -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 {
@ -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)...)
@ -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)...)
@ -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,
@ -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{
@ -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,

View file

@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/pelotech/drone-helm3/internal/env"
"github.com/pelotech/drone-helm3/internal/run"
)
@ -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",
@ -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",
}
@ -129,7 +130,7 @@ func (suite *PlanTestSuite) TestExecuteAbortsOnError() {
}
func (suite *PlanTestSuite) TestUpgrade() {
cfg := Config{
cfg := env.Config{
ChartVersion: "seventeen",
DryRun: true,
Wait: true,
@ -172,7 +173,7 @@ func (suite *PlanTestSuite) TestUpgrade() {
}
func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
}
steps := upgrade(cfg)
@ -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",
},
@ -193,7 +194,7 @@ func (suite *PlanTestSuite) TestUpgradeWithAddRepos() {
}
func (suite *PlanTestSuite) TestUninstall() {
cfg := Config{
cfg := env.Config{
KubeToken: "b2YgbXkgYWZmZWN0aW9u",
SkipTLSVerify: true,
Certificate: "cHJvY2xhaW1zIHdvbmRlcmZ1bCBmcmllbmRzaGlw",
@ -233,7 +234,7 @@ func (suite *PlanTestSuite) TestUninstall() {
}
func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
}
steps := uninstall(cfg)
@ -243,7 +244,7 @@ func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
}
func (suite *PlanTestSuite) TestInitKube() {
cfg := Config{
cfg := env.Config{
KubeToken: "cXVlZXIgY2hhcmFjdGVyCg==",
SkipTLSVerify: true,
Certificate: "b2Ygd29rZW5lc3MK",
@ -269,7 +270,7 @@ func (suite *PlanTestSuite) TestInitKube() {
}
func (suite *PlanTestSuite) TestDepUpdate() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
Chart: "scatterplot",
}
@ -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",
@ -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",
@ -326,7 +327,7 @@ func (suite *PlanTestSuite) TestLint() {
}
func (suite *PlanTestSuite) TestLintWithUpdateDependencies() {
cfg := Config{
cfg := env.Config{
UpdateDependencies: true,
}
steps := lint(cfg)
@ -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)
@ -344,7 +345,7 @@ func (suite *PlanTestSuite) TestLintWithAddRepos() {
}
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
cfg := Config{
cfg := env.Config{
Command: "upgrade",
}
stepsMaker := determineSteps(cfg)
@ -352,7 +353,7 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
}
func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
cfg := Config{}
cfg := env.Config{}
upgradeEvents := []string{"push", "tag", "deployment", "pull_request", "promote", "rollback"}
for _, event := range upgradeEvents {
@ -363,7 +364,7 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
}
func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() {
cfg := Config{
cfg := env.Config{
Command: "uninstall",
}
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
func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
cfg := Config{
cfg := env.Config{
Command: "delete",
}
stepsMaker := determineSteps(cfg)
@ -380,7 +381,7 @@ func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
}
func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() {
cfg := Config{
cfg := env.Config{
DroneEvent: "delete",
}
stepsMaker := determineSteps(cfg)
@ -388,7 +389,7 @@ func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() {
}
func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
cfg := Config{
cfg := env.Config{
Command: "lint",
}
@ -397,7 +398,7 @@ func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
}
func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
cfg := Config{
cfg := env.Config{
Command: "help",
}