Skip to content
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

OCM-1888: Add docs for ocm delete account command #592

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions cmd/ocm/delete/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,49 @@ var args struct {
}

var Cmd = &cobra.Command{
Use: "delete [flags] PATH",
Use: "delete [flags] (PATH | RESOURCE_ALIAS RESOURCE_ID)",
mnecas marked this conversation as resolved.
Show resolved Hide resolved
Short: "Send a DELETE request",
Long: "Send a DELETE request to the given path.",
RunE: run,
ValidArgs: urls.Resources(),
}

// for template format refer: https://pkg.go.dev/text/template
var usageTemplate = `
Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if .HasExample}}

Resource Alias:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}

Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}

Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`

var helpTemplate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
cgiradkar marked this conversation as resolved.
Show resolved Hide resolved
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`

func init() {
Cmd.SetUsageTemplate(usageTemplate)
Cmd.SetHelpTemplate(helpTemplate)
Cmd.Example = " account\n addon\n cluster\n role_binding\n sku_rule\n subscription"
fs := Cmd.Flags()
arguments.AddParameterFlag(fs, &args.parameter)
arguments.AddHeaderFlag(fs, &args.header)
Expand All @@ -61,13 +96,13 @@ func init() {
func run(cmd *cobra.Command, argv []string) error {
path, err := urls.Expand(argv)
if err != nil {
return fmt.Errorf("Could not create URI: %v", err)
return fmt.Errorf("could not create URI: %w", err)
}

// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return fmt.Errorf("Failed to create OCM connection: %v", err)
return fmt.Errorf("failed to create OCM connection: %w", err)
}
defer connection.Close()

Expand All @@ -84,7 +119,7 @@ func run(cmd *cobra.Command, argv []string) error {
// Send the request:
response, err := request.Send()
if err != nil {
return fmt.Errorf("Can't send request: %v", err)
return fmt.Errorf("can't send request: %w", err)
}
status := response.Status()
body := response.Bytes()
Expand All @@ -94,26 +129,26 @@ func run(cmd *cobra.Command, argv []string) error {
err = dump.Pretty(os.Stderr, body)
}
if err != nil {
return fmt.Errorf("Can't print body: %v", err)
return fmt.Errorf("can't print body: %w", err)
}

// Load the configuration file:
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("Can't load config file: %v", err)
return fmt.Errorf("can't load config file: %w", err)
}
if cfg == nil {
return fmt.Errorf("Not logged in, run the 'login' command")
return fmt.Errorf("not logged in, run the 'login' command")
}

// Save the configuration:
cfg.AccessToken, cfg.RefreshToken, err = connection.Tokens()
if err != nil {
return fmt.Errorf("Can't get tokens: %v", err)
return fmt.Errorf("can't get tokens: %w", err)
}
err = config.Save(cfg)
if err != nil {
return fmt.Errorf("Can't save config file: %v", err)
return fmt.Errorf("can't save config file: %w", err)
}

// Bye:
Expand Down
Loading