Skip to content

Commit de9d45e

Browse files
committed
Profile library
1 parent 959f910 commit de9d45e

File tree

8 files changed

+628
-241
lines changed

8 files changed

+628
-241
lines changed

cliext/config.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

cliext/env.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cliext
2+
3+
import "os"
4+
5+
// EnvLookupOS is the default EnvLookup implementation.
6+
var EnvLookupOS EnvLookup = envLookupOS{}
7+
8+
type EnvLookup interface {
9+
LookupEnv(key string) (string, bool)
10+
Environ() []string
11+
}
12+
13+
type envLookupOS struct{}
14+
15+
func (envLookupOS) LookupEnv(key string) (string, bool) {
16+
return os.LookupEnv(key)
17+
}
18+
19+
func (envLookupOS) Environ() []string {
20+
return os.Environ()
21+
}

cliext/go.mod

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module github.com/temporalio/cli/cliext
2+
3+
go 1.25.0
4+
5+
require go.temporal.io/sdk/contrib/envconfig v0.1.0
6+
7+
require (
8+
github.com/BurntSushi/toml v1.4.0 // indirect
9+
github.com/davecgh/go-spew v1.1.1 // indirect
10+
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
11+
github.com/gogo/protobuf v1.3.2 // indirect
12+
github.com/golang/mock v1.6.0 // indirect
13+
github.com/google/uuid v1.6.0 // indirect
14+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
15+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
16+
github.com/nexus-rpc/sdk-go v0.3.0 // indirect
17+
github.com/pborman/uuid v1.2.1 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
github.com/robfig/cron v1.2.0 // indirect
20+
github.com/stretchr/objx v0.5.2 // indirect
21+
github.com/stretchr/testify v1.10.0 // indirect
22+
go.temporal.io/api v1.44.1 // indirect
23+
go.temporal.io/sdk v1.32.1 // indirect
24+
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
25+
golang.org/x/net v0.28.0 // indirect
26+
golang.org/x/sync v0.8.0 // indirect
27+
golang.org/x/sys v0.24.0 // indirect
28+
golang.org/x/text v0.17.0 // indirect
29+
golang.org/x/time v0.3.0 // indirect
30+
google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed // indirect
31+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed // indirect
32+
google.golang.org/grpc v1.66.0 // indirect
33+
google.golang.org/protobuf v1.34.2 // indirect
34+
gopkg.in/yaml.v3 v3.0.1 // indirect
35+
)

cliext/go.sum

Lines changed: 183 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)