From 2231cdb7779422f1a37c4bc81d274f5c483986c6 Mon Sep 17 00:00:00 2001 From: Stephan Behnke Date: Wed, 26 Nov 2025 15:32:47 -0800 Subject: [PATCH] Extract config and profile helpers --- cliext/config.go | 81 +++++++++++ cliext/env.go | 21 +++ cliext/go.mod | 35 +++++ cliext/go.sum | 183 ++++++++++++++++++++++++ cliext/profile.go | 79 ++++++++++ go.mod | 3 + internal/temporalcli/commands.config.go | 132 +++++------------ internal/temporalcli/commands.go | 8 +- 8 files changed, 441 insertions(+), 101 deletions(-) create mode 100644 cliext/config.go create mode 100644 cliext/env.go create mode 100644 cliext/go.mod create mode 100644 cliext/go.sum create mode 100644 cliext/profile.go diff --git a/cliext/config.go b/cliext/config.go new file mode 100644 index 000000000..9350f08fa --- /dev/null +++ b/cliext/config.go @@ -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 +} diff --git a/cliext/env.go b/cliext/env.go new file mode 100644 index 000000000..bd96f69ed --- /dev/null +++ b/cliext/env.go @@ -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() +} diff --git a/cliext/go.mod b/cliext/go.mod new file mode 100644 index 000000000..2e76a4b87 --- /dev/null +++ b/cliext/go.mod @@ -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 +) diff --git a/cliext/go.sum b/cliext/go.sum new file mode 100644 index 000000000..31188895f --- /dev/null +++ b/cliext/go.sum @@ -0,0 +1,183 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw= +github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA= +github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= +github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/nexus-rpc/sdk-go v0.3.0 h1:Y3B0kLYbMhd4C2u00kcYajvmOrfozEtTV/nHSnV57jA= +github.com/nexus-rpc/sdk-go v0.3.0/go.mod h1:TpfkM2Cw0Rlk9drGkoiSMpFqflKTiQLWUNyKJjF8mKQ= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw= +github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= +github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.temporal.io/api v1.44.1 h1:sb5Hq08AB0WtYvfLJMiWmHzxjqs2b+6Jmzg4c8IOeng= +go.temporal.io/api v1.44.1/go.mod h1:1WwYUMo6lao8yl0371xWUm13paHExN5ATYT/B7QtFis= +go.temporal.io/sdk v1.32.1 h1:slA8prhdFr4lxpsTcRusWVitD/cGjELfKUh0mBj73SU= +go.temporal.io/sdk v1.32.1/go.mod h1:8U8H7rF9u4Hyb4Ry9yiEls5716DHPNvVITPNkgWUwE8= +go.temporal.io/sdk/contrib/envconfig v0.1.0 h1:s+G/Ujph+Xl2jzLiiIm2T1vuijDkUL4Kse49dgDVGBE= +go.temporal.io/sdk/contrib/envconfig v0.1.0/go.mod h1:FQEO3C56h9C7M6sDgSanB8HnBTmopw9qgVx4F1S6pJk= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No= +golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed h1:3RgNmBoI9MZhsj3QxC+AP/qQhNwpCLOvYDYYsFrhFt0= +google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed/go.mod h1:OCdP9MfskevB/rbYvHTsXTtKC+3bHWajPdoKgjcYkfo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed h1:J6izYgfBXAI3xTKLgxzTmUltdYaLsuBxFCgDHWJ/eXg= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= +google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c= +google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/cliext/profile.go b/cliext/profile.go new file mode 100644 index 000000000..ae324cde2 --- /dev/null +++ b/cliext/profile.go @@ -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 +} diff --git a/go.mod b/go.mod index cf4032f74..8ffc5c454 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/spf13/cobra v1.9.1 github.com/spf13/pflag v1.0.6 github.com/stretchr/testify v1.10.0 + github.com/temporalio/cli/cliext v0.0.0 github.com/temporalio/ui-server/v2 v2.42.1 go.temporal.io/api v1.53.0 go.temporal.io/sdk v1.37.0 @@ -176,3 +177,5 @@ require ( modernc.org/strutil v1.2.1 // indirect modernc.org/token v1.1.0 // indirect ) + +replace github.com/temporalio/cli/cliext => ./cliext diff --git a/internal/temporalcli/commands.config.go b/internal/temporalcli/commands.config.go index 79467328d..5910b356f 100644 --- a/internal/temporalcli/commands.config.go +++ b/internal/temporalcli/commands.config.go @@ -2,46 +2,41 @@ package temporalcli import ( "fmt" - "os" - "path/filepath" "reflect" "sort" "strings" "github.com/BurntSushi/toml" + "github.com/temporalio/cli/cliext" "github.com/temporalio/cli/internal/printer" "go.temporal.io/sdk/contrib/envconfig" ) func (c *TemporalConfigDeleteCommand) run(cctx *CommandContext, _ []string) error { - // Load config - profileName := envConfigProfileName(cctx) - conf, confProfile, err := loadEnvConfigProfile(cctx, profileName, true) + result, err := cliext.LoadProfile(loadProfileOpts(cctx, true)) if err != nil { return err } if strings.HasPrefix(c.Prop, "grpc_meta.") { key := strings.TrimPrefix(c.Prop, "grpc_meta.") - if _, ok := confProfile.GRPCMeta[key]; !ok { + if _, ok := result.Profile.GRPCMeta[key]; !ok { return fmt.Errorf("gRPC meta key %q not found", key) } - delete(confProfile.GRPCMeta, key) + delete(result.Profile.GRPCMeta, key) } else { - reflectVal, err := reflectEnvConfigProp(confProfile, c.Prop, true) + reflectVal, err := reflectEnvConfigProp(result.Profile, c.Prop, true) if err != nil { return err } reflectVal.SetZero() } - // Save - return writeEnvConfigFile(cctx, conf) + cctx.Logger.Info("Writing config file", "file", result.ConfigFilePath) + return cliext.WriteConfig(result.Config, result.ConfigFilePath) } func (c *TemporalConfigDeleteProfileCommand) run(cctx *CommandContext, _ []string) error { - // Load config - profileName := envConfigProfileName(cctx) - conf, _, err := loadEnvConfigProfile(cctx, profileName, true) + result, err := cliext.LoadProfile(loadProfileOpts(cctx, true)) if err != nil { return err } @@ -51,16 +46,14 @@ func (c *TemporalConfigDeleteProfileCommand) run(cctx *CommandContext, _ []strin if cctx.RootCommand.Profile == "" { return fmt.Errorf("to delete an entire profile, --profile must be provided explicitly") } - delete(conf.Profiles, profileName) + delete(result.Config.Profiles, result.ProfileName) - // Save - return writeEnvConfigFile(cctx, conf) + cctx.Logger.Info("Writing config file", "file", result.ConfigFilePath) + return cliext.WriteConfig(result.Config, result.ConfigFilePath) } func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error { - // Load config profile - profileName := envConfigProfileName(cctx) - conf, confProfile, err := loadEnvConfigProfile(cctx, profileName, true) + result, err := cliext.LoadProfile(loadProfileOpts(cctx, true)) if err != nil { return err } @@ -78,14 +71,14 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error { var reflectVal reflect.Value // gRPC meta is special if strings.HasPrefix(c.Prop, "grpc_meta.") { - v, ok := confProfile.GRPCMeta[strings.TrimPrefix(c.Prop, "grpc_meta.")] + v, ok := result.Profile.GRPCMeta[strings.TrimPrefix(c.Prop, "grpc_meta.")] if !ok { return fmt.Errorf("unknown property %q", c.Prop) } reflectVal = reflect.ValueOf(v) } else { // Single value goes into property-value structure - reflectVal, err = reflectEnvConfigProp(confProfile, c.Prop, false) + reflectVal, err = reflectEnvConfigProp(result.Profile, c.Prop, false) if err != nil { return err } @@ -104,12 +97,12 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error { var tomlConf struct { Profiles map[string]any `toml:"profile"` } - if b, err := conf.ToTOML(envconfig.ClientConfigToTOMLOptions{}); err != nil { + if b, err := result.Config.ToTOML(envconfig.ClientConfigToTOMLOptions{}); err != nil { return fmt.Errorf("failed converting to TOML: %w", err) } else if err := toml.Unmarshal(b, &tomlConf); err != nil { return fmt.Errorf("failed converting from TOML: %w", err) } - return cctx.Printer.PrintStructured(tomlConf.Profiles[profileName], printer.StructuredOptions{}) + return cctx.Printer.PrintStructured(tomlConf.Profiles[result.ProfileName], printer.StructuredOptions{}) } else { // Get every property individually as a property-value pair except zero // vals @@ -117,12 +110,12 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error { for k := range envConfigPropsToFieldNames { // TLS is a special case if k == "tls" { - if confProfile.TLS != nil { + if result.Profile.TLS != nil { props = append(props, prop{Property: "tls", Value: true}) } continue } - if val, err := reflectEnvConfigProp(confProfile, k, false); err != nil { + if val, err := reflectEnvConfigProp(result.Profile, k, false); err != nil { return err } else if !val.IsZero() { props = append(props, prop{Property: k, Value: val.Interface()}) @@ -130,7 +123,7 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error { } // Add "grpc_meta" - for k, v := range confProfile.GRPCMeta { + for k, v := range result.Profile.GRPCMeta { props = append(props, prop{Property: "grpc_meta." + k, Value: v}) } @@ -141,7 +134,7 @@ func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error { } func (c *TemporalConfigListCommand) run(cctx *CommandContext, _ []string) error { - clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{ + result, err := cliext.LoadConfig(cliext.LoadConfigOptions{ ConfigFilePath: cctx.RootCommand.ConfigFile, EnvLookup: cctx.Options.EnvLookup, }) @@ -151,8 +144,8 @@ func (c *TemporalConfigListCommand) run(cctx *CommandContext, _ []string) error type profile struct { Name string `json:"name"` } - profiles := make([]profile, 0, len(clientConfig.Profiles)) - for k := range clientConfig.Profiles { + profiles := make([]profile, 0, len(result.Config.Profiles)) + for k := range result.Config.Profiles { profiles = append(profiles, profile{Name: k}) } sort.Slice(profiles, func(i, j int) bool { return profiles[i].Name < profiles[j].Name }) @@ -160,20 +153,19 @@ func (c *TemporalConfigListCommand) run(cctx *CommandContext, _ []string) error } func (c *TemporalConfigSetCommand) run(cctx *CommandContext, _ []string) error { - // Load config - conf, confProfile, err := loadEnvConfigProfile(cctx, envConfigProfileName(cctx), false) + result, err := cliext.LoadProfile(loadProfileOpts(cctx, false)) if err != nil { return err } // As a special case, "grpc_meta." values are handled specifically if strings.HasPrefix(c.Prop, "grpc_meta.") { - if confProfile.GRPCMeta == nil { - confProfile.GRPCMeta = map[string]string{} + if result.Profile.GRPCMeta == nil { + result.Profile.GRPCMeta = map[string]string{} } - confProfile.GRPCMeta[strings.TrimPrefix(c.Prop, "grpc_meta.")] = c.Value + result.Profile.GRPCMeta[strings.TrimPrefix(c.Prop, "grpc_meta.")] = c.Value } else { // Get reflect value - reflectVal, err := reflectEnvConfigProp(confProfile, c.Prop, false) + reflectVal, err := reflectEnvConfigProp(result.Profile, c.Prop, false) if err != nil { return err } @@ -211,42 +203,17 @@ func (c *TemporalConfigSetCommand) run(cctx *CommandContext, _ []string) error { } } - // Save - return writeEnvConfigFile(cctx, conf) + cctx.Logger.Info("Writing config file", "file", result.ConfigFilePath) + return cliext.WriteConfig(result.Config, result.ConfigFilePath) } -func envConfigProfileName(cctx *CommandContext) string { - if cctx.RootCommand.Profile != "" { - return cctx.RootCommand.Profile - } else if p, _ := cctx.Options.EnvLookup.LookupEnv("TEMPORAL_PROFILE"); p != "" { - return p +func loadProfileOpts(cctx *CommandContext, failIfNotFound bool) cliext.LoadProfileOptions { + return cliext.LoadProfileOptions{ + ConfigFilePath: cctx.RootCommand.ConfigFile, + ProfileName: cctx.RootCommand.Profile, + CreateIfMissing: !failIfNotFound, + EnvLookup: cctx.Options.EnvLookup, } - return envconfig.DefaultConfigFileProfile -} - -func loadEnvConfigProfile( - cctx *CommandContext, - profile string, - failIfNotFound bool, -) (*envconfig.ClientConfig, *envconfig.ClientConfigProfile, error) { - clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{ - ConfigFilePath: cctx.RootCommand.ConfigFile, - EnvLookup: cctx.Options.EnvLookup, - }) - if err != nil { - return nil, nil, err - } - - // Load profile - clientProfile := clientConfig.Profiles[profile] - if clientProfile == nil { - if failIfNotFound { - return nil, nil, fmt.Errorf("profile %q not found", profile) - } - clientProfile = &envconfig.ClientConfigProfile{} - clientConfig.Profiles[profile] = clientProfile - } - return &clientConfig, clientProfile, nil } var envConfigPropsToFieldNames = map[string]string{ @@ -304,32 +271,3 @@ func reflectEnvConfigProp( } return parentVal.FieldByName(field), nil } - -func writeEnvConfigFile(cctx *CommandContext, conf *envconfig.ClientConfig) error { - // Get file - configFile := cctx.RootCommand.ConfigFile - if configFile == "" { - configFile, _ = cctx.Options.EnvLookup.LookupEnv("TEMPORAL_CONFIG_FILE") - if configFile == "" { - var err error - if configFile, err = envconfig.DefaultConfigFilePath(); err != nil { - return err - } - } - } - - // Convert to TOML - b, err := conf.ToTOML(envconfig.ClientConfigToTOMLOptions{}) - if err != nil { - return fmt.Errorf("failed building TOML: %w", err) - } - - // Write to file, making dirs as needed - cctx.Logger.Info("Writing config file", "file", configFile) - if err := os.MkdirAll(filepath.Dir(configFile), 0700); err != nil { - return fmt.Errorf("failed making config file parent dirs: %w", err) - } else if err := os.WriteFile(configFile, b, 0600); err != nil { - return fmt.Errorf("failed writing config file: %w", err) - } - return nil -} diff --git a/internal/temporalcli/commands.go b/internal/temporalcli/commands.go index 135cb113f..70ec847d4 100644 --- a/internal/temporalcli/commands.go +++ b/internal/temporalcli/commands.go @@ -20,12 +20,12 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/temporalio/cli/internal/printer" + "github.com/temporalio/cli/cliext" "github.com/temporalio/ui-server/v2/server/version" "go.temporal.io/api/common/v1" commonpb "go.temporal.io/api/common/v1" "go.temporal.io/api/failure/v1" "go.temporal.io/api/temporalproto" - "go.temporal.io/sdk/contrib/envconfig" "go.temporal.io/sdk/converter" "go.temporal.io/sdk/temporal" "go.temporal.io/server/common/headers" @@ -66,8 +66,8 @@ type CommandOptions struct { Args []string // Deprecated `--env` and `--env-file` approach DeprecatedEnvConfig DeprecatedEnvConfig - // If nil, [envconfig.EnvLookupOS] is used. - EnvLookup envconfig.EnvLookup + // If nil, [cliext.EnvLookupOS] is used. + EnvLookup cliext.EnvLookup // These three fields below default to OS values Stdin io.Reader @@ -116,7 +116,7 @@ func (c *CommandContext) preprocessOptions() error { c.Options.Args = os.Args[1:] } if c.Options.EnvLookup == nil { - c.Options.EnvLookup = envconfig.EnvLookupOS + c.Options.EnvLookup = cliext.EnvLookupOS } if c.Options.Stdin == nil {