From 1e44ed3d92d6b4b10ab5b8282fafd137b22eb266 Mon Sep 17 00:00:00 2001 From: Damian Debkowski Date: Sat, 23 Jul 2022 09:50:52 -0700 Subject: [PATCH] fix(cli) check error value from command func (#2286) --- .../cmd/commands/accountscmd/accounts.gen.go | 33 ++++++++++----- .../commands/accountscmd/oidc_accounts.gen.go | 30 +++++++++----- .../accountscmd/password_accounts.gen.go | 30 +++++++++----- .../authmethodscmd/authmethods.gen.go | 33 ++++++++++----- .../authmethodscmd/oidc_authmethods.gen.go | 30 +++++++++----- .../password_authmethods.gen.go | 30 +++++++++----- .../commands/authtokenscmd/authtokens.gen.go | 33 ++++++++++----- .../credentiallibraries.gen.go | 33 ++++++++++----- .../vault_credentiallibraries.gen.go | 32 +++++++++------ .../credentialscmd/credentials.gen.go | 33 ++++++++++----- .../ssh-private-key_credentials.gen.go | 32 +++++++++------ .../username-password_credentials.gen.go | 32 +++++++++------ .../credentialstores.gen.go | 33 ++++++++++----- .../static_credentialstores.gen.go | 32 +++++++++------ .../vault_credentialstores.gen.go | 32 +++++++++------ internal/cmd/commands/groupscmd/groups.gen.go | 39 +++++++++++++----- .../hostcatalogscmd/hostcatalogs.gen.go | 33 ++++++++++----- .../plugin_hostcatalogs.gen.go | 30 +++++++++----- .../static_hostcatalogs.gen.go | 30 +++++++++----- internal/cmd/commands/hostscmd/hosts.gen.go | 33 ++++++++++----- .../cmd/commands/hostscmd/static_hosts.gen.go | 30 +++++++++----- .../cmd/commands/hostsetscmd/hostsets.gen.go | 33 ++++++++++----- .../hostsetscmd/plugin_hostsets.gen.go | 30 +++++++++----- .../hostsetscmd/static_hostsets.gen.go | 30 +++++++++----- .../managedgroupscmd/managedgroups.gen.go | 33 ++++++++++----- .../oidc_managedgroups.gen.go | 30 +++++++++----- internal/cmd/commands/rolescmd/roles.gen.go | 39 +++++++++++++----- internal/cmd/commands/scopescmd/scopes.gen.go | 39 +++++++++++++----- internal/cmd/commands/sessionscmd/funcs.go | 3 ++ .../cmd/commands/sessionscmd/sessions.gen.go | 30 +++++++++----- internal/cmd/commands/targetscmd/funcs.go | 27 ++++++++++++ .../commands/targetscmd/ssh_targets.gen.go | 30 +++++++++----- .../cmd/commands/targetscmd/targets.gen.go | 33 ++++++++++----- .../commands/targetscmd/tcp_targets.gen.go | 30 +++++++++----- internal/cmd/commands/userscmd/users.gen.go | 39 +++++++++++++----- .../workerscmd/worker-led_workers.gen.go | 24 ++++++----- .../cmd/commands/workerscmd/workers.gen.go | 36 +++++++++++----- internal/cmd/gencli/templates.go | 41 +++++++++++++------ 38 files changed, 828 insertions(+), 372 deletions(-) diff --git a/internal/cmd/commands/accountscmd/accounts.gen.go b/internal/cmd/commands/accountscmd/accounts.gen.go index 20d9167aa4..8c54686bf2 100644 --- a/internal/cmd/commands/accountscmd/accounts.gen.go +++ b/internal/cmd/commands/accountscmd/accounts.gen.go @@ -232,31 +232,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = accountsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = accountsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = accountsClient.List(c.Context, c.FlagAuthMethodId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, accountsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -311,6 +312,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/accountscmd/oidc_accounts.gen.go b/internal/cmd/commands/accountscmd/oidc_accounts.gen.go index ce2af86d3f..a49b3eb009 100644 --- a/internal/cmd/commands/accountscmd/oidc_accounts.gen.go +++ b/internal/cmd/commands/accountscmd/oidc_accounts.gen.go @@ -204,27 +204,25 @@ func (c *OidcCommand) Run(args []string) int { case "create": createResult, err = accountsClient.Create(c.Context, c.FlagAuthMethodId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = accountsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraOidcActions(c, resp, item, err, accountsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomOidcActionOutput(c) @@ -253,6 +251,18 @@ func (c *OidcCommand) Run(args []string) int { return base.CommandSuccess } +func (c *OidcCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraOidcActionsFlagsMapFunc = func() map[string][]string { return nil } extraOidcSynopsisFunc = func(*OidcCommand) string { return "" } diff --git a/internal/cmd/commands/accountscmd/password_accounts.gen.go b/internal/cmd/commands/accountscmd/password_accounts.gen.go index a60c974ebb..8936959c2d 100644 --- a/internal/cmd/commands/accountscmd/password_accounts.gen.go +++ b/internal/cmd/commands/accountscmd/password_accounts.gen.go @@ -204,27 +204,25 @@ func (c *PasswordCommand) Run(args []string) int { case "create": createResult, err = accountsClient.Create(c.Context, c.FlagAuthMethodId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = accountsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraPasswordActions(c, resp, item, err, accountsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomPasswordActionOutput(c) @@ -253,6 +251,18 @@ func (c *PasswordCommand) Run(args []string) int { return base.CommandSuccess } +func (c *PasswordCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraPasswordActionsFlagsMapFunc = func() map[string][]string { return nil } extraPasswordSynopsisFunc = func(*PasswordCommand) string { return "" } diff --git a/internal/cmd/commands/authmethodscmd/authmethods.gen.go b/internal/cmd/commands/authmethodscmd/authmethods.gen.go index 2a75631b1a..fbeae3ed70 100644 --- a/internal/cmd/commands/authmethodscmd/authmethods.gen.go +++ b/internal/cmd/commands/authmethodscmd/authmethods.gen.go @@ -199,31 +199,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = authmethodsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = authmethodsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = authmethodsClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, authmethodsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -278,6 +279,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/authmethodscmd/oidc_authmethods.gen.go b/internal/cmd/commands/authmethodscmd/oidc_authmethods.gen.go index a7f1f36d1b..29534844fb 100644 --- a/internal/cmd/commands/authmethodscmd/oidc_authmethods.gen.go +++ b/internal/cmd/commands/authmethodscmd/oidc_authmethods.gen.go @@ -217,27 +217,25 @@ func (c *OidcCommand) Run(args []string) int { case "create": createResult, err = authmethodsClient.Create(c.Context, "oidc", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraOidcActions(c, resp, item, err, authmethodsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomOidcActionOutput(c) @@ -266,6 +264,18 @@ func (c *OidcCommand) Run(args []string) int { return base.CommandSuccess } +func (c *OidcCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraOidcActionsFlagsMapFunc = func() map[string][]string { return nil } extraOidcSynopsisFunc = func(*OidcCommand) string { return "" } diff --git a/internal/cmd/commands/authmethodscmd/password_authmethods.gen.go b/internal/cmd/commands/authmethodscmd/password_authmethods.gen.go index 46c54d259e..bc54be4ba5 100644 --- a/internal/cmd/commands/authmethodscmd/password_authmethods.gen.go +++ b/internal/cmd/commands/authmethodscmd/password_authmethods.gen.go @@ -209,27 +209,25 @@ func (c *PasswordCommand) Run(args []string) int { case "create": createResult, err = authmethodsClient.Create(c.Context, "password", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraPasswordActions(c, resp, item, err, authmethodsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomPasswordActionOutput(c) @@ -258,6 +256,18 @@ func (c *PasswordCommand) Run(args []string) int { return base.CommandSuccess } +func (c *PasswordCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraPasswordActionsFlagsMapFunc = func() map[string][]string { return nil } extraPasswordSynopsisFunc = func(*PasswordCommand) string { return "" } diff --git a/internal/cmd/commands/authtokenscmd/authtokens.gen.go b/internal/cmd/commands/authtokenscmd/authtokens.gen.go index 228c0b53b9..f7cffee04d 100644 --- a/internal/cmd/commands/authtokenscmd/authtokens.gen.go +++ b/internal/cmd/commands/authtokenscmd/authtokens.gen.go @@ -194,31 +194,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = authtokensClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = authtokensClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = authtokensClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, authtokensClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -273,6 +274,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/credentiallibrariescmd/credentiallibraries.gen.go b/internal/cmd/commands/credentiallibrariescmd/credentiallibraries.gen.go index 3e100424ac..419f6c014d 100644 --- a/internal/cmd/commands/credentiallibrariescmd/credentiallibraries.gen.go +++ b/internal/cmd/commands/credentiallibrariescmd/credentiallibraries.gen.go @@ -194,31 +194,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = credentiallibrariesClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = credentiallibrariesClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = credentiallibrariesClient.List(c.Context, c.FlagCredentialStoreId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, credentiallibrariesClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -273,6 +274,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/credentiallibrariescmd/vault_credentiallibraries.gen.go b/internal/cmd/commands/credentiallibrariescmd/vault_credentiallibraries.gen.go index 33f91a885a..84a1768165 100644 --- a/internal/cmd/commands/credentiallibrariescmd/vault_credentiallibraries.gen.go +++ b/internal/cmd/commands/credentiallibrariescmd/vault_credentiallibraries.gen.go @@ -204,29 +204,25 @@ func (c *VaultCommand) Run(args []string) int { case "create": createResult, err = credentiallibrariesClient.Create(c.Context, c.FlagCredentialStoreId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = credentiallibrariesClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraVaultActions(c, resp, item, err, credentiallibrariesClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - opts = append(opts, base.WithAttributeFieldPrefix("vault")) - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomVaultActionOutput(c) @@ -255,6 +251,18 @@ func (c *VaultCommand) Run(args []string) int { return base.CommandSuccess } +func (c *VaultCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraVaultActionsFlagsMapFunc = func() map[string][]string { return nil } extraVaultSynopsisFunc = func(*VaultCommand) string { return "" } diff --git a/internal/cmd/commands/credentialscmd/credentials.gen.go b/internal/cmd/commands/credentialscmd/credentials.gen.go index 77635e8ae5..a668387056 100644 --- a/internal/cmd/commands/credentialscmd/credentials.gen.go +++ b/internal/cmd/commands/credentialscmd/credentials.gen.go @@ -194,31 +194,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = credentialsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = credentialsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = credentialsClient.List(c.Context, c.FlagCredentialStoreId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, credentialsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -273,6 +274,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/credentialscmd/ssh-private-key_credentials.gen.go b/internal/cmd/commands/credentialscmd/ssh-private-key_credentials.gen.go index 6603be771a..7dd7d48ca8 100644 --- a/internal/cmd/commands/credentialscmd/ssh-private-key_credentials.gen.go +++ b/internal/cmd/commands/credentialscmd/ssh-private-key_credentials.gen.go @@ -204,29 +204,25 @@ func (c *SshPrivateKeyCommand) Run(args []string) int { case "create": createResult, err = credentialsClient.Create(c.Context, "ssh_private_key", c.FlagCredentialStoreId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = credentialsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraSshPrivateKeyActions(c, resp, item, err, credentialsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - opts = append(opts, base.WithAttributeFieldPrefix("ssh_private_key")) - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomSshPrivateKeyActionOutput(c) @@ -255,6 +251,18 @@ func (c *SshPrivateKeyCommand) Run(args []string) int { return base.CommandSuccess } +func (c *SshPrivateKeyCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraSshPrivateKeyActionsFlagsMapFunc = func() map[string][]string { return nil } extraSshPrivateKeySynopsisFunc = func(*SshPrivateKeyCommand) string { return "" } diff --git a/internal/cmd/commands/credentialscmd/username-password_credentials.gen.go b/internal/cmd/commands/credentialscmd/username-password_credentials.gen.go index 5dc7a4316a..8bb664b0f4 100644 --- a/internal/cmd/commands/credentialscmd/username-password_credentials.gen.go +++ b/internal/cmd/commands/credentialscmd/username-password_credentials.gen.go @@ -204,29 +204,25 @@ func (c *UsernamePasswordCommand) Run(args []string) int { case "create": createResult, err = credentialsClient.Create(c.Context, "username_password", c.FlagCredentialStoreId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = credentialsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraUsernamePasswordActions(c, resp, item, err, credentialsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - opts = append(opts, base.WithAttributeFieldPrefix("username_password")) - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomUsernamePasswordActionOutput(c) @@ -255,6 +251,18 @@ func (c *UsernamePasswordCommand) Run(args []string) int { return base.CommandSuccess } +func (c *UsernamePasswordCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraUsernamePasswordActionsFlagsMapFunc = func() map[string][]string { return nil } extraUsernamePasswordSynopsisFunc = func(*UsernamePasswordCommand) string { return "" } diff --git a/internal/cmd/commands/credentialstorescmd/credentialstores.gen.go b/internal/cmd/commands/credentialstorescmd/credentialstores.gen.go index 47cc1403de..799028dca7 100644 --- a/internal/cmd/commands/credentialstorescmd/credentialstores.gen.go +++ b/internal/cmd/commands/credentialstorescmd/credentialstores.gen.go @@ -199,31 +199,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = credentialstoresClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = credentialstoresClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = credentialstoresClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, credentialstoresClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -278,6 +279,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/credentialstorescmd/static_credentialstores.gen.go b/internal/cmd/commands/credentialstorescmd/static_credentialstores.gen.go index 39394e602d..2f7b332463 100644 --- a/internal/cmd/commands/credentialstorescmd/static_credentialstores.gen.go +++ b/internal/cmd/commands/credentialstorescmd/static_credentialstores.gen.go @@ -207,29 +207,25 @@ func (c *StaticCommand) Run(args []string) int { case "create": createResult, err = credentialstoresClient.Create(c.Context, "static", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = credentialstoresClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraStaticActions(c, resp, item, err, credentialstoresClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - opts = append(opts, base.WithAttributeFieldPrefix("static")) - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomStaticActionOutput(c) @@ -258,6 +254,18 @@ func (c *StaticCommand) Run(args []string) int { return base.CommandSuccess } +func (c *StaticCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraStaticActionsFlagsMapFunc = func() map[string][]string { return nil } extraStaticSynopsisFunc = func(*StaticCommand) string { return "" } diff --git a/internal/cmd/commands/credentialstorescmd/vault_credentialstores.gen.go b/internal/cmd/commands/credentialstorescmd/vault_credentialstores.gen.go index 992acfad8d..256eb41815 100644 --- a/internal/cmd/commands/credentialstorescmd/vault_credentialstores.gen.go +++ b/internal/cmd/commands/credentialstorescmd/vault_credentialstores.gen.go @@ -209,29 +209,25 @@ func (c *VaultCommand) Run(args []string) int { case "create": createResult, err = credentialstoresClient.Create(c.Context, "vault", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = credentialstoresClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraVaultActions(c, resp, item, err, credentialstoresClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - opts = append(opts, base.WithAttributeFieldPrefix("vault")) - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomVaultActionOutput(c) @@ -260,6 +256,18 @@ func (c *VaultCommand) Run(args []string) int { return base.CommandSuccess } +func (c *VaultCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraVaultActionsFlagsMapFunc = func() map[string][]string { return nil } extraVaultSynopsisFunc = func(*VaultCommand) string { return "" } diff --git a/internal/cmd/commands/groupscmd/groups.gen.go b/internal/cmd/commands/groupscmd/groups.gen.go index 322bcd9d47..19c54c99af 100644 --- a/internal/cmd/commands/groupscmd/groups.gen.go +++ b/internal/cmd/commands/groupscmd/groups.gen.go @@ -267,41 +267,48 @@ func (c *Command) Run(args []string) int { case "create": createResult, err = groupsClient.Create(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "read": readResult, err = groupsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "update": updateResult, err = groupsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() case "delete": deleteResult, err = groupsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = groupsClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, groupsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -356,6 +363,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/hostcatalogscmd/hostcatalogs.gen.go b/internal/cmd/commands/hostcatalogscmd/hostcatalogs.gen.go index 2ac531ce84..1d9f899a3c 100644 --- a/internal/cmd/commands/hostcatalogscmd/hostcatalogs.gen.go +++ b/internal/cmd/commands/hostcatalogscmd/hostcatalogs.gen.go @@ -199,31 +199,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = hostcatalogsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = hostcatalogsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = hostcatalogsClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, hostcatalogsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -278,6 +279,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go b/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go index 5598377d87..c99c05c442 100644 --- a/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go +++ b/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go @@ -254,27 +254,25 @@ func (c *PluginCommand) Run(args []string) int { case "create": createResult, err = hostcatalogsClient.Create(c.Context, "plugin", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = hostcatalogsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraPluginActions(c, resp, item, err, hostcatalogsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomPluginActionOutput(c) @@ -303,6 +301,18 @@ func (c *PluginCommand) Run(args []string) int { return base.CommandSuccess } +func (c *PluginCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraPluginActionsFlagsMapFunc = func() map[string][]string { return nil } extraPluginSynopsisFunc = func(*PluginCommand) string { return "" } diff --git a/internal/cmd/commands/hostcatalogscmd/static_hostcatalogs.gen.go b/internal/cmd/commands/hostcatalogscmd/static_hostcatalogs.gen.go index 9a06a150d2..6c3980dba9 100644 --- a/internal/cmd/commands/hostcatalogscmd/static_hostcatalogs.gen.go +++ b/internal/cmd/commands/hostcatalogscmd/static_hostcatalogs.gen.go @@ -207,27 +207,25 @@ func (c *StaticCommand) Run(args []string) int { case "create": createResult, err = hostcatalogsClient.Create(c.Context, "static", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = hostcatalogsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraStaticActions(c, resp, item, err, hostcatalogsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomStaticActionOutput(c) @@ -256,6 +254,18 @@ func (c *StaticCommand) Run(args []string) int { return base.CommandSuccess } +func (c *StaticCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraStaticActionsFlagsMapFunc = func() map[string][]string { return nil } extraStaticSynopsisFunc = func(*StaticCommand) string { return "" } diff --git a/internal/cmd/commands/hostscmd/hosts.gen.go b/internal/cmd/commands/hostscmd/hosts.gen.go index 7f3939cb8b..db5fe6ab0d 100644 --- a/internal/cmd/commands/hostscmd/hosts.gen.go +++ b/internal/cmd/commands/hostscmd/hosts.gen.go @@ -210,31 +210,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = hostsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = hostsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = hostsClient.List(c.Context, c.FlagHostCatalogId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, hostsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -289,6 +290,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/hostscmd/static_hosts.gen.go b/internal/cmd/commands/hostscmd/static_hosts.gen.go index 522a63a730..2af64b76e0 100644 --- a/internal/cmd/commands/hostscmd/static_hosts.gen.go +++ b/internal/cmd/commands/hostscmd/static_hosts.gen.go @@ -204,27 +204,25 @@ func (c *StaticCommand) Run(args []string) int { case "create": createResult, err = hostsClient.Create(c.Context, c.FlagHostCatalogId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = hostsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraStaticActions(c, resp, item, err, hostsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomStaticActionOutput(c) @@ -253,6 +251,18 @@ func (c *StaticCommand) Run(args []string) int { return base.CommandSuccess } +func (c *StaticCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraStaticActionsFlagsMapFunc = func() map[string][]string { return nil } extraStaticSynopsisFunc = func(*StaticCommand) string { return "" } diff --git a/internal/cmd/commands/hostsetscmd/hostsets.gen.go b/internal/cmd/commands/hostsetscmd/hostsets.gen.go index abf671390d..afc0891e68 100644 --- a/internal/cmd/commands/hostsetscmd/hostsets.gen.go +++ b/internal/cmd/commands/hostsetscmd/hostsets.gen.go @@ -240,31 +240,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = hostsetsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = hostsetsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = hostsetsClient.List(c.Context, c.FlagHostCatalogId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, hostsetsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -319,6 +320,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/hostsetscmd/plugin_hostsets.gen.go b/internal/cmd/commands/hostsetscmd/plugin_hostsets.gen.go index d6e5e43a6c..0eeac1ba4e 100644 --- a/internal/cmd/commands/hostsetscmd/plugin_hostsets.gen.go +++ b/internal/cmd/commands/hostsetscmd/plugin_hostsets.gen.go @@ -222,27 +222,25 @@ func (c *PluginCommand) Run(args []string) int { case "create": createResult, err = hostsetsClient.Create(c.Context, c.FlagHostCatalogId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = hostsetsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraPluginActions(c, resp, item, err, hostsetsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomPluginActionOutput(c) @@ -271,6 +269,18 @@ func (c *PluginCommand) Run(args []string) int { return base.CommandSuccess } +func (c *PluginCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraPluginActionsFlagsMapFunc = func() map[string][]string { return nil } extraPluginSynopsisFunc = func(*PluginCommand) string { return "" } diff --git a/internal/cmd/commands/hostsetscmd/static_hostsets.gen.go b/internal/cmd/commands/hostsetscmd/static_hostsets.gen.go index 9aa885159b..1f0906d8d7 100644 --- a/internal/cmd/commands/hostsetscmd/static_hostsets.gen.go +++ b/internal/cmd/commands/hostsetscmd/static_hostsets.gen.go @@ -202,27 +202,25 @@ func (c *StaticCommand) Run(args []string) int { case "create": createResult, err = hostsetsClient.Create(c.Context, c.FlagHostCatalogId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = hostsetsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraStaticActions(c, resp, item, err, hostsetsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomStaticActionOutput(c) @@ -251,6 +249,18 @@ func (c *StaticCommand) Run(args []string) int { return base.CommandSuccess } +func (c *StaticCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraStaticActionsFlagsMapFunc = func() map[string][]string { return nil } extraStaticSynopsisFunc = func(*StaticCommand) string { return "" } diff --git a/internal/cmd/commands/managedgroupscmd/managedgroups.gen.go b/internal/cmd/commands/managedgroupscmd/managedgroups.gen.go index a7effdad7f..eb3a6d2f89 100644 --- a/internal/cmd/commands/managedgroupscmd/managedgroups.gen.go +++ b/internal/cmd/commands/managedgroupscmd/managedgroups.gen.go @@ -210,31 +210,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = managedgroupsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = managedgroupsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = managedgroupsClient.List(c.Context, c.FlagAuthMethodId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, managedgroupsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -289,6 +290,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/managedgroupscmd/oidc_managedgroups.gen.go b/internal/cmd/commands/managedgroupscmd/oidc_managedgroups.gen.go index 8182f5a4b8..c0b1e44aa8 100644 --- a/internal/cmd/commands/managedgroupscmd/oidc_managedgroups.gen.go +++ b/internal/cmd/commands/managedgroupscmd/oidc_managedgroups.gen.go @@ -204,27 +204,25 @@ func (c *OidcCommand) Run(args []string) int { case "create": createResult, err = managedgroupsClient.Create(c.Context, c.FlagAuthMethodId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = managedgroupsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraOidcActions(c, resp, item, err, managedgroupsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomOidcActionOutput(c) @@ -253,6 +251,18 @@ func (c *OidcCommand) Run(args []string) int { return base.CommandSuccess } +func (c *OidcCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraOidcActionsFlagsMapFunc = func() map[string][]string { return nil } extraOidcSynopsisFunc = func(*OidcCommand) string { return "" } diff --git a/internal/cmd/commands/rolescmd/roles.gen.go b/internal/cmd/commands/rolescmd/roles.gen.go index 898e92078f..9aaab02c4c 100644 --- a/internal/cmd/commands/rolescmd/roles.gen.go +++ b/internal/cmd/commands/rolescmd/roles.gen.go @@ -291,41 +291,48 @@ func (c *Command) Run(args []string) int { case "create": createResult, err = rolesClient.Create(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "read": readResult, err = rolesClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "update": updateResult, err = rolesClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() case "delete": deleteResult, err = rolesClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = rolesClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, rolesClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -380,6 +387,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/scopescmd/scopes.gen.go b/internal/cmd/commands/scopescmd/scopes.gen.go index 3c75240a8b..e00bcd08bf 100644 --- a/internal/cmd/commands/scopescmd/scopes.gen.go +++ b/internal/cmd/commands/scopescmd/scopes.gen.go @@ -243,41 +243,48 @@ func (c *Command) Run(args []string) int { case "create": createResult, err = scopesClient.Create(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "read": readResult, err = scopesClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "update": updateResult, err = scopesClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() case "delete": deleteResult, err = scopesClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = scopesClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, scopesClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -332,6 +339,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/sessionscmd/funcs.go b/internal/cmd/commands/sessionscmd/funcs.go index f1acfe74b6..0f112c6f7a 100644 --- a/internal/cmd/commands/sessionscmd/funcs.go +++ b/internal/cmd/commands/sessionscmd/funcs.go @@ -90,6 +90,9 @@ func executeExtraActionsImpl(c *Command, origResp *api.Response, origItem *sessi switch c.Func { case "cancel": result, err := sessionClient.Cancel(c.Context, c.FlagId, version, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err } return origResp, origItem, origItems, origError diff --git a/internal/cmd/commands/sessionscmd/sessions.gen.go b/internal/cmd/commands/sessionscmd/sessions.gen.go index 1eb0974cb3..1e656bca5d 100644 --- a/internal/cmd/commands/sessionscmd/sessions.gen.go +++ b/internal/cmd/commands/sessionscmd/sessions.gen.go @@ -206,27 +206,25 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = sessionsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "list": listResult, err = sessionsClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, sessionsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -268,6 +266,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/targetscmd/funcs.go b/internal/cmd/commands/targetscmd/funcs.go index 84aec71c26..29a4333c96 100644 --- a/internal/cmd/commands/targetscmd/funcs.go +++ b/internal/cmd/commands/targetscmd/funcs.go @@ -466,30 +466,57 @@ func executeExtraActionsImpl(c *Command, origResp *api.Response, origItem *targe switch c.Func { case "add-host-sets": result, err := targetClient.AddHostSets(c.Context, c.FlagId, version, c.flagHostSets, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "remove-host-sets": result, err := targetClient.RemoveHostSets(c.Context, c.FlagId, version, c.flagHostSets, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "set-host-sets": result, err := targetClient.SetHostSets(c.Context, c.FlagId, version, c.flagHostSets, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "add-host-sources": result, err := targetClient.AddHostSources(c.Context, c.FlagId, version, c.flagHostSources, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "remove-host-sources": result, err := targetClient.RemoveHostSources(c.Context, c.FlagId, version, c.flagHostSources, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "set-host-sources": result, err := targetClient.SetHostSources(c.Context, c.FlagId, version, c.flagHostSources, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "add-credential-sources": result, err := targetClient.AddCredentialSources(c.Context, c.FlagId, version, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "remove-credential-sources": result, err := targetClient.RemoveCredentialSources(c.Context, c.FlagId, version, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "set-credential-sources": result, err := targetClient.SetCredentialSources(c.Context, c.FlagId, version, opts...) + if err != nil { + return nil, nil, nil, err + } return result.GetResponse(), result.GetItem(), nil, err case "authorize-session": var err error diff --git a/internal/cmd/commands/targetscmd/ssh_targets.gen.go b/internal/cmd/commands/targetscmd/ssh_targets.gen.go index b5446c4bea..73fa4c1d2d 100644 --- a/internal/cmd/commands/targetscmd/ssh_targets.gen.go +++ b/internal/cmd/commands/targetscmd/ssh_targets.gen.go @@ -209,27 +209,25 @@ func (c *SshCommand) Run(args []string) int { case "create": createResult, err = targetsClient.Create(c.Context, "ssh", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = targetsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraSshActions(c, resp, item, err, targetsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomSshActionOutput(c) @@ -258,6 +256,18 @@ func (c *SshCommand) Run(args []string) int { return base.CommandSuccess } +func (c *SshCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraSshActionsFlagsMapFunc = func() map[string][]string { return nil } extraSshSynopsisFunc = func(*SshCommand) string { return "" } diff --git a/internal/cmd/commands/targetscmd/targets.gen.go b/internal/cmd/commands/targetscmd/targets.gen.go index c6f3818f05..a48922dc25 100644 --- a/internal/cmd/commands/targetscmd/targets.gen.go +++ b/internal/cmd/commands/targetscmd/targets.gen.go @@ -294,31 +294,32 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = targetsClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "delete": deleteResult, err = targetsClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = targetsClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, targetsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -373,6 +374,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/targetscmd/tcp_targets.gen.go b/internal/cmd/commands/targetscmd/tcp_targets.gen.go index 9acde05f5b..f682b1a00e 100644 --- a/internal/cmd/commands/targetscmd/tcp_targets.gen.go +++ b/internal/cmd/commands/targetscmd/tcp_targets.gen.go @@ -209,27 +209,25 @@ func (c *TcpCommand) Run(args []string) int { case "create": createResult, err = targetsClient.Create(c.Context, "tcp", c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "update": updateResult, err = targetsClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() } resp, item, err = executeExtraTcpActions(c, resp, item, err, targetsClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomTcpActionOutput(c) @@ -258,6 +256,18 @@ func (c *TcpCommand) Run(args []string) int { return base.CommandSuccess } +func (c *TcpCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraTcpActionsFlagsMapFunc = func() map[string][]string { return nil } extraTcpSynopsisFunc = func(*TcpCommand) string { return "" } diff --git a/internal/cmd/commands/userscmd/users.gen.go b/internal/cmd/commands/userscmd/users.gen.go index ad868dd783..0cec31494b 100644 --- a/internal/cmd/commands/userscmd/users.gen.go +++ b/internal/cmd/commands/userscmd/users.gen.go @@ -267,41 +267,48 @@ func (c *Command) Run(args []string) int { case "create": createResult, err = usersClient.Create(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = createResult.GetResponse() item = createResult.GetItem() case "read": readResult, err = usersClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "update": updateResult, err = usersClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() case "delete": deleteResult, err = usersClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = usersClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, usersClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -356,6 +363,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/commands/workerscmd/worker-led_workers.gen.go b/internal/cmd/commands/workerscmd/worker-led_workers.gen.go index b202e37fff..bc74407c2b 100644 --- a/internal/cmd/commands/workerscmd/worker-led_workers.gen.go +++ b/internal/cmd/commands/workerscmd/worker-led_workers.gen.go @@ -195,16 +195,8 @@ func (c *WorkerLedCommand) Run(args []string) int { } resp, item, err = executeExtraWorkerLedActions(c, resp, item, err, workersClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomWorkerLedActionOutput(c) @@ -233,6 +225,18 @@ func (c *WorkerLedCommand) Run(args []string) int { return base.CommandSuccess } +func (c *WorkerLedCommand) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( extraWorkerLedActionsFlagsMapFunc = func() map[string][]string { return nil } extraWorkerLedSynopsisFunc = func(*WorkerLedCommand) string { return "" } diff --git a/internal/cmd/commands/workerscmd/workers.gen.go b/internal/cmd/commands/workerscmd/workers.gen.go index b43abc98f8..738e4535bf 100644 --- a/internal/cmd/commands/workerscmd/workers.gen.go +++ b/internal/cmd/commands/workerscmd/workers.gen.go @@ -231,36 +231,40 @@ func (c *Command) Run(args []string) int { case "read": readResult, err = workersClient.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = readResult.GetResponse() item = readResult.GetItem() case "update": updateResult, err = workersClient.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = updateResult.GetResponse() item = updateResult.GetItem() case "delete": deleteResult, err = workersClient.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = deleteResult.GetResponse() case "list": listResult, err = workersClient.List(c.Context, c.FlagScopeId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = listResult.GetResponse() items = listResult.GetItems() } resp, item, items, err = executeExtraActions(c, resp, item, items, err, workersClient, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustomActionOutput(c) @@ -315,6 +319,18 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } +func (c *Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( flagsOnce = new(sync.Once) diff --git a/internal/cmd/gencli/templates.go b/internal/cmd/gencli/templates.go index 732f2358a1..b2b7cd6fc9 100644 --- a/internal/cmd/gencli/templates.go +++ b/internal/cmd/gencli/templates.go @@ -416,6 +416,9 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int { {{ if ( not ( hasAction $input.SkipClientCallActions "create") ) }} case "create": {{ $action }}Result, err = {{ $input.Pkg }}Client.Create(c.Context, {{ if (and $input.SubActionPrefix $input.NeedsSubtypeInCreate) }}"{{ $input.SubActionPrefix }}",{{ end }} c.Flag{{ $input.Container }}Id, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = {{ $action }}Result.GetResponse() item = {{ $action }}Result.GetItem() {{ end }} @@ -423,23 +426,35 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int { {{ if eq $action "read" }} case "read": {{ $action }}Result, err = {{ $input.Pkg }}Client.Read(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = {{ $action }}Result.GetResponse() item = {{ $action }}Result.GetItem() {{ end }} {{ if eq $action "update" }} case "update": {{ $action }}Result, err = {{ $input.Pkg }}Client.Update(c.Context, c.FlagId, version, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = {{ $action }}Result.GetResponse() item = {{ $action }}Result.GetItem() {{ end }} {{ if eq $action "delete" }} case "delete": {{ $action }}Result, err = {{ $input.Pkg }}Client.Delete(c.Context, c.FlagId, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = {{ $action }}Result.GetResponse() {{ end }} {{ if eq $action "list" }} case "list": {{ $action }}Result, err = {{ $input.Pkg}}Client.List(c.Context, c.Flag{{ $input.Container }}Id, opts...) + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode + } resp = {{ $action }}Result.GetResponse() items = {{ $action }}Result.GetItems() {{ end }} @@ -447,18 +462,8 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int { } resp, item, {{ if hasAction .StdActions "list" }}items, {{ end }}err = executeExtra{{ camelCase .SubActionPrefix }}Actions(c, resp, item, {{ if hasAction .StdActions "list" }}items, {{ end }}err, {{ .Pkg }}Client, version, opts) - - if err != nil { - if apiErr := api.AsServerError(err); apiErr != nil { - var opts []base.Option - {{ if (and $input.SubActionPrefix $input.PrefixAttributeFieldErrorsWithSubactionPrefix ) }} - opts = append(opts, base.WithAttributeFieldPrefix("{{ $input.SubActionPrefix }}")) - {{ end }} - c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...) - return base.CommandApiError - } - c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) - return base.CommandCliError + if exitCode := c.checkFuncError(err); exitCode > 0 { + return exitCode } output, err := printCustom{{ camelCase .SubActionPrefix }}ActionOutput(c) @@ -516,6 +521,18 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int { return base.CommandSuccess } +func (c *{{ camelCase .SubActionPrefix }}Command) checkFuncError(err error) int { + if err == nil { + return 0 + } + if apiErr := api.AsServerError(err); apiErr != nil { + c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural)) + return base.CommandApiError + } + c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error())) + return base.CommandCliError +} + var ( {{ if not .SubActionPrefix }} flagsOnce = new(sync.Once)