-
Notifications
You must be signed in to change notification settings - Fork 2
Added region support to CLI #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rob-ryszewski
wants to merge
6
commits into
main
Choose a base branch
from
rob-ryszewski/gcp-eu-deploy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be9e02c
Removed unused appdata.Config.Token, as it is instead has always been…
rob-ryszewski 4d04f53
Added support for --region, AMP_REGION, and 'amp set:region'/'amp get…
rob-ryszewski 8e90fc8
Added unit tests accordingly
rob-ryszewski 0f5ded1
Changed region.Regionalize -> region.RegionalizeURL
rob-ryszewski 00d2f44
Replaced xdg.SearchConfigFile with xdg.ConfigFile in appdata
rob-ryszewski 2db3d97
Swapped region priority so 'amp set:region' outranks AMP_REGION, and …
rob-ryszewski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package appdata | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/adrg/xdg" | ||
| ) | ||
|
|
||
| func setup(t *testing.T) string { | ||
| t.Helper() | ||
|
|
||
| configHome := t.TempDir() | ||
| t.Setenv("XDG_CONFIG_HOME", configHome) | ||
| xdg.Reload() | ||
|
|
||
| t.Cleanup(xdg.Reload) | ||
|
|
||
| return configHome | ||
| } | ||
|
|
||
| func writeConfig(t *testing.T, configHome string, contents string) { | ||
| t.Helper() | ||
|
|
||
| configDir := filepath.Join(configHome, "Ampersand") | ||
|
|
||
| err := os.MkdirAll(configDir, 0o700) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err = os.WriteFile(filepath.Join(configDir, "config.json"), []byte(contents), 0o600) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates the environment | ||
| func TestGetNoFile(t *testing.T) { | ||
| setup(t) | ||
|
|
||
| config, err := Get() | ||
| if err != nil { | ||
| t.Fatalf("Get() error = %v", err) | ||
| } | ||
|
|
||
| if config.Region != "" { | ||
| t.Errorf("Get() = %+v, want zero config", config) | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates the environment | ||
| func TestGetCorruptFile(t *testing.T) { | ||
| configHome := setup(t) | ||
| writeConfig(t, configHome, `{"region":`) | ||
|
|
||
| _, err := Get() | ||
| if err == nil { | ||
| t.Error("Get() error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates the environment | ||
| func TestSetCreatesFile(t *testing.T) { | ||
| configHome := setup(t) | ||
|
|
||
| err := Set(Config{Region: "eu"}) | ||
| if err != nil { | ||
| t.Fatalf("Set() error = %v", err) | ||
| } | ||
|
|
||
| contents, err := os.ReadFile(filepath.Join(configHome, "Ampersand", "config.json")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if string(contents) != `{"region":"eu"}` { | ||
| t.Errorf("config.json = %s, want %s", contents, `{"region":"eu"}`) | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates the environment | ||
| func TestSetGetRoundTrip(t *testing.T) { | ||
| setup(t) | ||
|
|
||
| err := Set(Config{Region: "eu"}) | ||
| if err != nil { | ||
| t.Fatalf("Set() error = %v", err) | ||
| } | ||
|
|
||
| config, err := Get() | ||
| if err != nil { | ||
| t.Fatalf("Get() error = %v", err) | ||
| } | ||
|
|
||
| if config.Region != "eu" { | ||
| t.Errorf("Get().Region = %q, want %q", config.Region, "eu") | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates the environment | ||
| func TestSetOverwrites(t *testing.T) { | ||
| setup(t) | ||
|
|
||
| err := Set(Config{Region: "eu"}) | ||
| if err != nil { | ||
| t.Fatalf("Set() error = %v", err) | ||
| } | ||
|
|
||
| err = Set(Config{Region: "us"}) | ||
| if err != nil { | ||
| t.Fatalf("Set() error = %v", err) | ||
| } | ||
|
|
||
| config, err := Get() | ||
| if err != nil { | ||
| t.Fatalf("Get() error = %v", err) | ||
| } | ||
|
|
||
| if config.Region != "us" { | ||
| t.Errorf("Get().Region = %q, want %q", config.Region, "us") | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates the environment | ||
| func TestSetCorruptFile(t *testing.T) { | ||
| configHome := setup(t) | ||
| writeConfig(t, configHome, `{"region":`) | ||
|
|
||
| err := Set(Config{Region: "us"}) | ||
| if err == nil { | ||
| t.Fatal("Set() error = nil, want error") | ||
| } | ||
|
|
||
| contents, err := os.ReadFile(filepath.Join(configHome, "Ampersand", "config.json")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if string(contents) != `{"region":` { | ||
| t.Errorf("config.json = %s, want it unchanged", contents) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package clerk | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/amp-labs/cli/flags" | ||
| "github.com/amp-labs/cli/region" | ||
| ) | ||
|
|
||
| //nolint:paralleltest // mutates viper and the environment | ||
| func TestGetJwtFile(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| region region.Region | ||
| stage string | ||
| want string | ||
| }{ | ||
| {name: "default prod", region: region.Default, stage: "prod", want: "amp/jwt.json"}, | ||
| {name: "us prod", region: region.US, stage: "prod", want: "amp/jwt.json"}, | ||
| {name: "us staging", region: region.US, stage: "staging", want: "amp/jwt-staging.json"}, | ||
| {name: "us dev", region: region.US, stage: "dev", want: "amp/jwt-dev.json"}, | ||
| {name: "eu prod", region: region.EU, stage: "prod", want: "amp/jwt-eu.json"}, | ||
| {name: "eu staging", region: region.EU, stage: "staging", want: "amp/jwt-eu-staging.json"}, | ||
| {name: "eu dev", region: region.EU, stage: "dev", want: "amp/jwt-eu-dev.json"}, | ||
| } | ||
|
|
||
| t.Cleanup(func() { flags.SetRegion(region.Default) }) | ||
|
|
||
| for _, testCase := range tests { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Setenv("AMP_STAGE_OVERRIDE", testCase.stage) | ||
| flags.SetRegion(testCase.region) | ||
|
|
||
| got := GetJwtFile() | ||
| if got != testCase.want { | ||
| t.Errorf("GetJwtFile() = %q, want %q", got, testCase.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates viper and the environment | ||
| func TestGetJwtFileUnique(t *testing.T) { | ||
| t.Cleanup(func() { flags.SetRegion(region.Default) }) | ||
|
|
||
| seen := make(map[string]string) | ||
|
|
||
| for _, name := range region.Known() { | ||
| for _, stage := range []string{"prod", "staging", "dev", "local"} { | ||
| reg, err := region.Parse(name) | ||
| if err != nil { | ||
| t.Fatalf("Parse(%q) error = %v", name, err) | ||
| } | ||
|
|
||
| t.Setenv("AMP_STAGE_OVERRIDE", stage) | ||
| flags.SetRegion(reg) | ||
|
|
||
| path := GetJwtFile() | ||
| key := name + "/" + stage | ||
|
|
||
| if other, ok := seen[path]; ok { | ||
| t.Errorf("GetJwtFile() = %q for both %s and %s", path, other, key) | ||
| } | ||
|
|
||
| seen[path] = key | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/amp-labs/cli/flags" | ||
| "github.com/amp-labs/cli/logger" | ||
| "github.com/amp-labs/cli/region" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var getRegionCmd = &cobra.Command{ //nolint:gochecknoglobals | ||
| Use: "get:region", | ||
| Short: "Print the region the CLI is talking to", | ||
| Long: "Print the region the CLI is talking to.\n\n" + | ||
| "This is the --region flag if given, otherwise the region saved by 'amp set:region', " + | ||
| "otherwise the " + region.EnvVar + " environment variable, otherwise defaults to " + string(region.Default) + ".", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // The root command's PersistentPreRun has already resolved this, and would | ||
| // have exited if it could not. | ||
| logger.Info(string(flags.GetRegion())) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(getRegionCmd) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this was only
Config.Tokenand not being used anywhere, (token was being managed in amp/jwt.json by clerk/clerk.go, this particular changeset doesn't break any backwards compatibility for this config