Skip to content

Commit

Permalink
client: envvarパッケージへの切り替え (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc committed Mar 10, 2022
1 parent ab494c9 commit d831012
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
3 changes: 2 additions & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ 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
)

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
Expand Down
2 changes: 2 additions & 0 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
53 changes: 11 additions & 42 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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を作成する際のオプション
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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", "") != "",
}
}

Expand Down Expand Up @@ -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)
}

0 comments on commit d831012

Please sign in to comment.