From 26a31561e41e838a74fa24f35fbcc74c058c7d37 Mon Sep 17 00:00:00 2001 From: "Jonas L." Date: Mon, 5 Aug 2024 10:34:35 +0200 Subject: [PATCH] refactor: reuse `envutil.LookupEnvWithFile` from hcloud-go (#676) --- internal/app/app.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 075515e5..03e4ba34 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -19,6 +19,7 @@ import ( "github.com/hetznercloud/csi-driver/internal/driver" "github.com/hetznercloud/csi-driver/internal/metrics" "github.com/hetznercloud/hcloud-go/v2/hcloud" + "github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/envutil" "github.com/hetznercloud/hcloud-go/v2/hcloud/metadata" ) @@ -101,21 +102,14 @@ func CreateMetrics(logger log.Logger) *metrics.Metrics { // CreateHcloudClient creates a hcloud.Client using various environment variables to guide configuration func CreateHcloudClient(metricsRegistry *prometheus.Registry, logger log.Logger) (*hcloud.Client, error) { - // apiToken can be set via HCLOUD_TOKEN or HCLOUD_TOKEN_FILE - // HCLOUD_TOKEN is preferred - apiToken, ok := os.LookupEnv("HCLOUD_TOKEN") - if !ok { - filepath, ok := os.LookupEnv("HCLOUD_TOKEN_FILE") - if !ok { - return nil, fmt.Errorf("you need to provide an API token via the HCLOUD_TOKEN or HCLOUD_TOKEN_FILE env var") - } - apiTokenBytes, err := os.ReadFile(filepath) - if err != nil { - return nil, fmt.Errorf("failed to read HCLOUD_TOKEN_FILE: %w", err) - } - apiToken = strings.TrimSpace(string(apiTokenBytes)) + // apiToken can be set via HCLOUD_TOKEN (preferred) or HCLOUD_TOKEN_FILE + apiToken, err := envutil.LookupEnvWithFile("HCLOUD_TOKEN") + if err != nil { + return nil, err + } + if apiToken == "" { + return nil, fmt.Errorf("you need to provide an API token via the HCLOUD_TOKEN or HCLOUD_TOKEN_FILE env var") } - if len(apiToken) != 64 { return nil, errors.New("entered token is invalid (must be exactly 64 characters long)") }