From 979811280ff09ff4c2da9ff3e992ba74779419ab Mon Sep 17 00:00:00 2001 From: Kazumichi Yamamoto Date: Thu, 10 Mar 2022 09:23:49 +0900 Subject: [PATCH] =?UTF-8?q?client:=20envvar=E3=83=91=E3=83=83=E3=82=B1?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E3=81=B8=E3=81=AE=E5=88=87=E3=82=8A=E6=9B=BF?= =?UTF-8?q?=E3=81=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/go.mod | 3 ++- client/go.sum | 2 ++ client/options.go | 53 ++++++++++------------------------------------- 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/client/go.mod b/client/go.mod index 7c03041..0633c89 100644 --- a/client/go.mod +++ b/client/go.mod @@ -3,7 +3,9 @@ module github.com/sacloud/sacloud-go/client go 1.17 require ( + github.com/hashicorp/go-retryablehttp v0.7.0 github.com/sacloud/go-http v0.0.4 + github.com/sacloud/sacloud-go/pkg v0.0.0-20220310002004-ab494c9c1c6d github.com/stretchr/testify v1.7.0 ) @@ -11,7 +13,6 @@ require ( github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/hashicorp/go-cleanhttp v0.5.1 // indirect - github.com/hashicorp/go-retryablehttp v0.7.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.uber.org/ratelimit v0.2.0 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect diff --git a/client/go.sum b/client/go.sum index 11b8089..23de962 100644 --- a/client/go.sum +++ b/client/go.sum @@ -13,6 +13,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sacloud/go-http v0.0.4 h1:+vgx/uCctcGiHMe8jE+qirMKd3+d73MNZXK7nrUo0po= github.com/sacloud/go-http v0.0.4/go.mod h1:aYTXNuAnPmD6Ar3ktDaR1gPxJCPv2CqpppcYciy1hmo= +github.com/sacloud/sacloud-go/pkg v0.0.0-20220310002004-ab494c9c1c6d h1:Rdj8QU3EZrmdUKJMvJ0U3kkgUZFLzZ5u1kMKdQnW7eY= +github.com/sacloud/sacloud-go/pkg v0.0.0-20220310002004-ab494c9c1c6d/go.mod h1:/hcfG/jZlqNDRpnMDl9rm1aBLgNCeX1NF6FyaIwk9+k= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/client/options.go b/client/options.go index 1944c50..4558c3e 100644 --- a/client/options.go +++ b/client/options.go @@ -17,11 +17,10 @@ package client import ( "context" "net/http" - "os" - "strconv" sacloudhttp "github.com/sacloud/go-http" "github.com/sacloud/sacloud-go/client/profile" + "github.com/sacloud/sacloud-go/pkg/envvar" ) // Options sacloudhttp.Clientを作成する際のオプション @@ -92,7 +91,7 @@ func DefaultOption() (*Options, error) { // 同じ項目を複数箇所で指定していた場合、環境変数->プロファイルの順で上書きされたものが返される func DefaultOptionWithProfile(profileName string) (*Options, error) { if profileName == "" { - profileName = stringFromEnvMulti([]string{"SAKURACLOUD_PROFILE", "USACLOUD_PROFILE"}, "") + profileName = envvar.StringFromEnvMulti([]string{"SAKURACLOUD_PROFILE", "USACLOUD_PROFILE"}, "") } fromProfile, err := OptionsFromProfile(profileName) if err != nil { @@ -159,19 +158,19 @@ func MergeOptions(opts ...*Options) *Options { // OptionsFromEnv 環境変数からCallerOptionsを組み立てて返す func OptionsFromEnv() *Options { return &Options{ - AccessToken: stringFromEnv("SAKURACLOUD_ACCESS_TOKEN", ""), - AccessTokenSecret: stringFromEnv("SAKURACLOUD_ACCESS_TOKEN_SECRET", ""), + AccessToken: envvar.StringFromEnv("SAKURACLOUD_ACCESS_TOKEN", ""), + AccessTokenSecret: envvar.StringFromEnv("SAKURACLOUD_ACCESS_TOKEN_SECRET", ""), - AcceptLanguage: stringFromEnv("SAKURACLOUD_ACCEPT_LANGUAGE", ""), + AcceptLanguage: envvar.StringFromEnv("SAKURACLOUD_ACCEPT_LANGUAGE", ""), - HttpRequestTimeout: intFromEnv("SAKURACLOUD_API_REQUEST_TIMEOUT", 0), - HttpRequestRateLimit: intFromEnv("SAKURACLOUD_API_REQUEST_RATE_LIMIT", 0), + HttpRequestTimeout: envvar.IntFromEnv("SAKURACLOUD_API_REQUEST_TIMEOUT", 0), + HttpRequestRateLimit: envvar.IntFromEnv("SAKURACLOUD_API_REQUEST_RATE_LIMIT", 0), - RetryMax: intFromEnv("SAKURACLOUD_RETRY_MAX", 0), - RetryWaitMax: intFromEnv("SAKURACLOUD_RETRY_WAIT_MAX", 0), - RetryWaitMin: intFromEnv("SAKURACLOUD_RETRY_WAIT_MIN", 0), + RetryMax: envvar.IntFromEnv("SAKURACLOUD_RETRY_MAX", 0), + RetryWaitMax: envvar.IntFromEnv("SAKURACLOUD_RETRY_WAIT_MAX", 0), + RetryWaitMin: envvar.IntFromEnv("SAKURACLOUD_RETRY_WAIT_MIN", 0), - Trace: stringFromEnv("SAKURACLOUD_TRACE", "") != "", + Trace: envvar.StringFromEnv("SAKURACLOUD_TRACE", "") != "", } } @@ -205,33 +204,3 @@ func OptionsFromProfile(profileName string) (*Options, error) { profileConfigValue: &config, }, nil } - -func stringFromEnv(key, defaultValue string) string { - v := os.Getenv(key) - if v == "" { - return defaultValue - } - return v -} - -func stringFromEnvMulti(keys []string, defaultValue string) string { - for _, key := range keys { - v := os.Getenv(key) - if v != "" { - return v - } - } - return defaultValue -} - -func intFromEnv(key string, defaultValue int) int { - v := os.Getenv(key) - if v == "" { - return defaultValue - } - i, err := strconv.ParseInt(v, 10, 64) - if err != nil { - return defaultValue - } - return int(i) -}