-
Notifications
You must be signed in to change notification settings - Fork 66
Extract profile helper #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
stephanos
wants to merge
1
commit into
temporalio:main
Choose a base branch
from
stephanos:auth-lib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package cliext | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "go.temporal.io/sdk/contrib/envconfig" | ||
| ) | ||
|
|
||
| // LoadConfigOptions contains options for loading configuration. | ||
| type LoadConfigOptions struct { | ||
| // ConfigFilePath is the path to the configuration file. | ||
| // If empty, TEMPORAL_CONFIG_FILE env var is checked, then the default path is used. | ||
| ConfigFilePath string | ||
|
|
||
| // EnvLookup is used for environment variable lookups. | ||
| // If nil, os.LookupEnv is used. | ||
| EnvLookup EnvLookup | ||
| } | ||
|
|
||
| // LoadConfigResult contains the result of loading configuration. | ||
| type LoadConfigResult struct { | ||
| // Config is the loaded configuration. | ||
| Config *envconfig.ClientConfig | ||
|
|
||
| // ConfigFilePath is the resolved path to the configuration file that was loaded. | ||
| // This may differ from the input if TEMPORAL_CONFIG_FILE env var was used. | ||
| ConfigFilePath string | ||
| } | ||
|
|
||
| // LoadConfig loads the client configuration from the specified file or default location. | ||
| // If ConfigFilePath is empty, the TEMPORAL_CONFIG_FILE environment variable is checked. | ||
| func LoadConfig(options LoadConfigOptions) (LoadConfigResult, error) { | ||
| envLookup := options.EnvLookup | ||
| if envLookup == nil { | ||
| envLookup = EnvLookupOS | ||
| } | ||
| configFilePath := options.ConfigFilePath | ||
| if configFilePath == "" { | ||
| configFilePath, _ = envLookup.LookupEnv("TEMPORAL_CONFIG_FILE") | ||
| } | ||
| clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{ | ||
| ConfigFilePath: configFilePath, | ||
| EnvLookup: envLookup, | ||
| }) | ||
| if err != nil { | ||
| return LoadConfigResult{}, err | ||
| } | ||
| return LoadConfigResult{ | ||
| Config: &clientConfig, | ||
| ConfigFilePath: configFilePath, | ||
| }, nil | ||
| } | ||
|
|
||
| // WriteConfig writes the configuration to the specified file or default location. | ||
| // If configFilePath is empty, the default path will be used. | ||
| func WriteConfig(config *envconfig.ClientConfig, configFilePath string) error { | ||
| // Get file | ||
| if configFilePath == "" { | ||
| var err error | ||
| if configFilePath, err = envconfig.DefaultConfigFilePath(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Convert to TOML | ||
| b, err := config.ToTOML(envconfig.ClientConfigToTOMLOptions{}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed building TOML: %w", err) | ||
| } | ||
|
|
||
| // Write to file, making dirs as needed | ||
| if err := os.MkdirAll(filepath.Dir(configFilePath), 0700); err != nil { | ||
| return fmt.Errorf("failed making config file parent dirs: %w", err) | ||
| } | ||
| if err := os.WriteFile(configFilePath, b, 0600); err != nil { | ||
| return fmt.Errorf("failed writing config file: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package cliext | ||
|
|
||
| import "os" | ||
|
|
||
| // EnvLookupOS is the default EnvLookup implementation. | ||
| var EnvLookupOS EnvLookup = envLookupOS{} | ||
|
|
||
| type EnvLookup interface { | ||
| LookupEnv(key string) (string, bool) | ||
| Environ() []string | ||
| } | ||
|
|
||
| type envLookupOS struct{} | ||
|
|
||
| func (envLookupOS) LookupEnv(key string) (string, bool) { | ||
| return os.LookupEnv(key) | ||
| } | ||
|
|
||
| func (envLookupOS) Environ() []string { | ||
| return os.Environ() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| module github.com/temporalio/cli/cliext | ||
|
|
||
| go 1.25.0 | ||
|
|
||
| require go.temporal.io/sdk/contrib/envconfig v0.1.0 | ||
|
|
||
| require ( | ||
| github.com/BurntSushi/toml v1.4.0 // indirect | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect | ||
| github.com/gogo/protobuf v1.3.2 // indirect | ||
| github.com/golang/mock v1.6.0 // indirect | ||
| github.com/google/uuid v1.6.0 // indirect | ||
| github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect | ||
| github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect | ||
| github.com/nexus-rpc/sdk-go v0.3.0 // indirect | ||
| github.com/pborman/uuid v1.2.1 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| github.com/robfig/cron v1.2.0 // indirect | ||
| github.com/stretchr/objx v0.5.2 // indirect | ||
| github.com/stretchr/testify v1.10.0 // indirect | ||
| go.temporal.io/api v1.44.1 // indirect | ||
| go.temporal.io/sdk v1.32.1 // indirect | ||
| golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect | ||
| golang.org/x/net v0.28.0 // indirect | ||
| golang.org/x/sync v0.8.0 // indirect | ||
| golang.org/x/sys v0.24.0 // indirect | ||
| golang.org/x/text v0.17.0 // indirect | ||
| golang.org/x/time v0.3.0 // indirect | ||
| google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed // indirect | ||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed // indirect | ||
| google.golang.org/grpc v1.66.0 // indirect | ||
| google.golang.org/protobuf v1.34.2 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package cliext | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "go.temporal.io/sdk/contrib/envconfig" | ||
| ) | ||
|
|
||
| // LoadProfileOptions contains options for loading a profile. | ||
| type LoadProfileOptions struct { | ||
| // ConfigFilePath is the path to the configuration file. | ||
| // If empty, TEMPORAL_CONFIG_FILE env var is checked, then the default path is used. | ||
| ConfigFilePath string | ||
|
|
||
| // ProfileName is the name of the profile to load. | ||
| // If empty, TEMPORAL_PROFILE env var is checked, then the default profile is used. | ||
| ProfileName string | ||
|
|
||
| // CreateIfMissing creates an empty profile if it doesn't exist. | ||
| CreateIfMissing bool | ||
|
|
||
| // EnvLookup is used for environment variable lookups. | ||
| // If nil, os.LookupEnv is used. | ||
| EnvLookup EnvLookup | ||
| } | ||
|
|
||
| // LoadProfileResult contains the result of loading a profile. | ||
| type LoadProfileResult struct { | ||
| // Config is the loaded configuration. | ||
| Config *envconfig.ClientConfig | ||
|
|
||
| // ConfigFilePath is the resolved path to the configuration file. | ||
| ConfigFilePath string | ||
|
|
||
| // Profile is the loaded profile. | ||
| Profile *envconfig.ClientConfigProfile | ||
|
|
||
| // ProfileName is the resolved profile name. | ||
| ProfileName string | ||
| } | ||
|
|
||
| // LoadProfile loads a specific profile from the configuration. | ||
| func LoadProfile(opts LoadProfileOptions) (LoadProfileResult, error) { | ||
| envLookup := opts.EnvLookup | ||
| if envLookup == nil { | ||
| envLookup = EnvLookupOS | ||
| } | ||
|
|
||
| configResult, err := LoadConfig(LoadConfigOptions{ | ||
| ConfigFilePath: opts.ConfigFilePath, | ||
| EnvLookup: envLookup, | ||
| }) | ||
| if err != nil { | ||
| return LoadProfileResult{}, err | ||
| } | ||
|
|
||
| profileName := opts.ProfileName | ||
| if profileName == "" { | ||
| profileName, _ = envLookup.LookupEnv("TEMPORAL_PROFILE") | ||
| } | ||
| if profileName == "" { | ||
| profileName = envconfig.DefaultConfigFileProfile | ||
| } | ||
|
|
||
| profile := configResult.Config.Profiles[profileName] | ||
| if profile == nil { | ||
| if !opts.CreateIfMissing { | ||
| return LoadProfileResult{}, fmt.Errorf("profile %q not found", profileName) | ||
| } | ||
| profile = &envconfig.ClientConfigProfile{} | ||
| configResult.Config.Profiles[profileName] = profile | ||
| } | ||
| return LoadProfileResult{ | ||
| Config: configResult.Config, | ||
| Profile: profile, | ||
| ConfigFilePath: configResult.ConfigFilePath, | ||
| ProfileName: profileName, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using structs for input and output seems easier to maintain and extend going forward.