From 7baf86a50055a3c21dd8e1a3d811728b0686e55d Mon Sep 17 00:00:00 2001 From: Henrique Santos Date: Tue, 21 Nov 2023 12:41:09 +0000 Subject: [PATCH 1/4] Replace inits with constructors --- .../activate_service_account.go | 84 +++++++-------- internal/cmd/auth/auth.go | 21 ++-- internal/cmd/auth/login/login.go | 32 +++--- internal/cmd/config/config.go | 22 ++-- internal/cmd/config/inspect/inspect.go | 39 +++---- internal/cmd/config/set/set.go | 52 ++++----- internal/cmd/config/unset/unset.go | 58 +++++----- internal/cmd/dns/dns.go | 20 ++-- internal/cmd/dns/record-set/create/create.go | 78 +++++++------- internal/cmd/dns/record-set/delete/delete.go | 82 +++++++------- .../cmd/dns/record-set/describe/describe.go | 78 +++++++------- internal/cmd/dns/record-set/list/list.go | 88 +++++++-------- internal/cmd/dns/record-set/record_set.go | 27 ++--- internal/cmd/dns/record-set/update/update.go | 76 ++++++------- internal/cmd/dns/zone/create/create.go | 78 +++++++------- internal/cmd/dns/zone/delete/delete.go | 82 +++++++------- internal/cmd/dns/zone/describe/describe.go | 70 ++++++------ internal/cmd/dns/zone/list/list.go | 86 +++++++-------- internal/cmd/dns/zone/update/update.go | 82 +++++++------- internal/cmd/dns/zone/zone.go | 27 ++--- .../postgresql/credential/create/create.go | 58 +++++----- .../cmd/postgresql/credential/credential.go | 24 +++-- .../postgresql/credential/delete/delete.go | 58 +++++----- .../credential/describe/describe.go | 76 ++++++------- .../cmd/postgresql/credential/list/list.go | 86 +++++++-------- .../cmd/postgresql/instance/create/create.go | 84 +++++++-------- .../cmd/postgresql/instance/delete/delete.go | 68 ++++++------ .../postgresql/instance/describe/describe.go | 68 ++++++------ internal/cmd/postgresql/instance/instance.go | 26 +++-- internal/cmd/postgresql/instance/list/list.go | 77 ++++++------- .../cmd/postgresql/instance/update/update.go | 84 +++++++-------- .../cmd/postgresql/offerings/list/list.go | 77 ++++++------- .../cmd/postgresql/offerings/offerings.go | 19 ++-- internal/cmd/postgresql/postgresql.go | 22 ++-- internal/cmd/root.go | 41 ++++--- internal/cmd/ske/cluster/cluster.go | 26 +++-- internal/cmd/ske/cluster/create/create.go | 102 +++++++++--------- internal/cmd/ske/cluster/delete/delete.go | 68 ++++++------ internal/cmd/ske/cluster/describe/describe.go | 69 ++++++------ internal/cmd/ske/cluster/list/list.go | 77 ++++++------- internal/cmd/ske/cluster/update/update.go | 92 ++++++++-------- internal/cmd/ske/ske.go | 18 ++-- main.go | 4 + scripts/generate.go | 2 +- 44 files changed, 1281 insertions(+), 1227 deletions(-) diff --git a/internal/cmd/auth/activate-service-account/activate_service_account.go b/internal/cmd/auth/activate-service-account/activate_service_account.go index 59117010..808a4551 100644 --- a/internal/cmd/auth/activate-service-account/activate_service_account.go +++ b/internal/cmd/auth/activate-service-account/activate_service_account.go @@ -27,48 +27,48 @@ type flagModel struct { JwksCustomEndpoint string } -var Cmd = &cobra.Command{ - Use: "activate-service-account", - Short: "Activate service account authentication", - Long: "Activate authentication using service account credentials.\nFor more details on how to configure your service account, check the Authentication section on our documentation (LINK HERE README)", - Example: `$ stackit auth activate-service-account --service-account-key-path path/to/service_account_key.json --private-key-path path/to/private_key.pem`, - RunE: func(cmd *cobra.Command, args []string) error { - model := parseFlags(cmd) - - err := storeFlags(model) - if err != nil { - return err - } - - cfg := &sdkConfig.Configuration{ - Token: model.ServiceAccountToken, - ServiceAccountKeyPath: model.ServiceAccountKeyPath, - PrivateKeyPath: model.PrivateKeyPath, - TokenCustomUrl: model.TokenCustomEndpoint, - JWKSCustomUrl: model.JwksCustomEndpoint, - } - - // Setup authentication based on the provided credentials and the environment - // Initializes the authentication flow - rt, err := sdkAuth.SetupAuth(cfg) - if err != nil { - return fmt.Errorf("set up authentication: %w", err) - } - - // Authenticates the service account and stores credentials - email, err := auth.AuthenticateServiceAccount(rt) - if err != nil { - return fmt.Errorf("authenticate service account: %w", err) - } - - cmd.Printf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email) - - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "activate-service-account", + Short: "Activate service account authentication", + Long: "Activate authentication using service account credentials.\nFor more details on how to configure your service account, check the Authentication section on our documentation (LINK HERE README)", + Example: `$ stackit auth activate-service-account --service-account-key-path path/to/service_account_key.json --private-key-path path/to/private_key.pem`, + RunE: func(cmd *cobra.Command, args []string) error { + model := parseFlags(cmd) + + err := storeFlags(model) + if err != nil { + return err + } + + cfg := &sdkConfig.Configuration{ + Token: model.ServiceAccountToken, + ServiceAccountKeyPath: model.ServiceAccountKeyPath, + PrivateKeyPath: model.PrivateKeyPath, + TokenCustomUrl: model.TokenCustomEndpoint, + JWKSCustomUrl: model.JwksCustomEndpoint, + } + + // Setup authentication based on the provided credentials and the environment + // Initializes the authentication flow + rt, err := sdkAuth.SetupAuth(cfg) + if err != nil { + return fmt.Errorf("set up authentication: %w", err) + } + + // Authenticates the service account and stores credentials + email, err := auth.AuthenticateServiceAccount(rt) + if err != nil { + return fmt.Errorf("authenticate service account: %w", err) + } + + cmd.Printf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email) + + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/auth/auth.go b/internal/cmd/auth/auth.go index 951e52ef..2483897b 100644 --- a/internal/cmd/auth/auth.go +++ b/internal/cmd/auth/auth.go @@ -7,15 +7,18 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "auth", - Short: "Provides authentication functionality", - Long: "Provides authentication functionality", - Example: `$ stackit auth login`, +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "auth", + Short: "Provides authentication functionality", + Long: "Provides authentication functionality", + Example: `$ stackit auth login`, + } + addChilds(cmd) + return cmd } -func init() { - // Add all direct child commands - Cmd.AddCommand(login.Cmd) - Cmd.AddCommand(activateserviceaccount.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(login.NewCmd()) + cmd.AddCommand(activateserviceaccount.NewCmd()) } diff --git a/internal/cmd/auth/login/login.go b/internal/cmd/auth/login/login.go index ef4c6c34..a5b2abb0 100644 --- a/internal/cmd/auth/login/login.go +++ b/internal/cmd/auth/login/login.go @@ -8,21 +8,21 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "login", - Short: "Login to the provider", - Long: "Login to the provider", - Example: `$ stackit auth login`, - RunE: func(cmd *cobra.Command, args []string) error { - err := auth.AuthorizeUser() - if err != nil { - return fmt.Errorf("authorization failed: %w", err) - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "login", + Short: "Login to the provider", + Long: "Login to the provider", + Example: `$ stackit auth login`, + RunE: func(cmd *cobra.Command, args []string) error { + err := auth.AuthorizeUser() + if err != nil { + return fmt.Errorf("authorization failed: %w", err) + } - cmd.Println("Successfully logged into STACKIT CLI.") - return nil - }, -} - -func init() { + cmd.Println("Successfully logged into STACKIT CLI.") + return nil + }, + } + return cmd } diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index b198b124..ebc49443 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -10,15 +10,19 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "config", - Short: "CLI configuration options", - Long: "CLI configuration options", - Example: fmt.Sprintf("%s\n%s\n%s", set.Cmd.Example, inspect.Cmd.Example, unset.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "config", + Short: "CLI configuration options", + Long: "CLI configuration options", + Example: fmt.Sprintf("%s\n%s\n%s", set.NewCmd().Example, inspect.NewCmd().Example, unset.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(inspect.Cmd) - Cmd.AddCommand(set.Cmd) - Cmd.AddCommand(unset.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(inspect.NewCmd()) + cmd.AddCommand(set.NewCmd()) + cmd.AddCommand(unset.NewCmd()) } diff --git a/internal/cmd/config/inspect/inspect.go b/internal/cmd/config/inspect/inspect.go index c8df01d7..1433d581 100644 --- a/internal/cmd/config/inspect/inspect.go +++ b/internal/cmd/config/inspect/inspect.go @@ -8,25 +8,28 @@ import ( "github.com/spf13/viper" ) -var Cmd = &cobra.Command{ - Use: "inspect", - Short: "Inspect the current CLI configuration values", - Long: "Inspect the current CLI configuration values", - Example: `$ stackit config inspect`, - RunE: func(cmd *cobra.Command, args []string) error { - err := viper.ReadInConfig() - if err != nil { - return fmt.Errorf("read config file: %w", err) - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "inspect", + Short: "Inspect the current CLI configuration values", + Long: "Inspect the current CLI configuration values", + Example: `$ stackit config inspect`, + RunE: func(cmd *cobra.Command, args []string) error { + err := viper.ReadInConfig() + if err != nil { + return fmt.Errorf("read config file: %w", err) + } - configData := viper.AllSettings() + configData := viper.AllSettings() - configJSON, err := json.MarshalIndent(configData, "", " ") - if err != nil { - return fmt.Errorf("marshal config: %w", err) - } - cmd.Println(string(configJSON)) + configJSON, err := json.MarshalIndent(configData, "", " ") + if err != nil { + return fmt.Errorf("marshal config: %w", err) + } + cmd.Println(string(configJSON)) - return nil - }, + return nil + }, + } + return cmd } diff --git a/internal/cmd/config/set/set.go b/internal/cmd/config/set/set.go index 7e03fba4..97760188 100644 --- a/internal/cmd/config/set/set.go +++ b/internal/cmd/config/set/set.go @@ -23,32 +23,32 @@ type flagModel struct { SessionTimeLimit *string } -var Cmd = &cobra.Command{ - Use: "set", - Short: "Set CLI configuration options", - Long: "Set CLI configuration options", - Example: `$ stackit config set --project-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - model, err := parseFlags(cmd) - if err != nil { - return err - } - - if model.SessionTimeLimit != nil { - cmd.Println("Authenticate again to apply changes to session time limit") - viper.Set(config.SessionTimeLimitKey, *model.SessionTimeLimit) - } - - err = viper.WriteConfig() - if err != nil { - return fmt.Errorf("write new config to file: %w", err) - } - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "set", + Short: "Set CLI configuration options", + Long: "Set CLI configuration options", + Example: `$ stackit config set --project-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + model, err := parseFlags(cmd) + if err != nil { + return err + } + + if model.SessionTimeLimit != nil { + cmd.Println("Authenticate again to apply changes to session time limit") + viper.Set(config.SessionTimeLimitKey, *model.SessionTimeLimit) + } + + err = viper.WriteConfig() + if err != nil { + return fmt.Errorf("write new config to file: %w", err) + } + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/config/unset/unset.go b/internal/cmd/config/unset/unset.go index 265aa8d9..50fcc680 100644 --- a/internal/cmd/config/unset/unset.go +++ b/internal/cmd/config/unset/unset.go @@ -24,37 +24,37 @@ type flagModel struct { SKECustomEndpoint bool } -var Cmd = &cobra.Command{ - Use: "unset", - Short: "Unset CLI configuration options", - Long: "Unset CLI configuration options", - Example: `$ stackit config unset --project-id`, - RunE: func(cmd *cobra.Command, args []string) error { - model := parseFlags(cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "unset", + Short: "Unset CLI configuration options", + Long: "Unset CLI configuration options", + Example: `$ stackit config unset --project-id`, + RunE: func(cmd *cobra.Command, args []string) error { + model := parseFlags(cmd) - if model.ProjectId { - viper.Set(config.ProjectIdKey, "") - } - if model.DNSCustomEndpoint { - viper.Set(config.DNSCustomEndpointKey, "") - } - if model.PostgreSQLCustomEndpoint { - viper.Set(config.PostgreSQLCustomEndpointKey, "") - } - if model.PostgreSQLCustomEndpoint { - viper.Set(config.SKECustomEndpointKey, "") - } + if model.ProjectId { + viper.Set(config.ProjectIdKey, "") + } + if model.DNSCustomEndpoint { + viper.Set(config.DNSCustomEndpointKey, "") + } + if model.PostgreSQLCustomEndpoint { + viper.Set(config.PostgreSQLCustomEndpointKey, "") + } + if model.PostgreSQLCustomEndpoint { + viper.Set(config.SKECustomEndpointKey, "") + } - err := viper.WriteConfig() - if err != nil { - return fmt.Errorf("write updated config to file: %w", err) - } - return nil - }, -} - -func init() { - configureFlags(Cmd) + err := viper.WriteConfig() + if err != nil { + return fmt.Errorf("write updated config to file: %w", err) + } + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/dns.go b/internal/cmd/dns/dns.go index 8c911763..a9ef026d 100644 --- a/internal/cmd/dns/dns.go +++ b/internal/cmd/dns/dns.go @@ -9,14 +9,18 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "dns", - Short: "Provides functionality for DNS", - Long: "Provides functionality for DNS", - Example: fmt.Sprintf("%s\n%s", zone.Cmd.Example, recordset.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "dns", + Short: "Provides functionality for DNS", + Long: "Provides functionality for DNS", + Example: fmt.Sprintf("%s\n%s", zone.NewCmd().Example, recordset.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(zone.Cmd) - Cmd.AddCommand(recordset.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(zone.NewCmd()) + cmd.AddCommand(recordset.NewCmd()) } diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index 9ed60b86..24e7b5c1 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -35,45 +35,45 @@ type flagModel struct { Type *string } -var Cmd = &cobra.Command{ - Use: "create", - Short: "Creates a DNS record set", - Long: "Creates a DNS record set", - Example: `$ stackit dns record-set create --project-id xxx --zone-id xxx --name my-zone --type A --record 1.2.3.4 --record 5.6.7.8`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("create DNS record set: %w", err) - } - - // Wait for async operation - recordSetId := *resp.Rrset.Id - _, err = wait.CreateRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, recordSetId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for DNS record set creation: %w", err) - } - - cmd.Printf("Created record set with ID %s\n", recordSetId) - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Creates a DNS record set", + Long: "Creates a DNS record set", + Example: `$ stackit dns record-set create --project-id xxx --zone-id xxx --name my-zone --type A --record 1.2.3.4 --record 5.6.7.8`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("create DNS record set: %w", err) + } + + // Wait for async operation + recordSetId := *resp.Rrset.Id + _, err = wait.CreateRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, recordSetId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for DNS record set creation: %w", err) + } + + cmd.Printf("Created record set with ID %s\n", recordSetId) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/record-set/delete/delete.go b/internal/cmd/dns/record-set/delete/delete.go index 673f7ce4..39cdf707 100644 --- a/internal/cmd/dns/record-set/delete/delete.go +++ b/internal/cmd/dns/record-set/delete/delete.go @@ -27,47 +27,47 @@ type flagModel struct { RecordSetId string } -var Cmd = &cobra.Command{ - Use: "delete", - Short: "Delete a DNS record set", - Long: "Delete a DNS record set", - Example: `$ stackit dns record-set delete --project-id xxx --zone-id xxx --record-set-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - if err != nil { - return err - } - _, err = req.Execute() - if err != nil { - return fmt.Errorf("delete DNS record set: %w", err) - } - - // Wait for async operation - _, err = wait.DeleteRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for DNS record set deletion: %w", err) - } - - cmd.Println("Record set deleted") - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a DNS record set", + Long: "Delete a DNS record set", + Example: `$ stackit dns record-set delete --project-id xxx --zone-id xxx --record-set-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + if err != nil { + return err + } + _, err = req.Execute() + if err != nil { + return fmt.Errorf("delete DNS record set: %w", err) + } + + // Wait for async operation + _, err = wait.DeleteRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for DNS record set deletion: %w", err) + } + + cmd.Println("Record set deleted") + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/record-set/describe/describe.go b/internal/cmd/dns/record-set/describe/describe.go index 92f3d8de..d066cf32 100644 --- a/internal/cmd/dns/record-set/describe/describe.go +++ b/internal/cmd/dns/record-set/describe/describe.go @@ -27,45 +27,45 @@ type flagModel struct { RecordSetId string } -var Cmd = &cobra.Command{ - Use: "describe", - Short: "Get details of a DNS record set", - Long: "Get details of a DNS record set", - Example: `$ stackit dns record-set describe --project-id xxx --zone-id xxx --record-set-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("read DNS record set: %w", err) - } - recordSet := *resp.Rrset - - // Show details - details, err := json.MarshalIndent(recordSet, "", " ") - if err != nil { - return fmt.Errorf("marshal DNS record set: %w", err) - } - cmd.Println(string(details)) - - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "describe", + Short: "Get details of a DNS record set", + Long: "Get details of a DNS record set", + Example: `$ stackit dns record-set describe --project-id xxx --zone-id xxx --record-set-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("read DNS record set: %w", err) + } + recordSet := *resp.Rrset + + // Show details + details, err := json.MarshalIndent(recordSet, "", " ") + if err != nil { + return fmt.Errorf("marshal DNS record set: %w", err) + } + cmd.Println(string(details)) + + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/record-set/list/list.go b/internal/cmd/dns/record-set/list/list.go index bd4662cb..fd23091f 100644 --- a/internal/cmd/dns/record-set/list/list.go +++ b/internal/cmd/dns/record-set/list/list.go @@ -32,51 +32,51 @@ type flagModel struct { OrderByName *string } -var Cmd = &cobra.Command{ - Use: "list", - Short: "List all DNS record sets", - Long: "List all DNS record sets", - Example: `$ stackit dns record-set list --project-id xxx --zone-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("get DNS record sets: %w", err) - } - recordSets := *resp.RrSets - if len(recordSets) == 0 { - cmd.Printf("No record-sets found for zone with ID %s\n", model.ZoneId) - return nil - } - - // Show output as table - table := tables.NewTable() - table.SetHeader("ID", "Name", "Type", "State") - for i := range recordSets { - rs := recordSets[i] - table.AddRow(*rs.Id, *rs.Name, *rs.Type, *rs.State) - } - table.Render(cmd) - - return nil - }, -} +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all DNS record sets", + Long: "List all DNS record sets", + Example: `$ stackit dns record-set list --project-id xxx --zone-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("get DNS record sets: %w", err) + } + recordSets := *resp.RrSets + if len(recordSets) == 0 { + cmd.Printf("No record-sets found for zone with ID %s\n", model.ZoneId) + return nil + } + + // Show output as table + table := tables.NewTable() + table.SetHeader("ID", "Name", "Type", "State") + for i := range recordSets { + rs := recordSets[i] + table.AddRow(*rs.Id, *rs.Name, *rs.Type, *rs.State) + } + table.Render(cmd) -func init() { - configureFlags(Cmd) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/record-set/record_set.go b/internal/cmd/dns/record-set/record_set.go index 8c946244..7019c6fe 100644 --- a/internal/cmd/dns/record-set/record_set.go +++ b/internal/cmd/dns/record-set/record_set.go @@ -12,18 +12,21 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "record-set", - Short: "Provides functionality for DNS record set", - Long: "Provides functionality for DNS record set", - Example: fmt.Sprintf("%s\n%s", list.Cmd.Example, create.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "record-set", + Short: "Provides functionality for DNS record set", + Long: "Provides functionality for DNS record set", + Example: fmt.Sprintf("%s\n%s", list.NewCmd().Example, create.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - // Add all direct child commands - Cmd.AddCommand(list.Cmd) - Cmd.AddCommand(create.Cmd) - Cmd.AddCommand(describe.Cmd) - Cmd.AddCommand(delete.Cmd) - Cmd.AddCommand(update.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(list.NewCmd()) + cmd.AddCommand(create.NewCmd()) + cmd.AddCommand(describe.NewCmd()) + cmd.AddCommand(delete.NewCmd()) + cmd.AddCommand(update.NewCmd()) } diff --git a/internal/cmd/dns/record-set/update/update.go b/internal/cmd/dns/record-set/update/update.go index c8c5880f..3874ef20 100644 --- a/internal/cmd/dns/record-set/update/update.go +++ b/internal/cmd/dns/record-set/update/update.go @@ -35,44 +35,44 @@ type flagModel struct { TTL *int64 } -var Cmd = &cobra.Command{ - Use: "update", - Short: "Updates a DNS record set", - Long: "Updates a DNS record set. Performs a partial update; fields not provided are kept unchanged", - Example: `$ stackit dns record-set update --project-id xxx --zone-id xxx --record-set-id xxx --name my-zone --type A --record 1.2.3.4 --record 5.6.7.8`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - _, err = req.Execute() - if err != nil { - return fmt.Errorf("update DNS record set: %w", err) - } - - // Wait for async operation - _, err = wait.UpdateRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for DNS record set update: %w", err) - } - - cmd.Println("Record set updated") - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Updates a DNS record set", + Long: "Updates a DNS record set. Performs a partial update; fields not provided are kept unchanged", + Example: `$ stackit dns record-set update --project-id xxx --zone-id xxx --record-set-id xxx --name my-zone --type A --record 1.2.3.4 --record 5.6.7.8`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + _, err = req.Execute() + if err != nil { + return fmt.Errorf("update DNS record set: %w", err) + } + + // Wait for async operation + _, err = wait.UpdateRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for DNS record set update: %w", err) + } + + cmd.Println("Record set updated") + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/zone/create/create.go b/internal/cmd/dns/zone/create/create.go index e38bcfb1..3fceed73 100644 --- a/internal/cmd/dns/zone/create/create.go +++ b/internal/cmd/dns/zone/create/create.go @@ -48,45 +48,45 @@ type flagModel struct { ContactEmail *string } -var Cmd = &cobra.Command{ - Use: "create", - Short: "Creates a DNS zone", - Long: "Creates a DNS zone", - Example: `$ stackit dns zone create --project-id xxx --name my-zone --dns-name my-zone.com`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("create DNS zone: %w", err) - } - - // Wait for async operation - zoneId := *resp.Zone.Id - _, err = wait.CreateZoneWaitHandler(ctx, apiClient, model.ProjectId, zoneId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for DNS zone creation: %w", err) - } - - cmd.Printf("Created zone with ID %s\n", zoneId) - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Creates a DNS zone", + Long: "Creates a DNS zone", + Example: `$ stackit dns zone create --project-id xxx --name my-zone --dns-name my-zone.com`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("create DNS zone: %w", err) + } + + // Wait for async operation + zoneId := *resp.Zone.Id + _, err = wait.CreateZoneWaitHandler(ctx, apiClient, model.ProjectId, zoneId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for DNS zone creation: %w", err) + } + + cmd.Printf("Created zone with ID %s\n", zoneId) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/zone/delete/delete.go b/internal/cmd/dns/zone/delete/delete.go index b558e773..8a562946 100644 --- a/internal/cmd/dns/zone/delete/delete.go +++ b/internal/cmd/dns/zone/delete/delete.go @@ -25,47 +25,47 @@ type flagModel struct { ZoneId string } -var Cmd = &cobra.Command{ - Use: "delete", - Short: "Delete a DNS zone", - Long: "Delete a DNS zone", - Example: `$ stackit dns zone delete --project-id xxx --zone-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - if err != nil { - return err - } - _, err = req.Execute() - if err != nil { - return fmt.Errorf("delete DNS zone: %w", err) - } - - // Wait for async operation - _, err = wait.DeleteZoneWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for DNS zone deletion: %w", err) - } - - cmd.Println("Zone deleted") - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a DNS zone", + Long: "Delete a DNS zone", + Example: `$ stackit dns zone delete --project-id xxx --zone-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + if err != nil { + return err + } + _, err = req.Execute() + if err != nil { + return fmt.Errorf("delete DNS zone: %w", err) + } + + // Wait for async operation + _, err = wait.DeleteZoneWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for DNS zone deletion: %w", err) + } + + cmd.Println("Zone deleted") + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/zone/describe/describe.go b/internal/cmd/dns/zone/describe/describe.go index f34b0de1..f71c7cb6 100644 --- a/internal/cmd/dns/zone/describe/describe.go +++ b/internal/cmd/dns/zone/describe/describe.go @@ -25,44 +25,44 @@ type flagModel struct { ZoneId string } -var Cmd = &cobra.Command{ - Use: "describe", - Short: "Get details of a DNS zone", - Long: "Get details of a DNS zone", - Example: `$ stackit dns zone describe --project-id xxx --zone-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "describe", + Short: "Get details of a DNS zone", + Long: "Get details of a DNS zone", + Example: `$ stackit dns zone describe --project-id xxx --zone-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("read DNS zone: %w", err) - } - zone := *resp.Zone + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("read DNS zone: %w", err) + } + zone := *resp.Zone - // Show details - details, err := json.MarshalIndent(zone, "", " ") - if err != nil { - return fmt.Errorf("marshal DNS zone: %w", err) - } - cmd.Println(string(details)) + // Show details + details, err := json.MarshalIndent(zone, "", " ") + if err != nil { + return fmt.Errorf("marshal DNS zone: %w", err) + } + cmd.Println(string(details)) - return nil - }, -} - -func init() { - configureFlags(Cmd) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/zone/list/list.go b/internal/cmd/dns/zone/list/list.go index 08d10a3d..adafbde7 100644 --- a/internal/cmd/dns/zone/list/list.go +++ b/internal/cmd/dns/zone/list/list.go @@ -30,51 +30,51 @@ type flagModel struct { OrderByName *string } -var Cmd = &cobra.Command{ - Use: "list", - Short: "List all DNS zones", - Long: "List all DNS zones", - Example: `$ stackit dns zone list --project-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all DNS zones", + Long: "List all DNS zones", + Example: `$ stackit dns zone list --project-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("get DNS zones: %w", err) + } + zones := *resp.Zones + if len(zones) == 0 { + cmd.Printf("No zones found for project with ID %s\n", model.ProjectId) + return nil + } + + // Show output as table + table := tables.NewTable() + table.SetHeader("ID", "NAME", "DNS_NAME", "STATE") + for i := range zones { + z := zones[i] + table.AddRow(*z.Id, *z.Name, *z.DnsName, *z.State) + } + table.Render(cmd) - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("get DNS zones: %w", err) - } - zones := *resp.Zones - if len(zones) == 0 { - cmd.Printf("No zones found for project with ID %s\n", model.ProjectId) return nil - } - - // Show output as table - table := tables.NewTable() - table.SetHeader("ID", "NAME", "DNS_NAME", "STATE") - for i := range zones { - z := zones[i] - table.AddRow(*z.Id, *z.Name, *z.DnsName, *z.State) - } - table.Render(cmd) - - return nil - }, -} - -func init() { - configureFlags(Cmd) + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/zone/update/update.go b/internal/cmd/dns/zone/update/update.go index 4a915330..e600f831 100644 --- a/internal/cmd/dns/zone/update/update.go +++ b/internal/cmd/dns/zone/update/update.go @@ -45,47 +45,47 @@ const ( contactEmailFlag = "contact-email" ) -var Cmd = &cobra.Command{ - Use: "update", - Short: "Updates a DNS zone", - Long: "Updates a DNS zone. Performs a partial update; fields not provided are kept unchanged", - Example: `$ stackit dns zone update --project-id xxx --zone-id xxx --name my-zone --dns-name my-zone.com`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - if err != nil { - return err - } - _, err = req.Execute() - if err != nil { - return fmt.Errorf("update DNS zone: %w", err) - } - - // Wait for async operation - _, err = wait.UpdateZoneWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for DNS zone update: %w", err) - } - - cmd.Println("Zone updated") - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Updates a DNS zone", + Long: "Updates a DNS zone. Performs a partial update; fields not provided are kept unchanged", + Example: `$ stackit dns zone update --project-id xxx --zone-id xxx --name my-zone --dns-name my-zone.com`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + if err != nil { + return err + } + _, err = req.Execute() + if err != nil { + return fmt.Errorf("update DNS zone: %w", err) + } + + // Wait for async operation + _, err = wait.UpdateZoneWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for DNS zone update: %w", err) + } + + cmd.Println("Zone updated") + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/dns/zone/zone.go b/internal/cmd/dns/zone/zone.go index 385c97b5..3b1471a7 100644 --- a/internal/cmd/dns/zone/zone.go +++ b/internal/cmd/dns/zone/zone.go @@ -12,18 +12,21 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "zone", - Short: "Provides functionality for DNS zone", - Long: "Provides functionality for DNS zone", - Example: fmt.Sprintf("%s\n%s", list.Cmd.Example, create.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "zone", + Short: "Provides functionality for DNS zone", + Long: "Provides functionality for DNS zone", + Example: fmt.Sprintf("%s\n%s", list.NewCmd().Example, create.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - // Add all direct child commands - Cmd.AddCommand(list.Cmd) - Cmd.AddCommand(create.Cmd) - Cmd.AddCommand(describe.Cmd) - Cmd.AddCommand(update.Cmd) - Cmd.AddCommand(delete.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(list.NewCmd()) + cmd.AddCommand(create.NewCmd()) + cmd.AddCommand(describe.NewCmd()) + cmd.AddCommand(update.NewCmd()) + cmd.AddCommand(delete.NewCmd()) } diff --git a/internal/cmd/postgresql/credential/create/create.go b/internal/cmd/postgresql/credential/create/create.go index 56b32e90..156a7712 100644 --- a/internal/cmd/postgresql/credential/create/create.go +++ b/internal/cmd/postgresql/credential/create/create.go @@ -24,38 +24,38 @@ type flagModel struct { InstanceId string } -var Cmd = &cobra.Command{ - Use: "create", - Short: "Create credentials for a PostgreSQL instance", - Long: "Create credentials for a PostgreSQL instance", - Example: `$ stackit postgresql credential create --project-id xxx --instance-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Create credentials for a PostgreSQL instance", + Long: "Create credentials for a PostgreSQL instance", + Example: `$ stackit postgresql credential create --project-id xxx --instance-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("create PostgreSQL credentials: %w", err) - } + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("create PostgreSQL credentials: %w", err) + } - cmd.Printf("Created credentials with ID %s\n", *resp.Id) - return nil - }, -} - -func init() { - configureFlags(Cmd) + cmd.Printf("Created credentials with ID %s\n", *resp.Id) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/credential/credential.go b/internal/cmd/postgresql/credential/credential.go index 70d02346..53d29e80 100644 --- a/internal/cmd/postgresql/credential/credential.go +++ b/internal/cmd/postgresql/credential/credential.go @@ -11,16 +11,20 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "credential", - Short: "Provides functionality for PostgreSQL credentials", - Long: "Provides functionality for PostgreSQL credentials", - Example: fmt.Sprintf("%s\n%s", create.Cmd.Example, describe.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "credential", + Short: "Provides functionality for PostgreSQL credentials", + Long: "Provides functionality for PostgreSQL credentials", + Example: fmt.Sprintf("%s\n%s", create.NewCmd().Example, describe.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(create.Cmd) - Cmd.AddCommand(delete.Cmd) - Cmd.AddCommand(describe.Cmd) - Cmd.AddCommand(list.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(create.NewCmd()) + cmd.AddCommand(delete.NewCmd()) + cmd.AddCommand(describe.NewCmd()) + cmd.AddCommand(list.NewCmd()) } diff --git a/internal/cmd/postgresql/credential/delete/delete.go b/internal/cmd/postgresql/credential/delete/delete.go index 3b02dcf2..6e18a13d 100644 --- a/internal/cmd/postgresql/credential/delete/delete.go +++ b/internal/cmd/postgresql/credential/delete/delete.go @@ -26,38 +26,38 @@ type flagModel struct { CredentialsId string } -var Cmd = &cobra.Command{ - Use: "delete", - Short: "Delete a PostgreSQL instance credential", - Long: "Delete a PostgreSQL instance credential", - Example: `$ stackit postgresql credential delete --project-id xxx --instance-id xxx --credentials-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a PostgreSQL instance credential", + Long: "Delete a PostgreSQL instance credential", + Example: `$ stackit postgresql credential delete --project-id xxx --instance-id xxx --credentials-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - err = req.Execute() - if err != nil { - return fmt.Errorf("delete PostgreSQL credentials: %w", err) - } + // Call API + req := buildRequest(ctx, model, apiClient) + err = req.Execute() + if err != nil { + return fmt.Errorf("delete PostgreSQL credentials: %w", err) + } - cmd.Printf("Deleted credentials with ID %s\n", model.CredentialsId) - return nil - }, -} - -func init() { - configureFlags(Cmd) + cmd.Printf("Deleted credentials with ID %s\n", model.CredentialsId) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/credential/describe/describe.go b/internal/cmd/postgresql/credential/describe/describe.go index 60586adf..537cfb03 100644 --- a/internal/cmd/postgresql/credential/describe/describe.go +++ b/internal/cmd/postgresql/credential/describe/describe.go @@ -27,44 +27,44 @@ type flagModel struct { CredentialsId string } -var Cmd = &cobra.Command{ - Use: "describe", - Short: "Get details of a PostgreSQL instance credential", - Long: "Get details of a PostgreSQL instance credential", - Example: `$ stackit postgresql credential describe --project-id xxx --instance-id xxx --credentials-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("describe PostgreSQL credentials: %w", err) - } - - // Show details - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgreSQL credentials: %w", err) - } - cmd.Println(string(details)) - - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "describe", + Short: "Get details of a PostgreSQL instance credential", + Long: "Get details of a PostgreSQL instance credential", + Example: `$ stackit postgresql credential describe --project-id xxx --instance-id xxx --credentials-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("describe PostgreSQL credentials: %w", err) + } + + // Show details + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgreSQL credentials: %w", err) + } + cmd.Println(string(details)) + + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/credential/list/list.go b/internal/cmd/postgresql/credential/list/list.go index 7c0db5ca..830719c3 100644 --- a/internal/cmd/postgresql/credential/list/list.go +++ b/internal/cmd/postgresql/credential/list/list.go @@ -25,51 +25,51 @@ type flagModel struct { InstanceId string } -var Cmd = &cobra.Command{ - Use: "list", - Short: "List all credentials IDs for a PostgreSQL instance", - Long: "List all credentials IDs for a PostgreSQL instance", - Example: `$ stackit postgresql credential list --project-id xxx --instance-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all credentials IDs for a PostgreSQL instance", + Long: "List all credentials IDs for a PostgreSQL instance", + Example: `$ stackit postgresql credential list --project-id xxx --instance-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("list PostgreSQL credentials: %w", err) + } + credentials := *resp.CredentialsList + if len(credentials) == 0 { + cmd.Printf("No credentials found for instance with ID %s\n", model.InstanceId) + return nil + } + + // Show output as table + table := tables.NewTable() + table.SetHeader("ID") + for i := range credentials { + c := credentials[i] + table.AddRow(*c.Id) + } + table.Render(cmd) - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("list PostgreSQL credentials: %w", err) - } - credentials := *resp.CredentialsList - if len(credentials) == 0 { - cmd.Printf("No credentials found for instance with ID %s\n", model.InstanceId) return nil - } - - // Show output as table - table := tables.NewTable() - table.SetHeader("ID") - for i := range credentials { - c := credentials[i] - table.AddRow(*c.Id) - } - table.Render(cmd) - - return nil - }, -} - -func init() { - configureFlags(Cmd) + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/instance/create/create.go b/internal/cmd/postgresql/instance/create/create.go index 6583b905..47d5131a 100644 --- a/internal/cmd/postgresql/instance/create/create.go +++ b/internal/cmd/postgresql/instance/create/create.go @@ -50,48 +50,48 @@ type flagModel struct { PlanId *string } -var Cmd = &cobra.Command{ - Use: "create", - Short: "Creates a PostgreSQL instance", - Long: "Creates a PostgreSQL instance", - Example: `$ stackit postgresql instance create --project-id xxx --name my-instance --plan-name plan-name --version version`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req, err := buildRequest(ctx, model, apiClient) - if err != nil { - return fmt.Errorf("build PostgreSQL instance creation request: %w", err) - } - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("create PostgreSQL instance: %w", err) - } - - // Wait for async operation - instanceId := *resp.InstanceId - _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for PostgreSQL instance creation: %w", err) - } - - cmd.Printf("Created instance with ID %s\n", instanceId) - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Creates a PostgreSQL instance", + Long: "Creates a PostgreSQL instance", + Example: `$ stackit postgresql instance create --project-id xxx --name my-instance --plan-name plan-name --version version`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req, err := buildRequest(ctx, model, apiClient) + if err != nil { + return fmt.Errorf("build PostgreSQL instance creation request: %w", err) + } + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("create PostgreSQL instance: %w", err) + } + + // Wait for async operation + instanceId := *resp.InstanceId + _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for PostgreSQL instance creation: %w", err) + } + + cmd.Printf("Created instance with ID %s\n", instanceId) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/instance/delete/delete.go b/internal/cmd/postgresql/instance/delete/delete.go index 5c0a6ad0..5341089e 100644 --- a/internal/cmd/postgresql/instance/delete/delete.go +++ b/internal/cmd/postgresql/instance/delete/delete.go @@ -25,43 +25,43 @@ type flagModel struct { InstanceId string } -var Cmd = &cobra.Command{ - Use: "delete", - Short: "Delete a PostgreSQL instance", - Long: "Delete a PostgreSQL instance", - Example: `$ stackit postgresql instance delete --project-id xxx --instance-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a PostgreSQL instance", + Long: "Delete a PostgreSQL instance", + Example: `$ stackit postgresql instance delete --project-id xxx --instance-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - err = req.Execute() - if err != nil { - return fmt.Errorf("delete PostgreSQL instance: %w", err) - } + // Call API + req := buildRequest(ctx, model, apiClient) + err = req.Execute() + if err != nil { + return fmt.Errorf("delete PostgreSQL instance: %w", err) + } - // Wait for async operation - _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for PostgreSQL instance deletion: %w", err) - } + // Wait for async operation + _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for PostgreSQL instance deletion: %w", err) + } - cmd.Printf("Deleted instance with ID %s\n", model.InstanceId) - return nil - }, -} - -func init() { - configureFlags(Cmd) + cmd.Printf("Deleted instance with ID %s\n", model.InstanceId) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/instance/describe/describe.go b/internal/cmd/postgresql/instance/describe/describe.go index 58a211af..769d2a9d 100644 --- a/internal/cmd/postgresql/instance/describe/describe.go +++ b/internal/cmd/postgresql/instance/describe/describe.go @@ -25,43 +25,43 @@ type flagModel struct { InstanceId string } -var Cmd = &cobra.Command{ - Use: "describe", - Short: "Get details of a PostgreSQL instance", - Long: "Get details of a PostgreSQL instance", - Example: `$ stackit postgresql instance describe --project-id xxx --instance-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "describe", + Short: "Get details of a PostgreSQL instance", + Long: "Get details of a PostgreSQL instance", + Example: `$ stackit postgresql instance describe --project-id xxx --instance-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("read PostgreSQL instance: %w", err) - } + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("read PostgreSQL instance: %w", err) + } - // Show details - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgreSQL instance: %w", err) - } - cmd.Println(string(details)) + // Show details + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgreSQL instance: %w", err) + } + cmd.Println(string(details)) - return nil - }, -} - -func init() { - configureFlags(Cmd) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/instance/instance.go b/internal/cmd/postgresql/instance/instance.go index 6bb9f5f3..05e119c1 100644 --- a/internal/cmd/postgresql/instance/instance.go +++ b/internal/cmd/postgresql/instance/instance.go @@ -12,17 +12,21 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "instance", - Short: "Provides functionality for PostgreSQL instance", - Long: "Provides functionality for PostgreSQL instance", - Example: fmt.Sprintf("%s\n%s", create.Cmd.Example, list.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "instance", + Short: "Provides functionality for PostgreSQL instance", + Long: "Provides functionality for PostgreSQL instance", + Example: fmt.Sprintf("%s\n%s", create.NewCmd().Example, list.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(create.Cmd) - Cmd.AddCommand(delete.Cmd) - Cmd.AddCommand(describe.Cmd) - Cmd.AddCommand(list.Cmd) - Cmd.AddCommand(update.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(create.NewCmd()) + cmd.AddCommand(delete.NewCmd()) + cmd.AddCommand(describe.NewCmd()) + cmd.AddCommand(list.NewCmd()) + cmd.AddCommand(update.NewCmd()) } diff --git a/internal/cmd/postgresql/instance/list/list.go b/internal/cmd/postgresql/instance/list/list.go index 4482e139..91d3a6a6 100644 --- a/internal/cmd/postgresql/instance/list/list.go +++ b/internal/cmd/postgresql/instance/list/list.go @@ -17,47 +17,50 @@ type flagModel struct { ProjectId string } -var Cmd = &cobra.Command{ - Use: "list", - Short: "List all PostgreSQL instances", - Long: "List all PostgreSQL instances", - Example: `$ stackit postgresql instance list --project-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all PostgreSQL instances", + Long: "List all PostgreSQL instances", + Example: `$ stackit postgresql instance list --project-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("get PostgreSQL instances: %w", err) - } - instances := *resp.Instances - if len(instances) == 0 { - cmd.Printf("No instances found for product with ID %s\n", model.ProjectId) - return nil - } + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("get PostgreSQL instances: %w", err) + } + instances := *resp.Instances + if len(instances) == 0 { + cmd.Printf("No instances found for product with ID %s\n", model.ProjectId) + return nil + } - // Show output as table - table := tables.NewTable() - table.SetHeader("ID", "NAME", "LAST_OPERATION.TYPE", "LAST_OPERATION.STATE") - for i := range instances { - instance := instances[i] - table.AddRow(*instance.InstanceId, *instance.Name, *instance.LastOperation.Type, *instance.LastOperation.State) - } - table.Render(cmd) + // Show output as table + table := tables.NewTable() + table.SetHeader("ID", "NAME", "LAST_OPERATION.TYPE", "LAST_OPERATION.STATE") + for i := range instances { + instance := instances[i] + table.AddRow(*instance.InstanceId, *instance.Name, *instance.LastOperation.Type, *instance.LastOperation.State) + } + table.Render(cmd) - return nil - }, + return nil + }, + } + return cmd } func parseFlags(_ *cobra.Command) (*flagModel, error) { diff --git a/internal/cmd/postgresql/instance/update/update.go b/internal/cmd/postgresql/instance/update/update.go index 779c759d..3ecab8de 100644 --- a/internal/cmd/postgresql/instance/update/update.go +++ b/internal/cmd/postgresql/instance/update/update.go @@ -51,48 +51,48 @@ type flagModel struct { PlanId *string } -var Cmd = &cobra.Command{ - Use: "update", - Short: "Updates a PostgreSQL instance", - Long: "Updates a PostgreSQL instance", - Example: `$ stackit postgresql instance update --project-id xxx --instance-id xxx --plan-id xxx --acl xx.xx.xx.xx/xx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Call API - req, err := buildRequest(ctx, model, apiClient) - if err != nil { - return fmt.Errorf("build PostgreSQL instance update request: %w", err) - } - err = req.Execute() - if err != nil { - return fmt.Errorf("update PostgreSQL instance: %w", err) - } - - // Wait for async operation - instanceId := model.InstanceId - _, err = wait.UpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for PostgreSQL instance update: %w", err) - } - - cmd.Printf("Updated instance with ID %s\n", instanceId) - return nil - }, -} - -func init() { - configureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Updates a PostgreSQL instance", + Long: "Updates a PostgreSQL instance", + Example: `$ stackit postgresql instance update --project-id xxx --instance-id xxx --plan-id xxx --acl xx.xx.xx.xx/xx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Call API + req, err := buildRequest(ctx, model, apiClient) + if err != nil { + return fmt.Errorf("build PostgreSQL instance update request: %w", err) + } + err = req.Execute() + if err != nil { + return fmt.Errorf("update PostgreSQL instance: %w", err) + } + + // Wait for async operation + instanceId := model.InstanceId + _, err = wait.UpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for PostgreSQL instance update: %w", err) + } + + cmd.Printf("Updated instance with ID %s\n", instanceId) + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/postgresql/offerings/list/list.go b/internal/cmd/postgresql/offerings/list/list.go index a7442155..b6bfcb70 100644 --- a/internal/cmd/postgresql/offerings/list/list.go +++ b/internal/cmd/postgresql/offerings/list/list.go @@ -17,48 +17,51 @@ type flagModel struct { ProjectId string } -var Cmd = &cobra.Command{ - Use: "list", - Short: "List all PostgreSQL service offerings", - Long: "List all PostgreSQL service offerings", - Example: `$ stackit postgresql offerings list --project-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all PostgreSQL service offerings", + Long: "List all PostgreSQL service offerings", + Example: `$ stackit postgresql offerings list --project-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("get PostgreSQL service offerings: %w", err) - } - offerings := *resp.Offerings + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("get PostgreSQL service offerings: %w", err) + } + offerings := *resp.Offerings - // Show output as table - table := tables.NewTable() - table.SetHeader("NAME", "PLAN.ID", "PLAN.NAME", "PLAN.DESCRIPTION") - for i := range offerings { - o := offerings[i] - for j := range *o.Plans { - p := (*o.Plans)[j] - table.AddRow(*o.Name, *p.Id, *p.Name, *p.Description) + // Show output as table + table := tables.NewTable() + table.SetHeader("NAME", "PLAN.ID", "PLAN.NAME", "PLAN.DESCRIPTION") + for i := range offerings { + o := offerings[i] + for j := range *o.Plans { + p := (*o.Plans)[j] + table.AddRow(*o.Name, *p.Id, *p.Name, *p.Description) + } + table.AddSeparator() } - table.AddSeparator() - } - table.EnableAutoMergeOnColumns(1) - table.Render(cmd) + table.EnableAutoMergeOnColumns(1) + table.Render(cmd) - return nil - }, + return nil + }, + } + return cmd } func parseFlags(_ *cobra.Command) (*flagModel, error) { diff --git a/internal/cmd/postgresql/offerings/offerings.go b/internal/cmd/postgresql/offerings/offerings.go index 23827e6b..9178e9e4 100644 --- a/internal/cmd/postgresql/offerings/offerings.go +++ b/internal/cmd/postgresql/offerings/offerings.go @@ -6,14 +6,17 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "offerings", - Short: "Provides information regarding the PostgreSQL service offerings", - Long: "Provides information regarding the PostgreSQL service offerings", - Example: list.Cmd.Example, +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "offerings", + Short: "Provides information regarding the PostgreSQL service offerings", + Long: "Provides information regarding the PostgreSQL service offerings", + Example: list.NewCmd().Example, + } + addChilds(cmd) + return cmd } -func init() { - // Add all direct child commands - Cmd.AddCommand(list.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(list.NewCmd()) } diff --git a/internal/cmd/postgresql/postgresql.go b/internal/cmd/postgresql/postgresql.go index 06e46556..4f68c7d0 100644 --- a/internal/cmd/postgresql/postgresql.go +++ b/internal/cmd/postgresql/postgresql.go @@ -10,15 +10,19 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "postgresql", - Short: "Provides functionality for PostgreSQL", - Long: "Provides functionality for PostgreSQL", - Example: fmt.Sprintf("%s\n%s", instance.Cmd.Example, credential.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "postgresql", + Short: "Provides functionality for PostgreSQL", + Long: "Provides functionality for PostgreSQL", + Example: fmt.Sprintf("%s\n%s", instance.NewCmd().Example, credential.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(instance.Cmd) - Cmd.AddCommand(offerings.Cmd) - Cmd.AddCommand(credential.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(instance.NewCmd()) + cmd.AddCommand(offerings.NewCmd()) + cmd.AddCommand(credential.NewCmd()) } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 33a8fb51..c866f41a 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -19,38 +19,37 @@ const ( projectIdFlag = "project-id" ) -var RootCmd = &cobra.Command{ - Use: "stackit", - Short: "The root command of the STACKIT CLI", - Long: "The root command of the STACKIT CLI", - SilenceUsage: true, - DisableAutoGenTag: true, +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "stackit", + Short: "The root command of the STACKIT CLI", + Long: "The root command of the STACKIT CLI", + SilenceUsage: true, + DisableAutoGenTag: true, + } + configureFlags(cmd) + addChilds(cmd) + return cmd } func Execute() { - err := RootCmd.Execute() + err := NewCmd().Execute() if err != nil { os.Exit(1) } } -func init() { - // Set up configuration files - configPkg.InitConfig() - - // Add all direct child commands - RootCmd.AddCommand(auth.Cmd) - RootCmd.AddCommand(config.Cmd) - RootCmd.AddCommand(dns.Cmd) - RootCmd.AddCommand(postgresql.Cmd) - RootCmd.AddCommand(ske.Cmd) - - configureFlags(RootCmd) -} - func configureFlags(cmd *cobra.Command) { cmd.PersistentFlags().Var(flags.UUIDFlag(), projectIdFlag, "Project ID") err := viper.BindPFlag(configPkg.ProjectIdKey, cmd.PersistentFlags().Lookup(projectIdFlag)) cobra.CheckErr(err) } + +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(auth.NewCmd()) + cmd.AddCommand(config.NewCmd()) + cmd.AddCommand(dns.NewCmd()) + cmd.AddCommand(postgresql.NewCmd()) + cmd.AddCommand(ske.NewCmd()) +} diff --git a/internal/cmd/ske/cluster/cluster.go b/internal/cmd/ske/cluster/cluster.go index 69ff357c..35b748aa 100644 --- a/internal/cmd/ske/cluster/cluster.go +++ b/internal/cmd/ske/cluster/cluster.go @@ -12,17 +12,21 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "cluster", - Short: "Provides functionality for SKE cluster", - Long: "Provides functionality for SKE cluster", - Example: fmt.Sprintf("%s\n%s", create.Cmd.Example, list.Cmd.Example), +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "cluster", + Short: "Provides functionality for SKE cluster", + Long: "Provides functionality for SKE cluster", + Example: fmt.Sprintf("%s\n%s", create.NewCmd().Example, list.NewCmd().Example), + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(create.Cmd) - Cmd.AddCommand(delete.Cmd) - Cmd.AddCommand(describe.Cmd) - Cmd.AddCommand(list.Cmd) - Cmd.AddCommand(update.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(create.NewCmd()) + cmd.AddCommand(delete.NewCmd()) + cmd.AddCommand(describe.NewCmd()) + cmd.AddCommand(list.NewCmd()) + cmd.AddCommand(update.NewCmd()) } diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index acd88684..bfe6e9a1 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -30,57 +30,57 @@ type FlagModel struct { Payload ske.CreateOrUpdateClusterPayload } -var Cmd = &cobra.Command{ - Use: "create", - Short: "Creates an SKE cluster", - Long: "Creates an SKE cluster", - Example: `$ stackit ske cluster create --project-id xxx --payload @./payload.json`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := ParseFlags(cmd, os.ReadFile) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } - - // Check if cluster exists - exists, err := skeUtils.ClusterExists(ctx, apiClient, model.ProjectId, model.Name) - if err != nil { - return err - } - if exists { - return fmt.Errorf("cluster with name %s already exists", model.Name) - } - - // Call API - req, err := BuildRequest(ctx, model, apiClient) - if err != nil { - return fmt.Errorf("build SKE cluster creation request: %w", err) - } - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("create SKE cluster: %w", err) - } - - // Wait for async operation - name := *resp.Name - _, err = wait.CreateOrUpdateClusterWaitHandler(ctx, apiClient, model.ProjectId, name).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for SKE cluster creation: %w", err) - } - - cmd.Printf("Created cluster with name %s\n", name) - return nil - }, -} - -func init() { - ConfigureFlags(Cmd) +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Creates an SKE cluster", + Long: "Creates an SKE cluster", + Example: `$ stackit ske cluster create --project-id xxx --payload @./payload.json`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := ParseFlags(cmd, os.ReadFile) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } + + // Check if cluster exists + exists, err := skeUtils.ClusterExists(ctx, apiClient, model.ProjectId, model.Name) + if err != nil { + return err + } + if exists { + return fmt.Errorf("cluster with name %s already exists", model.Name) + } + + // Call API + req, err := BuildRequest(ctx, model, apiClient) + if err != nil { + return fmt.Errorf("build SKE cluster creation request: %w", err) + } + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("create SKE cluster: %w", err) + } + + // Wait for async operation + name := *resp.Name + _, err = wait.CreateOrUpdateClusterWaitHandler(ctx, apiClient, model.ProjectId, name).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for SKE cluster creation: %w", err) + } + + cmd.Printf("Created cluster with name %s\n", name) + return nil + }, + } + ConfigureFlags(cmd) + return cmd } func ConfigureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/ske/cluster/delete/delete.go b/internal/cmd/ske/cluster/delete/delete.go index 333617c7..a9e7a29c 100644 --- a/internal/cmd/ske/cluster/delete/delete.go +++ b/internal/cmd/ske/cluster/delete/delete.go @@ -24,43 +24,43 @@ type flagModel struct { ClusterName string } -var Cmd = &cobra.Command{ - Use: "delete", - Short: "Delete a SKE cluster", - Long: "Delete a SKE cluster", - Example: `$ stackit ske cluster delete --project-id xxx --name xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete a SKE cluster", + Long: "Delete a SKE cluster", + Example: `$ stackit ske cluster delete --project-id xxx --name xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - _, err = req.Execute() - if err != nil { - return fmt.Errorf("delete SKE cluster: %w", err) - } + // Call API + req := buildRequest(ctx, model, apiClient) + _, err = req.Execute() + if err != nil { + return fmt.Errorf("delete SKE cluster: %w", err) + } - // Wait for async operation - _, err = wait.DeleteClusterWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for SKE cluster deletion: %w", err) - } + // Wait for async operation + _, err = wait.DeleteClusterWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for SKE cluster deletion: %w", err) + } - cmd.Println("Cluster deleted") - return nil - }, -} - -func init() { - configureFlags(Cmd) + cmd.Println("Cluster deleted") + return nil + }, + } + configureFlags(cmd) + return cmd } func configureFlags(cmd *cobra.Command) { diff --git a/internal/cmd/ske/cluster/describe/describe.go b/internal/cmd/ske/cluster/describe/describe.go index b720fee3..3e388083 100644 --- a/internal/cmd/ske/cluster/describe/describe.go +++ b/internal/cmd/ske/cluster/describe/describe.go @@ -24,45 +24,44 @@ type flagModel struct { ClusterName string } -var Cmd = &cobra.Command{ - Use: "describe", - Short: "Get details of a SKE cluster", - Long: "Get details of a SKE cluster", - Example: `$ stackit ske cluster describe --project-id xxx --name xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags(cmd) - if err != nil { - return err - } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "describe", + Short: "Get details of a SKE cluster", + Long: "Get details of a SKE cluster", + Example: `$ stackit ske cluster describe --project-id xxx --name xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags(cmd) + if err != nil { + return err + } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("read SKE cluster: %w", err) - } + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("read SKE cluster: %w", err) + } - // Show details - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal SKE cluster: %w", err) - } - cmd.Println(string(details)) + // Show details + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE cluster: %w", err) + } + cmd.Println(string(details)) - return nil - }, -} - -func init() { - configureFlags(Cmd) + return nil + }, + } + configureFlags(cmd) + return cmd } - func configureFlags(cmd *cobra.Command) { cmd.Flags().StringP(clusterNameFlag, "n", "", "Cluster name") diff --git a/internal/cmd/ske/cluster/list/list.go b/internal/cmd/ske/cluster/list/list.go index 891c69ef..5a323121 100644 --- a/internal/cmd/ske/cluster/list/list.go +++ b/internal/cmd/ske/cluster/list/list.go @@ -21,47 +21,50 @@ type flagModel struct { ProjectId string } -var Cmd = &cobra.Command{ - Use: "list", - Short: "List all SKE clusters", - Long: "List all SKE clusters", - Example: `$ stackit ske cluster list --project-id xxx`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseFlags() - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all SKE clusters", + Long: "List all SKE clusters", + Example: `$ stackit ske cluster list --project-id xxx`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseFlags() + if err != nil { + return err + } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("get SKE clusters: %w", err) - } - clusters := *resp.Items - if len(clusters) == 0 { - cmd.Printf("No clusters found for project with ID %s\n", model.ProjectId) - return nil - } + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("get SKE clusters: %w", err) + } + clusters := *resp.Items + if len(clusters) == 0 { + cmd.Printf("No clusters found for project with ID %s\n", model.ProjectId) + return nil + } - // Show output as table - table := tables.NewTable() - table.SetHeader("NAME", "STATE") - for i := range clusters { - c := clusters[i] - table.AddRow(*c.Name, *c.Status.Aggregated) - } - table.Render(cmd) + // Show output as table + table := tables.NewTable() + table.SetHeader("NAME", "STATE") + for i := range clusters { + c := clusters[i] + table.AddRow(*c.Name, *c.Status.Aggregated) + } + table.Render(cmd) - return nil - }, + return nil + }, + } + return cmd } func parseFlags() (*flagModel, error) { diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index e862e002..115bb522 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -13,55 +13,55 @@ import ( "github.com/stackitcloud/stackit-sdk-go/services/ske/wait" ) -var Cmd = &cobra.Command{ - Use: "update", - Short: "Updates an SKE cluster", - Long: "Updates an SKE cluster", - Example: `$ stackit ske cluster update --project-id xxx --payload @./payload.json`, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := create.ParseFlags(cmd, os.ReadFile) - if err != nil { - return err - } +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Updates an SKE cluster", + Long: "Updates an SKE cluster", + Example: `$ stackit ske cluster update --project-id xxx --payload @./payload.json`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := create.ParseFlags(cmd, os.ReadFile) + if err != nil { + return err + } - // Configure API client - apiClient, err := client.ConfigureClient(cmd) - if err != nil { - return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") - } + // Configure API client + apiClient, err := client.ConfigureClient(cmd) + if err != nil { + return fmt.Errorf("authentication failed, please run \"stackit auth login\" or \"stackit auth activate-service-account\"") + } - // Check if cluster exists - exists, err := utils.ClusterExists(ctx, apiClient, model.ProjectId, model.Name) - if err != nil { - return err - } - if !exists { - return fmt.Errorf("cluster with name %s does not exist", model.Name) - } + // Check if cluster exists + exists, err := utils.ClusterExists(ctx, apiClient, model.ProjectId, model.Name) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("cluster with name %s does not exist", model.Name) + } - // Call API - req, err := create.BuildRequest(ctx, model, apiClient) - if err != nil { - return fmt.Errorf("build SKE cluster update request: %w", err) - } - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("update SKE cluster: %w", err) - } + // Call API + req, err := create.BuildRequest(ctx, model, apiClient) + if err != nil { + return fmt.Errorf("build SKE cluster update request: %w", err) + } + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("update SKE cluster: %w", err) + } - // Wait for async operation - name := *resp.Name - _, err = wait.CreateOrUpdateClusterWaitHandler(ctx, apiClient, model.ProjectId, name).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for SKE cluster update: %w", err) - } + // Wait for async operation + name := *resp.Name + _, err = wait.CreateOrUpdateClusterWaitHandler(ctx, apiClient, model.ProjectId, name).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for SKE cluster update: %w", err) + } - cmd.Printf("Updated cluster with name %s\n", name) - return nil - }, -} - -func init() { - create.ConfigureFlags(Cmd) + cmd.Printf("Updated cluster with name %s\n", name) + return nil + }, + } + create.ConfigureFlags(cmd) + return cmd } diff --git a/internal/cmd/ske/ske.go b/internal/cmd/ske/ske.go index daaeb1bf..ff3df54e 100644 --- a/internal/cmd/ske/ske.go +++ b/internal/cmd/ske/ske.go @@ -6,13 +6,17 @@ import ( "github.com/spf13/cobra" ) -var Cmd = &cobra.Command{ - Use: "ske", - Short: "Provides functionality for SKE", - Long: "Provides functionality for STACKIT Kubernetes engine (SKE)", - Example: cluster.Cmd.Example, +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "ske", + Short: "Provides functionality for SKE", + Long: "Provides functionality for STACKIT Kubernetes engine (SKE)", + Example: cluster.NewCmd().Example, + } + addChilds(cmd) + return cmd } -func init() { - Cmd.AddCommand(cluster.Cmd) +func addChilds(cmd *cobra.Command) { + cmd.AddCommand(cluster.NewCmd()) } diff --git a/main.go b/main.go index 630091cd..a02acad0 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,12 @@ package main import ( "github.com/stackitcloud/stackit-cli/internal/cmd" + "github.com/stackitcloud/stackit-cli/internal/pkg/config" ) func main() { + // Set up configuration files + config.InitConfig() + cmd.Execute() } diff --git a/scripts/generate.go b/scripts/generate.go index 7c1b28f0..c2c29276 100644 --- a/scripts/generate.go +++ b/scripts/generate.go @@ -37,7 +37,7 @@ func main() { linkHandler := func(filename string) string { return fmt.Sprintf("./%s", filename) } - err = doc.GenMarkdownTreeCustom(cmd.RootCmd, docsDir, filePrepender, linkHandler) + err = doc.GenMarkdownTreeCustom(cmd.NewCmd(), docsDir, filePrepender, linkHandler) if err != nil { log.Fatalf("Error generating documentation: %v", err) } From b6049c63dbb0beff523e05049dd4cff9cdeb6c96 Mon Sep 17 00:00:00 2001 From: Henrique Santos Date: Tue, 21 Nov 2023 12:41:45 +0000 Subject: [PATCH 2/4] Enable gochecknoinits --- golang-ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/golang-ci.yaml b/golang-ci.yaml index 1770a552..7eceed18 100644 --- a/golang-ci.yaml +++ b/golang-ci.yaml @@ -76,7 +76,7 @@ linters: # additional linters - errorlint - exportloopref - # - gochecknoinits + - gochecknoinits - gocritic - gofmt - goimports From b3c6d3016fff11a865b8103107518a233a3a669d Mon Sep 17 00:00:00 2001 From: Henrique Santos Date: Tue, 21 Nov 2023 15:07:32 +0000 Subject: [PATCH 3/4] Rename root constructor --- internal/cmd/root.go | 4 ++-- scripts/generate.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index c866f41a..240f7e4c 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -19,7 +19,7 @@ const ( projectIdFlag = "project-id" ) -func NewCmd() *cobra.Command { +func NewRootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "stackit", Short: "The root command of the STACKIT CLI", @@ -33,7 +33,7 @@ func NewCmd() *cobra.Command { } func Execute() { - err := NewCmd().Execute() + err := NewRootCmd().Execute() if err != nil { os.Exit(1) } diff --git a/scripts/generate.go b/scripts/generate.go index c2c29276..7ee5d0f2 100644 --- a/scripts/generate.go +++ b/scripts/generate.go @@ -37,7 +37,7 @@ func main() { linkHandler := func(filename string) string { return fmt.Sprintf("./%s", filename) } - err = doc.GenMarkdownTreeCustom(cmd.NewCmd(), docsDir, filePrepender, linkHandler) + err = doc.GenMarkdownTreeCustom(cmd.NewRootCmd(), docsDir, filePrepender, linkHandler) if err != nil { log.Fatalf("Error generating documentation: %v", err) } From cc3436d6b4c89d1c30a8e5ca461751ae672e871b Mon Sep 17 00:00:00 2001 From: Henrique Santos Date: Tue, 21 Nov 2023 15:13:00 +0000 Subject: [PATCH 4/4] Rename routine --- internal/cmd/auth/auth.go | 4 ++-- internal/cmd/config/config.go | 4 ++-- internal/cmd/dns/dns.go | 4 ++-- internal/cmd/dns/record-set/record_set.go | 4 ++-- internal/cmd/dns/zone/zone.go | 4 ++-- internal/cmd/postgresql/credential/credential.go | 4 ++-- internal/cmd/postgresql/instance/instance.go | 4 ++-- internal/cmd/postgresql/offerings/offerings.go | 4 ++-- internal/cmd/postgresql/postgresql.go | 4 ++-- internal/cmd/root.go | 4 ++-- internal/cmd/ske/cluster/cluster.go | 4 ++-- internal/cmd/ske/ske.go | 4 ++-- 12 files changed, 24 insertions(+), 24 deletions(-) diff --git a/internal/cmd/auth/auth.go b/internal/cmd/auth/auth.go index 2483897b..0a38f184 100644 --- a/internal/cmd/auth/auth.go +++ b/internal/cmd/auth/auth.go @@ -14,11 +14,11 @@ func NewCmd() *cobra.Command { Long: "Provides authentication functionality", Example: `$ stackit auth login`, } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(login.NewCmd()) cmd.AddCommand(activateserviceaccount.NewCmd()) } diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index ebc49443..3575907b 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -17,11 +17,11 @@ func NewCmd() *cobra.Command { Long: "CLI configuration options", Example: fmt.Sprintf("%s\n%s\n%s", set.NewCmd().Example, inspect.NewCmd().Example, unset.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(inspect.NewCmd()) cmd.AddCommand(set.NewCmd()) cmd.AddCommand(unset.NewCmd()) diff --git a/internal/cmd/dns/dns.go b/internal/cmd/dns/dns.go index a9ef026d..a3d71905 100644 --- a/internal/cmd/dns/dns.go +++ b/internal/cmd/dns/dns.go @@ -16,11 +16,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for DNS", Example: fmt.Sprintf("%s\n%s", zone.NewCmd().Example, recordset.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(zone.NewCmd()) cmd.AddCommand(recordset.NewCmd()) } diff --git a/internal/cmd/dns/record-set/record_set.go b/internal/cmd/dns/record-set/record_set.go index 7019c6fe..35ee2b57 100644 --- a/internal/cmd/dns/record-set/record_set.go +++ b/internal/cmd/dns/record-set/record_set.go @@ -19,11 +19,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for DNS record set", Example: fmt.Sprintf("%s\n%s", list.NewCmd().Example, create.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(list.NewCmd()) cmd.AddCommand(create.NewCmd()) cmd.AddCommand(describe.NewCmd()) diff --git a/internal/cmd/dns/zone/zone.go b/internal/cmd/dns/zone/zone.go index 3b1471a7..e1e4d354 100644 --- a/internal/cmd/dns/zone/zone.go +++ b/internal/cmd/dns/zone/zone.go @@ -19,11 +19,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for DNS zone", Example: fmt.Sprintf("%s\n%s", list.NewCmd().Example, create.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(list.NewCmd()) cmd.AddCommand(create.NewCmd()) cmd.AddCommand(describe.NewCmd()) diff --git a/internal/cmd/postgresql/credential/credential.go b/internal/cmd/postgresql/credential/credential.go index 53d29e80..64e639f1 100644 --- a/internal/cmd/postgresql/credential/credential.go +++ b/internal/cmd/postgresql/credential/credential.go @@ -18,11 +18,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for PostgreSQL credentials", Example: fmt.Sprintf("%s\n%s", create.NewCmd().Example, describe.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(create.NewCmd()) cmd.AddCommand(delete.NewCmd()) cmd.AddCommand(describe.NewCmd()) diff --git a/internal/cmd/postgresql/instance/instance.go b/internal/cmd/postgresql/instance/instance.go index 05e119c1..11aae296 100644 --- a/internal/cmd/postgresql/instance/instance.go +++ b/internal/cmd/postgresql/instance/instance.go @@ -19,11 +19,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for PostgreSQL instance", Example: fmt.Sprintf("%s\n%s", create.NewCmd().Example, list.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(create.NewCmd()) cmd.AddCommand(delete.NewCmd()) cmd.AddCommand(describe.NewCmd()) diff --git a/internal/cmd/postgresql/offerings/offerings.go b/internal/cmd/postgresql/offerings/offerings.go index 9178e9e4..54956e2e 100644 --- a/internal/cmd/postgresql/offerings/offerings.go +++ b/internal/cmd/postgresql/offerings/offerings.go @@ -13,10 +13,10 @@ func NewCmd() *cobra.Command { Long: "Provides information regarding the PostgreSQL service offerings", Example: list.NewCmd().Example, } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(list.NewCmd()) } diff --git a/internal/cmd/postgresql/postgresql.go b/internal/cmd/postgresql/postgresql.go index 4f68c7d0..38b78a8d 100644 --- a/internal/cmd/postgresql/postgresql.go +++ b/internal/cmd/postgresql/postgresql.go @@ -17,11 +17,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for PostgreSQL", Example: fmt.Sprintf("%s\n%s", instance.NewCmd().Example, credential.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(instance.NewCmd()) cmd.AddCommand(offerings.NewCmd()) cmd.AddCommand(credential.NewCmd()) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 240f7e4c..d4b3e001 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -28,7 +28,7 @@ func NewRootCmd() *cobra.Command { DisableAutoGenTag: true, } configureFlags(cmd) - addChilds(cmd) + addSubcommands(cmd) return cmd } @@ -46,7 +46,7 @@ func configureFlags(cmd *cobra.Command) { cobra.CheckErr(err) } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(auth.NewCmd()) cmd.AddCommand(config.NewCmd()) cmd.AddCommand(dns.NewCmd()) diff --git a/internal/cmd/ske/cluster/cluster.go b/internal/cmd/ske/cluster/cluster.go index 35b748aa..bf6d2f98 100644 --- a/internal/cmd/ske/cluster/cluster.go +++ b/internal/cmd/ske/cluster/cluster.go @@ -19,11 +19,11 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for SKE cluster", Example: fmt.Sprintf("%s\n%s", create.NewCmd().Example, list.NewCmd().Example), } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(create.NewCmd()) cmd.AddCommand(delete.NewCmd()) cmd.AddCommand(describe.NewCmd()) diff --git a/internal/cmd/ske/ske.go b/internal/cmd/ske/ske.go index ff3df54e..0697f782 100644 --- a/internal/cmd/ske/ske.go +++ b/internal/cmd/ske/ske.go @@ -13,10 +13,10 @@ func NewCmd() *cobra.Command { Long: "Provides functionality for STACKIT Kubernetes engine (SKE)", Example: cluster.NewCmd().Example, } - addChilds(cmd) + addSubcommands(cmd) return cmd } -func addChilds(cmd *cobra.Command) { +func addSubcommands(cmd *cobra.Command) { cmd.AddCommand(cluster.NewCmd()) }