Skip to content

Commit 48be1fb

Browse files
committed
move EnvLookup back
1 parent b547309 commit 48be1fb

File tree

5 files changed

+89
-96
lines changed

5 files changed

+89
-96
lines changed

cliext/config.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
package cliext
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
48
"go.temporal.io/sdk/contrib/envconfig"
59
)
610

7-
// LoadOptions contains options for loading configuration.
8-
type LoadOptions struct {
9-
// ConfigFilePath is the path to the configuration file.
10-
// If empty, the default path will be used.
11-
ConfigFilePath string
12-
13-
// EnvLookup is used to look up environment variables.
14-
// If nil, EnvLookupOS will be used.
15-
EnvLookup EnvLookup
16-
}
17-
1811
// LoadConfig loads the client configuration from the specified file or default location.
19-
func LoadConfig(opts LoadOptions) (*envconfig.ClientConfig, error) {
20-
envLookup := opts.EnvLookup
21-
if envLookup == nil {
22-
envLookup = EnvLookupOS
23-
}
24-
12+
// If configFilePath is empty, the default path will be used.
13+
func LoadConfig(configFilePath string) (*envconfig.ClientConfig, error) {
2514
clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{
26-
ConfigFilePath: opts.ConfigFilePath,
27-
EnvLookup: envLookup,
15+
ConfigFilePath: configFilePath,
2816
})
2917
if err != nil {
3018
return nil, err
3119
}
3220
return &clientConfig, nil
3321
}
22+
23+
// WriteConfig writes the configuration to the specified file or default location.
24+
// If configFilePath is empty, the default path will be used.
25+
func WriteConfig(config *envconfig.ClientConfig, configFilePath string) error {
26+
// Get file
27+
if configFilePath == "" {
28+
var err error
29+
if configFilePath, err = envconfig.DefaultConfigFilePath(); err != nil {
30+
return err
31+
}
32+
}
33+
34+
// Convert to TOML
35+
b, err := config.ToTOML(envconfig.ClientConfigToTOMLOptions{})
36+
if err != nil {
37+
return fmt.Errorf("failed building TOML: %w", err)
38+
}
39+
40+
// Write to file, making dirs as needed
41+
if err := os.MkdirAll(filepath.Dir(configFilePath), 0700); err != nil {
42+
return fmt.Errorf("failed making config file parent dirs: %w", err)
43+
}
44+
if err := os.WriteFile(configFilePath, b, 0600); err != nil {
45+
return fmt.Errorf("failed writing config file: %w", err)
46+
}
47+
return nil
48+
}

cliext/profile.go

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@ package cliext
22

33
import (
44
"fmt"
5-
"os"
6-
"path/filepath"
75
"reflect"
86
"strings"
97

108
"go.temporal.io/sdk/contrib/envconfig"
119
)
1210

