diff --git a/cmd/apply.go b/cmd/apply.go index bca92096..b6799e2e 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -27,6 +27,10 @@ func init() { log.Fatal().Err(err).Msg("Unable to initialize myks's globe") } + if !g.SingleEnv() { + log.Fatal().Msg("Local apply can only be used with a specific environment. Make sure you are connected to the right cluster.") + } + if err := g.Apply(asyncLevel); err != nil { log.Fatal().Err(err).Msg("Unable to apply k8s manifests") } diff --git a/internal/myks/globe.go b/internal/myks/globe.go index e442a75b..48dcce85 100644 --- a/internal/myks/globe.go +++ b/internal/myks/globe.go @@ -400,3 +400,11 @@ func (g *Globe) Msg(msg string) string { formattedMessage := fmt.Sprintf(GlobalLogFormat, msg) return formattedMessage } + +func (g *Globe) SingleEnv() bool { + if g.environments == nil { + log.Error().Msg("Invoking SingleEnv() before Init()") + return false + } + return len(g.environments) == 1 +} diff --git a/internal/myks/globe_test.go b/internal/myks/globe_test.go new file mode 100644 index 00000000..e3db8aad --- /dev/null +++ b/internal/myks/globe_test.go @@ -0,0 +1,28 @@ +package myks + +import "testing" + +func TestGlobe_SingleEnv(t *testing.T) { + type fields struct { + environments map[string]*Environment + } + tests := []struct { + name string + fields fields + want bool + }{ + {"before init", fields{map[string]*Environment{}}, false}, + {"happy path", fields{map[string]*Environment{"test-env": {}}}, true}, + {"sad path", fields{map[string]*Environment{"test-env": {}, "test-env2": {}}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := &Globe{ + environments: tt.fields.environments, + } + if got := g.SingleEnv(); got != tt.want { + t.Errorf("SingleEnv() = %v, want %v", got, tt.want) + } + }) + } +}