|
| 1 | +package cliext |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "go.temporal.io/sdk/contrib/envconfig" |
| 9 | +) |
| 10 | + |
| 11 | +// LoadConfigOptions contains options for loading configuration. |
| 12 | +type LoadConfigOptions struct { |
| 13 | + // ConfigFilePath is the path to the configuration file. |
| 14 | + // If empty, TEMPORAL_CONFIG_FILE env var is checked, then the default path is used. |
| 15 | + ConfigFilePath string |
| 16 | + |
| 17 | + // EnvLookup is used for environment variable lookups. |
| 18 | + // If nil, os.LookupEnv is used. |
| 19 | + EnvLookup EnvLookup |
| 20 | +} |
| 21 | + |
| 22 | +// LoadConfigResult contains the result of loading configuration. |
| 23 | +type LoadConfigResult struct { |
| 24 | + // Config is the loaded configuration. |
| 25 | + Config *envconfig.ClientConfig |
| 26 | + // ConfigFilePath is the resolved path to the configuration file that was loaded. |
| 27 | + // This may differ from the input if TEMPORAL_CONFIG_FILE env var was used. |
| 28 | + ConfigFilePath string |
| 29 | +} |
| 30 | + |
| 31 | +// LoadConfig loads the client configuration from the specified file or default location. |
| 32 | +// If ConfigFilePath is empty, the TEMPORAL_CONFIG_FILE environment variable is checked. |
| 33 | +func LoadConfig(options LoadConfigOptions) (LoadConfigResult, error) { |
| 34 | + envLookup := options.EnvLookup |
| 35 | + if envLookup == nil { |
| 36 | + envLookup = EnvLookupOS |
| 37 | + } |
| 38 | + configFilePath := options.ConfigFilePath |
| 39 | + if configFilePath == "" { |
| 40 | + configFilePath, _ = envLookup.LookupEnv("TEMPORAL_CONFIG_FILE") |
| 41 | + } |
| 42 | + clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{ |
| 43 | + ConfigFilePath: configFilePath, |
| 44 | + EnvLookup: envLookup, |
| 45 | + }) |
| 46 | + if err != nil { |
| 47 | + return LoadConfigResult{}, err |
| 48 | + } |
| 49 | + return LoadConfigResult{ |
| 50 | + Config: &clientConfig, |
| 51 | + ConfigFilePath: configFilePath, |
| 52 | + }, nil |
| 53 | +} |
| 54 | + |
| 55 | +// WriteConfig writes the configuration to the specified file or default location. |
| 56 | +// If configFilePath is empty, the default path will be used. |
| 57 | +func WriteConfig(config *envconfig.ClientConfig, configFilePath string) error { |
| 58 | + // Get file |
| 59 | + if configFilePath == "" { |
| 60 | + var err error |
| 61 | + if configFilePath, err = envconfig.DefaultConfigFilePath(); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Convert to TOML |
| 67 | + b, err := config.ToTOML(envconfig.ClientConfigToTOMLOptions{}) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed building TOML: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Write to file, making dirs as needed |
| 73 | + if err := os.MkdirAll(filepath.Dir(configFilePath), 0700); err != nil { |
| 74 | + return fmt.Errorf("failed making config file parent dirs: %w", err) |
| 75 | + } |
| 76 | + if err := os.WriteFile(configFilePath, b, 0600); err != nil { |
| 77 | + return fmt.Errorf("failed writing config file: %w", err) |
| 78 | + } |
| 79 | + return nil |
| 80 | +} |
0 commit comments