-
Notifications
You must be signed in to change notification settings - Fork 16
Add stackit config profile list/delete
commands
#359
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f62900a
better error handling, make argument option in set
DiogoFerrao 2f9624e
address PR comments, improve testing for file utils copy file
DiogoFerrao fa7b0cd
check for command existance in set and get profile
DiogoFerrao 5846424
initial implementation
DiogoFerrao 7aec313
get email
DiogoFerrao b39598d
list command done
DiogoFerrao 105806d
finish list, add delete
DiogoFerrao 216670c
force profile names to be lowercase
DiogoFerrao 0ae453c
adapt testing for lowercase profile names
DiogoFerrao 6226bcb
generate docs
DiogoFerrao d792b5f
add testing to delete
DiogoFerrao 807aed4
update regex to not allow starting with hyphen
DiogoFerrao 70b9d3c
generate docs
DiogoFerrao 29a2501
add testing for new storage methods
DiogoFerrao b08b05d
address PR comments
DiogoFerrao ea75cc5
address PR comments
DiogoFerrao 594861e
remove unused test logic
DiogoFerrao 605bdc1
delete profile removes keyring entries
DiogoFerrao ecc5d7b
address PR comments
DiogoFerrao e6412a1
add test for keyring failure
DiogoFerrao 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,40 @@ | ||
## stackit config profile delete | ||
|
||
Delete a CLI configuration profile | ||
|
||
### Synopsis | ||
|
||
Delete a CLI configuration profile. | ||
If the deleted profile is the active profile, the default profile will be set to active. | ||
|
||
``` | ||
stackit config profile delete PROFILE [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
Delete the configuration profile "my-profile" | ||
$ stackit config profile delete my-profile | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help Help for "stackit config profile delete" | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-y, --assume-yes If set, skips all confirmation prompts | ||
--async If set, runs the command asynchronously | ||
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
-p, --project-id string Project ID | ||
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [stackit config profile](./stackit_config_profile.md) - Manage the CLI configuration profiles | ||
|
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,42 @@ | ||
## stackit config profile list | ||
|
||
Lists all CLI configuration profiles | ||
|
||
### Synopsis | ||
|
||
Lists all CLI configuration profiles. | ||
|
||
``` | ||
stackit config profile list [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
List the configuration profiles | ||
$ stackit config profile list | ||
|
||
List the configuration profiles in a json format | ||
$ stackit config profile list --output-format json | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help Help for "stackit config profile list" | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-y, --assume-yes If set, skips all confirmation prompts | ||
--async If set, runs the command asynchronously | ||
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
-p, --project-id string Project ID | ||
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [stackit config profile](./stackit_config_profile.md) - Manage the CLI configuration profiles | ||
|
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,105 @@ | ||
package delete | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/auth" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/config" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/errors" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/print" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
profileArg = "PROFILE" | ||
) | ||
|
||
type inputModel struct { | ||
*globalflags.GlobalFlagModel | ||
Profile string | ||
} | ||
|
||
func NewCmd(p *print.Printer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: fmt.Sprintf("delete %s", profileArg), | ||
Short: "Delete a CLI configuration profile", | ||
Long: fmt.Sprintf("%s\n%s", | ||
"Delete a CLI configuration profile.", | ||
"If the deleted profile is the active profile, the default profile will be set to active.", | ||
), | ||
Args: args.SingleArg(profileArg, nil), | ||
Example: examples.Build( | ||
examples.NewExample( | ||
`Delete the configuration profile "my-profile"`, | ||
"$ stackit config profile delete my-profile"), | ||
), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
model, err := parseInput(p, cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
profileExists, err := config.ProfileExists(model.Profile) | ||
if err != nil { | ||
return fmt.Errorf("check if profile exists: %w", err) | ||
} | ||
if !profileExists { | ||
return &errors.DeleteInexistentProfile{Profile: model.Profile} | ||
} | ||
|
||
if !model.AssumeYes { | ||
prompt := fmt.Sprintf("Are you sure you want to delete profile %q? (This cannot be undone)", model.Profile) | ||
err = p.PromptForConfirmation(prompt) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
err = config.DeleteProfile(p, model.Profile) | ||
if err != nil { | ||
return fmt.Errorf("delete profile: %w", err) | ||
} | ||
|
||
err = auth.DeleteProfileFromKeyring(model.Profile) | ||
if err != nil { | ||
return fmt.Errorf("delete profile from keyring: %w", err) | ||
} | ||
|
||
p.Info("Successfully deleted profile %q\n", model.Profile) | ||
|
||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { | ||
profile := inputArgs[0] | ||
|
||
err := config.ValidateProfile(profile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
globalFlags := globalflags.Parse(p, cmd) | ||
|
||
model := inputModel{ | ||
GlobalFlagModel: globalFlags, | ||
Profile: profile, | ||
} | ||
|
||
if p.IsVerbosityDebug() { | ||
modelStr, err := print.BuildDebugStrFromInputModel(model) | ||
if err != nil { | ||
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) | ||
} else { | ||
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) | ||
} | ||
} | ||
|
||
return &model, nil | ||
} |
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,132 @@ | ||
package delete | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/print" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
const testProfile = "test-profile" | ||
|
||
func fixtureArgValues(mods ...func(argValues []string)) []string { | ||
argValues := []string{ | ||
testProfile, | ||
} | ||
for _, mod := range mods { | ||
mod(argValues) | ||
} | ||
return argValues | ||
} | ||
|
||
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { | ||
model := &inputModel{ | ||
GlobalFlagModel: &globalflags.GlobalFlagModel{ | ||
Verbosity: globalflags.VerbosityDefault, | ||
}, | ||
Profile: testProfile, | ||
} | ||
for _, mod := range mods { | ||
mod(model) | ||
} | ||
return model | ||
} | ||
|
||
func TestParseInput(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
argValues []string | ||
flagValues map[string]string | ||
isValid bool | ||
expectedModel *inputModel | ||
}{ | ||
{ | ||
description: "base", | ||
argValues: fixtureArgValues(), | ||
isValid: true, | ||
expectedModel: fixtureInputModel(), | ||
}, | ||
{ | ||
description: "no values", | ||
argValues: []string{}, | ||
flagValues: map[string]string{}, | ||
isValid: false, | ||
}, | ||
{ | ||
description: "no arg values", | ||
argValues: []string{}, | ||
isValid: false, | ||
}, | ||
{ | ||
description: "some global flag", | ||
argValues: fixtureArgValues(), | ||
flagValues: map[string]string{ | ||
globalflags.VerbosityFlag: globalflags.DebugVerbosity, | ||
}, | ||
isValid: true, | ||
expectedModel: fixtureInputModel(func(model *inputModel) { | ||
model.GlobalFlagModel.Verbosity = globalflags.DebugVerbosity | ||
}), | ||
}, | ||
{ | ||
description: "invalid profile", | ||
argValues: []string{"invalid-profile-&"}, | ||
isValid: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.description, func(t *testing.T) { | ||
p := print.NewPrinter() | ||
cmd := NewCmd(p) | ||
err := globalflags.Configure(cmd.Flags()) | ||
if err != nil { | ||
t.Fatalf("configure global flags: %v", err) | ||
} | ||
|
||
for flag, value := range tt.flagValues { | ||
err := cmd.Flags().Set(flag, value) | ||
if err != nil { | ||
if !tt.isValid { | ||
return | ||
} | ||
t.Fatalf("setting flag --%s=%s: %v", flag, value, err) | ||
} | ||
} | ||
|
||
err = cmd.ValidateArgs(tt.argValues) | ||
if err != nil { | ||
if !tt.isValid { | ||
return | ||
} | ||
t.Fatalf("error validating args: %v", err) | ||
} | ||
|
||
err = cmd.ValidateRequiredFlags() | ||
if err != nil { | ||
if !tt.isValid { | ||
return | ||
} | ||
t.Fatalf("error validating flags: %v", err) | ||
} | ||
|
||
model, err := parseInput(p, cmd, tt.argValues) | ||
if err != nil { | ||
if !tt.isValid { | ||
return | ||
} | ||
t.Fatalf("error parsing input: %v", err) | ||
} | ||
|
||
if !tt.isValid { | ||
t.Fatalf("did not fail on invalid input") | ||
} | ||
diff := cmp.Diff(model, tt.expectedModel) | ||
if diff != "" { | ||
t.Fatalf("Data does not match: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.