From 9ddd3298c326d821cf0de81667c3d0a888d2756b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Michon?= Date: Mon, 18 Sep 2023 11:48:30 +0200 Subject: [PATCH 1/4] feat(region): more debug logs --- config/region.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/config/region.go b/config/region.go index 7b8ccfac8..487eb4a76 100644 --- a/config/region.go +++ b/config/region.go @@ -49,6 +49,7 @@ type GetRegionOpts struct { } func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (RegionsCache, error) { + debug.Println("[Regions] Ensure cache is filled") var regionsCache RegionsCache fd, err := os.Open(c.RegionsCachePath) if err == nil { @@ -63,6 +64,7 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi var client *scalingo.Client if opts.SkipAuth { + debug.Println("[Regions] Create an unauthenticated client to the authentication service") client, err = ScalingoUnauthenticatedAuthClient(ctx) if err != nil { return RegionsCache{}, errgo.Notef(err, "fail to create an unauthenticated client") @@ -77,13 +79,14 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi } } - debug.Println("[Regions] Get the list of regions to fill the cache") + debug.Println("[Regions] Create an authenticated client to the authentication service using the token") client, err = ScalingoAuthClientFromToken(ctx, token.Token) if err != nil { return RegionsCache{}, errgo.Notef(err, "fail to create an authenticated Scalingo client using the API token") } } + debug.Println("[Regions] Get the list of regions to fill the cache") regions, err := client.RegionsList(ctx) if err != nil { return RegionsCache{}, errgo.Notef(err, "fail to list available regions") @@ -93,16 +96,18 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi regionsCache.ExpireAt = time.Now().Add(10 * time.Minute) if opts.SkipAuth { + debug.Println("[Regions] Do not save the cache") // If we skipped the authentication the region cache should not be saved since it will not contain regions that are not publicly available (like osc-secnum-fr1) return regionsCache, nil } + debug.Println("[Regions] Save the cache") fd, err = os.OpenFile(c.RegionsCachePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0750) if err == nil { json.NewEncoder(fd).Encode(regionsCache) fd.Close() } else { - debug.Printf("[Regions] Failed to save the regions cache: %v\n", err) + debug.Printf("[Regions] Failed to save the cache: %v\n", err) } return regionsCache, nil @@ -119,6 +124,7 @@ func GetRegion(ctx context.Context, c Config, name string, opts GetRegionOpts) ( for _, region := range regionsCache.Regions { if region.Name == name { + debug.Println("[Regions] Found the region in the cache") return region, nil } } From 0b1dcd889161eea8882fd2e7b185b7839b87256c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Michon?= Date: Mon, 18 Sep 2023 13:44:00 +0200 Subject: [PATCH 2/4] refactor(region): more idiomatic Go --- config/region.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/config/region.go b/config/region.go index 487eb4a76..fe71d6042 100644 --- a/config/region.go +++ b/config/region.go @@ -52,7 +52,9 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi debug.Println("[Regions] Ensure cache is filled") var regionsCache RegionsCache fd, err := os.Open(c.RegionsCachePath) - if err == nil { + if err != nil { + debug.Printf("[Regions] Fail to open the cache: %v\n", err) + } else { json.NewDecoder(fd).Decode(®ionsCache) fd.Close() } @@ -103,13 +105,13 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi debug.Println("[Regions] Save the cache") fd, err = os.OpenFile(c.RegionsCachePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0750) - if err == nil { - json.NewEncoder(fd).Encode(regionsCache) - fd.Close() - } else { + if err != nil { debug.Printf("[Regions] Failed to save the cache: %v\n", err) + return regionsCache, nil } + json.NewEncoder(fd).Encode(regionsCache) + fd.Close() return regionsCache, nil } From 1d9d0aa73ce49d63354556855aded4e7bcddd439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Michon?= Date: Mon, 18 Sep 2023 13:45:25 +0200 Subject: [PATCH 3/4] docs: missing changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 890d715f5..dcffce5d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * feat: add database maintenance listing with the new `database-maintenance-list` command ([PR#982](https://github.com/Scalingo/cli/pull/982)) * feat(addons): add maintenance windows manipulation with the new `addon-config` command ([PR#955](https://github.com/Scalingo/cli/pull/955)) * feat(install.sh): verify the archive checksum ([PR#988](https://github.com/Scalingo/cli/pull/988)) +* feat(region): more debug logs ([PR#1007](https://github.com/Scalingo/cli/pull/1008)) ### 1.29.1 From 531c97265f4fa6ba1ad46116e3e6a039cef3b0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Michon?= Date: Mon, 18 Sep 2023 13:53:09 +0200 Subject: [PATCH 4/4] fix(region): missing err checks --- config/region.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/config/region.go b/config/region.go index fe71d6042..1f6cf8362 100644 --- a/config/region.go +++ b/config/region.go @@ -55,7 +55,10 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi if err != nil { debug.Printf("[Regions] Fail to open the cache: %v\n", err) } else { - json.NewDecoder(fd).Decode(®ionsCache) + err := json.NewDecoder(fd).Decode(®ionsCache) + if err != nil { + debug.Printf("[Regions] Fail to decode the cache to the file: %v\n", err) + } fd.Close() } // If cache is still valid @@ -110,7 +113,10 @@ func EnsureRegionsCache(ctx context.Context, c Config, opts GetRegionOpts) (Regi return regionsCache, nil } - json.NewEncoder(fd).Encode(regionsCache) + err = json.NewEncoder(fd).Encode(regionsCache) + if err != nil { + debug.Printf("[Regions] Fail to encode the cache to the file: %v\n", err) + } fd.Close() return regionsCache, nil }