13-
var envConfigPropsToFieldNames = map[string]string{
11+
// LoadProfileOptions contains options for loading a profile.
12+
type LoadProfileOptions struct {
13+
// ConfigFilePath is the path to the configuration file.
14+
// If empty, the default path will be used.
15+
ConfigFilePath string
16+
17+
// ProfileName is the name of the profile to load.
18+
ProfileName string
19+
20+
// CreateIfMissing creates an empty profile if it doesn't exist.
21+
CreateIfMissing bool
22+
}
23+
24+
var envProfilePropsToFieldNames = map[string]string{
1425
"address": "Address",
1526
"namespace": "Namespace",
1627
"api_key": "APIKey",
@@ -29,60 +40,23 @@ var envConfigPropsToFieldNames = map[string]string{
2940
}
3041

3142
// LoadProfile loads a specific profile from the configuration.
32-
// If createIfMissing is true and the profile doesn't exist, an empty profile is created.
33-
// If createIfMissing is false and the profile doesn't exist, an error is returned.
34-
func LoadProfile(opts LoadOptions, profileName string, createIfMissing bool) (*envconfig.ClientConfig, *envconfig.ClientConfigProfile, error) {
35-
config, err := LoadConfig(opts)
43+
func LoadProfile(opts LoadProfileOptions) (*envconfig.ClientConfig, *envconfig.ClientConfigProfile, error) {
44+
config, err := LoadConfig(opts.ConfigFilePath)
3645
if err != nil {
3746
return nil, nil, err
3847
}
3948

40-
// Load profile
41-
profile := config.Profiles[profileName]
49+
profile := config.Profiles[opts.ProfileName]
4250
if profile == nil {
43-
if !createIfMissing {
44-
return nil, nil, fmt.Errorf("profile %q not found", profileName)
51+
if !opts.CreateIfMissing {
52+
return nil, nil, fmt.Errorf("profile %q not found", opts.ProfileName)
4553
}
4654
profile = &envconfig.ClientConfigProfile{}
47-
config.Profiles[profileName] = profile
55+
config.Profiles[opts.ProfileName] = profile
4856
}
4957
return config, profile, nil
5058
}
5159

52-
// WriteConfig writes the configuration to the specified file or default location.
53-
func WriteConfig(config *envconfig.ClientConfig, opts LoadOptions) error {
54-
// Get file
55-
configFile := opts.ConfigFilePath
56-
if configFile == "" {
57-
envLookup := opts.EnvLookup
58-
if envLookup == nil {
59-
envLookup = EnvLookupOS
60-
}
61-
configFile, _ = envLookup.LookupEnv("TEMPORAL_CONFIG_FILE")
62-
if configFile == "" {
63-
var err error
64-
if configFile, err = envconfig.DefaultConfigFilePath(); err != nil {
65-
return err
66-
}
67-
}
68-
}
69-
70-
// Convert to TOML
71-
b, err := config.ToTOML(envconfig.ClientConfigToTOMLOptions{})
72-
if err != nil {
73-
return fmt.Errorf("failed building TOML: %w", err)
74-
}
75-
76-
// Write to file, making dirs as needed
77-
if err := os.MkdirAll(filepath.Dir(configFile), 0700); err != nil {
78-
return fmt.Errorf("failed making config file parent dirs: %w", err)
79-
}
80-
if err := os.WriteFile(configFile, b, 0600); err != nil {
81-
return fmt.Errorf("failed writing config file: %w", err)
82-
}
83-
return nil
84-
}
85-
8660
// GetPropertyValue gets a property value from a profile by property name.
8761
// For pointer types (like TLS), returns whether the pointer is non-nil.
8862
func GetPropertyValue(profile *envconfig.ClientConfigProfile, prop string) (any, error) {
@@ -184,7 +158,7 @@ func ListProperties(profile *envconfig.ClientConfigProfile) (map[string]any, err
184158
// Get every property individually as a property-value pair except zero vals
185159
props := make(map[string]any)
186160

187-
for k := range envConfigPropsToFieldNames {
161+
for k := range envProfilePropsToFieldNames {
188162
// TLS is a special case
189163
if k == "tls" {
190164
if profile.TLS != nil {
@@ -215,7 +189,7 @@ func getReflectValue(
215189
failIfParentNotFound bool,
216190
) (reflect.Value, error) {
217191
// Get field name
218-
field := envConfigPropsToFieldNames[prop]
192+
field := envProfilePropsToFieldNames[prop]
219193
if field == "" {
220194
return reflect.Value{}, fmt.Errorf("unknown property %q", prop)
221195
}

internal/temporalcli/commands.config.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import (
1111
)
1212

1313
func (c *TemporalConfigDeleteCommand) run(cctx *CommandContext, _ []string) error {
14-
opts := loadOptsFromContext(cctx)
15-
profileName := envConfigProfileName(cctx)
16-
conf, confProfile, err := cliext.LoadProfile(opts, profileName, false)
14+
opts := loadProfileOptsFromContext(cctx, false)
15+
conf, confProfile, err := cliext.LoadProfile(opts)
1716
if err != nil {
1817
return err
1918
}
@@ -23,13 +22,12 @@ func (c *TemporalConfigDeleteCommand) run(cctx *CommandContext, _ []string) erro
2322
}
2423

2524
cctx.Logger.Info("Writing config file", "file", opts.ConfigFilePath)
26-
return cliext.WriteConfig(conf, opts)
25+
return cliext.WriteConfig(conf, opts.ConfigFilePath)
2726
}
2827

2928
func (c *TemporalConfigDeleteProfileCommand) run(cctx *CommandContext, _ []string) error {
30-
opts := loadOptsFromContext(cctx)
31-
profileName := envConfigProfileName(cctx)
32-
conf, _, err := cliext.LoadProfile(opts, profileName, false)
29+
opts := loadProfileOptsFromContext(cctx, false)
30+
conf, _, err := cliext.LoadProfile(opts)
3331
if err != nil {
3432
return err
3533
}
@@ -39,16 +37,15 @@ func (c *TemporalConfigDeleteProfileCommand) run(cctx *CommandContext, _ []strin
3937
if cctx.RootCommand.Profile == "" {
4038
return fmt.Errorf("to delete an entire profile, --profile must be provided explicitly")
4139
}
42-
delete(conf.Profiles, profileName)
40+
delete(conf.Profiles, opts.ProfileName)
4341

4442
cctx.Logger.Info("Writing config file", "file", opts.ConfigFilePath)
45-
return cliext.WriteConfig(conf, opts)
43+
return cliext.WriteConfig(conf, opts.ConfigFilePath)
4644
}
4745

4846
func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error {
49-
opts := loadOptsFromContext(cctx)
50-
profileName := envConfigProfileName(cctx)
51-
conf, confProfile, err := cliext.LoadProfile(opts, profileName, false)
47+
opts := loadProfileOptsFromContext(cctx, false)
48+
conf, confProfile, err := cliext.LoadProfile(opts)
5249
if err != nil {
5350
return err
5451
}
@@ -82,7 +79,7 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error {
8279
} else if err := toml.Unmarshal(b, &tomlConf); err != nil {
8380
return fmt.Errorf("failed converting from TOML: %w", err)
8481
}
85-
return cctx.Printer.PrintStructured(tomlConf.Profiles[profileName], printer.StructuredOptions{})
82+
return cctx.Printer.PrintStructured(tomlConf.Profiles[opts.ProfileName], printer.StructuredOptions{})
8683
} else {
8784
// Get every property individually as a property-value pair except zero vals
8885
propsMap, err := cliext.ListProperties(confProfile)
@@ -101,8 +98,8 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error {
10198
}
10299

103100
func (c *TemporalConfigListCommand) run(cctx *CommandContext, _ []string) error {
104-
opts := loadOptsFromContext(cctx)
105-
config, err := cliext.LoadConfig(opts)
101+
configFilePath := configFilePathFromContext(cctx)
102+
config, err := cliext.LoadConfig(configFilePath)
106103
if err != nil {
107104
return err
108105
}
@@ -118,9 +115,8 @@ func (c *TemporalConfigListCommand) run(cctx *CommandContext, _ []string) error
118115
}
119116

120117
func (c *TemporalConfigSetCommand) run(cctx *CommandContext, _ []string) error {
121-
opts := loadOptsFromContext(cctx)
122-
profileName := envConfigProfileName(cctx)
123-
conf, confProfile, err := cliext.LoadProfile(opts, profileName, true)
118+
opts := loadProfileOptsFromContext(cctx, true)
119+
conf, confProfile, err := cliext.LoadProfile(opts)
124120
if err != nil {
125121
return err
126122
}
@@ -130,10 +126,18 @@ func (c *TemporalConfigSetCommand) run(cctx *CommandContext, _ []string) error {
130126
}
131127

132128
cctx.Logger.Info("Writing config file", "file", opts.ConfigFilePath)
133-
return cliext.WriteConfig(conf, opts)
129+
return cliext.WriteConfig(conf, opts.ConfigFilePath)
134130
}
135131

136-
func envConfigProfileName(cctx *CommandContext) string {
132+
func configFilePathFromContext(cctx *CommandContext) string {
133+
if cctx.RootCommand.ConfigFile != "" {
134+
return cctx.RootCommand.ConfigFile
135+
}
136+
configFilePath, _ := cctx.Options.EnvLookup.LookupEnv("TEMPORAL_CONFIG_FILE")
137+
return configFilePath
138+
}
139+
140+
func profileNameFromContext(cctx *CommandContext) string {
137141
if cctx.RootCommand.Profile != "" {
138142
return cctx.RootCommand.Profile
139143
} else if p, _ := cctx.Options.EnvLookup.LookupEnv("TEMPORAL_PROFILE"); p != "" {
@@ -142,9 +146,10 @@ func envConfigProfileName(cctx *CommandContext) string {
142146
return envconfig.DefaultConfigFileProfile
143147
}
144148

145-
func loadOptsFromContext(cctx *CommandContext) cliext.LoadOptions {
146-
return cliext.LoadOptions{
147-
ConfigFilePath: cctx.RootCommand.ConfigFile,
148-
EnvLookup: cctx.Options.EnvLookup,
149+
func loadProfileOptsFromContext(cctx *CommandContext, createIfMissing bool) cliext.LoadProfileOptions {
150+
return cliext.LoadProfileOptions{
151+
ConfigFilePath: configFilePathFromContext(cctx),
152+
ProfileName: profileNameFromContext(cctx),
153+
CreateIfMissing: createIfMissing,
149154
}
150155
}

internal/temporalcli/commands.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/fatih/color"
2020
"github.com/spf13/cobra"
2121
"github.com/spf13/pflag"
22-
"github.com/temporalio/cli/cliext"
2322
"github.com/temporalio/cli/internal/printer"
2423
"github.com/temporalio/ui-server/v2/server/version"
2524
"go.temporal.io/api/common/v1"
@@ -66,8 +65,8 @@ type CommandOptions struct {
6665
Args []string
6766
// Deprecated `--env` and `--env-file` approach
6867
DeprecatedEnvConfig DeprecatedEnvConfig
69-
// If nil, [cliext.EnvLookupOS] is used.
70-
EnvLookup cliext.EnvLookup
68+
// If nil, [EnvLookupOS] is used.
69+
EnvLookup EnvLookup
7170

7271
// These three fields below default to OS values
7372
Stdin io.Reader
@@ -116,7 +115,7 @@ func (c *CommandContext) preprocessOptions() error {
116115
c.Options.Args = os.Args[1:]
117116
}
118117
if c.Options.EnvLookup == nil {
119-
c.Options.EnvLookup = cliext.EnvLookupOS
118+
c.Options.EnvLookup = EnvLookupOS
120119
}
121120

122121
if c.Options.Stdin == nil {

cliext/env.go renamed to internal/temporalcli/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cliext
1+
package temporalcli
22

33
import "os"
44

0 commit comments

Comments
 (0)