diff --git a/app/env_test.go b/app/env_test.go deleted file mode 100644 index 6791f3a1..00000000 --- a/app/env_test.go +++ /dev/null @@ -1,89 +0,0 @@ -package app_test - -import ( - "log" - "os" - "path/filepath" - - "github.com/urfave/cli/v2" -) - -const ( - testEnvName = "tctl-test-env" -) - -func (s *cliAppSuite) TestSetEnvValue() { - defer setupConfig(s.app)() - - err := s.app.Run([]string{"", "env", "set", testEnvName + ".address", "0.0.0.0:00000"}) - s.NoError(err) - - config := readConfig() - s.Contains(config, "tctl-test-env:") - s.Contains(config, "address: 0.0.0.0:00000") - s.Contains(config, "namespace: tctl-test-namespace") -} - -func (s *cliAppSuite) TestDeleteEnvProperty() { - defer setupConfig(s.app)() - - err := s.app.Run([]string{"", "env", "set", testEnvName + ".address", "1.2.3.4:5678"}) - s.NoError(err) - - err = s.app.Run([]string{"", "env", "delete", testEnvName + ".address"}) - s.NoError(err) - - config := readConfig() - s.Contains(config, "tctl-test-env:") - s.Contains(config, "namespace: tctl-test-namespace") - s.NotContains(config, "address: 1.2.3.4:5678") -} - -func (s *cliAppSuite) TestDeleteEnv() { - defer setupConfig(s.app)() - - err := s.app.Run([]string{"", "env", "set", testEnvName + ".address", "1.2.3.4:5678"}) - s.NoError(err) - - err = s.app.Run([]string{"", "env", "delete", testEnvName}) - s.NoError(err) - - config := readConfig() - s.NotContains(config, "tctl-test-env:") - s.NotContains(config, "namespace: tctl-test-namespace") - s.NotContains(config, "address: 1.2.3.4:5678") -} - -func setupConfig(app *cli.App) func() { - err := app.Run([]string{"", "env", "set", testEnvName + ".namespace", "tctl-test-namespace"}) - if err != nil { - log.Fatal(err) - } - - return func() { - err := app.Run([]string{"", "env", "delete", testEnvName}) - if err != nil { - log.Printf("unable to unset test env: %s", err) - } - } -} - -func readConfig() string { - path := getConfigPath() - content, err := os.ReadFile(path) - if err != nil { - log.Fatal(err) - } - return string(content) -} - -func getConfigPath() string { - dpath, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } - - path := filepath.Join(dpath, ".config", "temporalio", "temporal.yaml") - - return path -} diff --git a/env/env.go b/env/env.go index 8771f36b..162345b8 100644 --- a/env/env.go +++ b/env/env.go @@ -213,6 +213,13 @@ func loadEnv(ctx *cli.Context) error { return nil } + if env == config.DefaultEnv { + if envV := ClientConfig.Env(env); envV == nil { + // ignore if default env is not defined (ex. has been removed) + return nil + } + } + for _, flag := range cmd.Flags { name := flag.Names()[0] diff --git a/tests/env_test.go b/tests/env_test.go new file mode 100644 index 00000000..73192e56 --- /dev/null +++ b/tests/env_test.go @@ -0,0 +1,122 @@ +package tests + +import ( + "os" + "path/filepath" + + "github.com/temporalio/tctl-kit/pkg/config" + "github.com/urfave/cli/v2" +) + +const ( + testEnvName = "tctl-test-env" +) + +func (s *e2eSuite) TestSetEnvValue() { + testserver, app, _ := s.setUpTestEnvironment() + defer func() { + _ = testserver.Stop() + }() + + cleanup := setupConfig(s, app) + defer cleanup() + + err := app.Run([]string{"", "env", "set", testEnvName + ".address", "0.0.0.0:00000"}) + s.NoError(err) + + cfg := readConfig(s) + s.Contains(cfg, "tctl-test-env:") + s.Contains(cfg, "address: 0.0.0.0:00000") + s.Contains(cfg, "namespace: tctl-test-namespace") +} + +func (s *e2eSuite) TestDeleteEnvProperty() { + testserver, app, _ := s.setUpTestEnvironment() + defer func() { + _ = testserver.Stop() + }() + + cleanup := setupConfig(s, app) + defer cleanup() + + err := app.Run([]string{"", "env", "set", testEnvName + ".address", "1.2.3.4:5678"}) + s.NoError(err) + + err = app.Run([]string{"", "env", "delete", testEnvName + ".address"}) + s.NoError(err) + + cfg := readConfig(s) + s.Contains(cfg, "tctl-test-env:") + s.Contains(cfg, "namespace: tctl-test-namespace") + s.NotContains(cfg, "address: 1.2.3.4:5678") +} + +func (s *e2eSuite) TestDeleteEnv() { + testserver, app, _ := s.setUpTestEnvironment() + defer func() { + _ = testserver.Stop() + }() + + cleanup := setupConfig(s, app) + defer cleanup() + + err := app.Run([]string{"", "env", "set", testEnvName + ".address", "1.2.3.4:5678"}) + s.NoError(err) + + err = app.Run([]string{"", "env", "delete", testEnvName}) + s.NoError(err) + + cfg := readConfig(s) + s.NotContains(cfg, "tctl-test-env:") + s.NotContains(cfg, "namespace: tctl-test-namespace") + s.NotContains(cfg, "address: 1.2.3.4:5678") +} + +func (s *e2eSuite) TestDeleteEnv_Default() { + testserver, app, _ := s.setUpTestEnvironment() + defer func() { + _ = testserver.Stop() + }() + + cleanup := setupConfig(s, app) + defer cleanup() + + err := app.Run([]string{"", "env", "set", testEnvName + ".address", "1.2.3.4:5678"}) + s.NoError(err) + + err = app.Run([]string{"", "env", "delete", config.DefaultEnv}) + s.NoError(err) + + cfg := readConfig(s) + s.NotContains(cfg, "default:") + + err = app.Run([]string{"", "workflow", "list"}) + s.NoError(err) +} + +func setupConfig(s *e2eSuite, app *cli.App) func() { + err := app.Run([]string{"", "env", "set", testEnvName + ".namespace", "tctl-test-namespace"}) + s.NoError(err) + + return func() { + err := app.Run([]string{"", "env", "delete", testEnvName}) + s.NoError(err, "unable to unset test env") + } +} + +func readConfig(s *e2eSuite) string { + path := getConfigPath(s) + content, err := os.ReadFile(path) + s.NoError(err) + + return string(content) +} + +func getConfigPath(s *e2eSuite) string { + dpath, err := os.UserHomeDir() + s.NoError(err) + + path := filepath.Join(dpath, ".config", "temporalio", "temporal.yaml") + + return path +}