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 2585233b..59117010 100644 --- a/internal/cmd/auth/activate-service-account/activate_service_account.go +++ b/internal/cmd/auth/activate-service-account/activate_service_account.go @@ -61,7 +61,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("authenticate service account: %w", err) } - fmt.Printf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email) + cmd.Printf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email) return nil }, diff --git a/internal/cmd/auth/login/login.go b/internal/cmd/auth/login/login.go index e17b59ff..ef4c6c34 100644 --- a/internal/cmd/auth/login/login.go +++ b/internal/cmd/auth/login/login.go @@ -2,7 +2,6 @@ package login import ( "fmt" - "os" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" @@ -14,14 +13,14 @@ var Cmd = &cobra.Command{ Short: "Login to the provider", Long: "Login to the provider", Example: `$ stackit auth login`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { err := auth.AuthorizeUser() if err != nil { - fmt.Printf("Authorization failed: %s\n", err) - os.Exit(1) + return fmt.Errorf("authorization failed: %w", err) } - fmt.Println("Successfully logged into STACKIT CLI.") + cmd.Println("Successfully logged into STACKIT CLI.") + return nil }, } diff --git a/internal/cmd/config/inspect/inspect.go b/internal/cmd/config/inspect/inspect.go index d9eacca1..c8df01d7 100644 --- a/internal/cmd/config/inspect/inspect.go +++ b/internal/cmd/config/inspect/inspect.go @@ -25,7 +25,7 @@ var Cmd = &cobra.Command{ if err != nil { return fmt.Errorf("marshal config: %w", err) } - fmt.Println(string(configJSON)) + cmd.Println(string(configJSON)) return nil }, diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index ff0e2185..9ed60b86 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -67,7 +67,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for DNS record set creation: %w", err) } - fmt.Printf("Created record set with ID %s\n", recordSetId) + cmd.Printf("Created record set with ID %s\n", recordSetId) return nil }, } diff --git a/internal/cmd/dns/record-set/delete/delete.go b/internal/cmd/dns/record-set/delete/delete.go index 9c4c736c..673f7ce4 100644 --- a/internal/cmd/dns/record-set/delete/delete.go +++ b/internal/cmd/dns/record-set/delete/delete.go @@ -61,7 +61,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for DNS record set deletion: %w", err) } - fmt.Println("Record set deleted") + cmd.Println("Record set deleted") return nil }, } diff --git a/internal/cmd/dns/record-set/describe/describe.go b/internal/cmd/dns/record-set/describe/describe.go index 85fd3df1..92f3d8de 100644 --- a/internal/cmd/dns/record-set/describe/describe.go +++ b/internal/cmd/dns/record-set/describe/describe.go @@ -58,7 +58,7 @@ var Cmd = &cobra.Command{ if err != nil { return fmt.Errorf("marshal DNS record set: %w", err) } - fmt.Println(string(details)) + cmd.Println(string(details)) return nil }, diff --git a/internal/cmd/dns/record-set/list/list.go b/internal/cmd/dns/record-set/list/list.go index f138e5ed..03def65b 100644 --- a/internal/cmd/dns/record-set/list/list.go +++ b/internal/cmd/dns/record-set/list/list.go @@ -58,7 +58,7 @@ var Cmd = &cobra.Command{ } recordSets := *resp.RrSets if len(recordSets) == 0 { - fmt.Printf("No record-sets found for zone with ID %s\n", model.ZoneId) + cmd.Printf("No record-sets found for zone with ID %s\n", model.ZoneId) return nil } @@ -68,7 +68,7 @@ var Cmd = &cobra.Command{ for _, recordSet := range recordSets { table.AddRow(*recordSet.Id, *recordSet.Name, *recordSet.Type, *recordSet.State) } - table.Render() + table.Render(cmd) return nil }, diff --git a/internal/cmd/dns/record-set/list/list_test.go b/internal/cmd/dns/record-set/list/list_test.go index e911a41e..96b4e0a1 100644 --- a/internal/cmd/dns/record-set/list/list_test.go +++ b/internal/cmd/dns/record-set/list/list_test.go @@ -2,7 +2,6 @@ package list import ( "context" - "fmt" "testing" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -187,7 +186,6 @@ func TestParseFlags(t *testing.T) { for flag, value := range tt.flagValues { err := cmd.Flags().Set(flag, value) - fmt.Printf("setting flag --%s=%s: %v", flag, value, err) if err != nil { if !tt.isValid { return diff --git a/internal/cmd/dns/record-set/update/update.go b/internal/cmd/dns/record-set/update/update.go index b9b6e7c0..c8c5880f 100644 --- a/internal/cmd/dns/record-set/update/update.go +++ b/internal/cmd/dns/record-set/update/update.go @@ -66,7 +66,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for DNS record set update: %w", err) } - fmt.Println("Record set updated") + cmd.Println("Record set updated") return nil }, } diff --git a/internal/cmd/dns/zone/create/create.go b/internal/cmd/dns/zone/create/create.go index 0c7800f0..e38bcfb1 100644 --- a/internal/cmd/dns/zone/create/create.go +++ b/internal/cmd/dns/zone/create/create.go @@ -80,7 +80,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for DNS zone creation: %w", err) } - fmt.Printf("Created zone with ID %s\n", zoneId) + cmd.Printf("Created zone with ID %s\n", zoneId) return nil }, } diff --git a/internal/cmd/dns/zone/delete/delete.go b/internal/cmd/dns/zone/delete/delete.go index 843fd7f3..b558e773 100644 --- a/internal/cmd/dns/zone/delete/delete.go +++ b/internal/cmd/dns/zone/delete/delete.go @@ -59,7 +59,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for DNS zone deletion: %w", err) } - fmt.Println("Zone deleted") + cmd.Println("Zone deleted") return nil }, } diff --git a/internal/cmd/dns/zone/describe/describe.go b/internal/cmd/dns/zone/describe/describe.go index fea57332..f34b0de1 100644 --- a/internal/cmd/dns/zone/describe/describe.go +++ b/internal/cmd/dns/zone/describe/describe.go @@ -55,7 +55,7 @@ var Cmd = &cobra.Command{ if err != nil { return fmt.Errorf("marshal DNS zone: %w", err) } - fmt.Println(string(details)) + cmd.Println(string(details)) return nil }, diff --git a/internal/cmd/dns/zone/list/list.go b/internal/cmd/dns/zone/list/list.go index eee92bcf..bddd3549 100644 --- a/internal/cmd/dns/zone/list/list.go +++ b/internal/cmd/dns/zone/list/list.go @@ -56,7 +56,7 @@ var Cmd = &cobra.Command{ } zones := *resp.Zones if len(zones) == 0 { - fmt.Printf("No zones found for project with ID %s\n", model.ProjectId) + cmd.Printf("No zones found for project with ID %s\n", model.ProjectId) return nil } @@ -66,7 +66,7 @@ var Cmd = &cobra.Command{ for _, zone := range zones { table.AddRow(*zone.Id, *zone.Name, *zone.DnsName, *zone.State) } - table.Render() + table.Render(cmd) return nil }, diff --git a/internal/cmd/dns/zone/update/update.go b/internal/cmd/dns/zone/update/update.go index 31bf2522..4a915330 100644 --- a/internal/cmd/dns/zone/update/update.go +++ b/internal/cmd/dns/zone/update/update.go @@ -79,7 +79,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for DNS zone update: %w", err) } - fmt.Println("Zone updated") + cmd.Println("Zone updated") return nil }, } diff --git a/internal/cmd/postgresql/credential/create/create.go b/internal/cmd/postgresql/credential/create/create.go index dd59020a..56b32e90 100644 --- a/internal/cmd/postgresql/credential/create/create.go +++ b/internal/cmd/postgresql/credential/create/create.go @@ -49,7 +49,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("create PostgreSQL credentials: %w", err) } - fmt.Printf("Created credentials with ID %s\n", *resp.Id) + cmd.Printf("Created credentials with ID %s\n", *resp.Id) return nil }, } diff --git a/internal/cmd/postgresql/credential/delete/delete.go b/internal/cmd/postgresql/credential/delete/delete.go index ed645036..3b02dcf2 100644 --- a/internal/cmd/postgresql/credential/delete/delete.go +++ b/internal/cmd/postgresql/credential/delete/delete.go @@ -51,7 +51,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("delete PostgreSQL credentials: %w", err) } - fmt.Printf("Deleted credentials with ID %s\n", model.CredentialsId) + cmd.Printf("Deleted credentials with ID %s\n", model.CredentialsId) return nil }, } diff --git a/internal/cmd/postgresql/credential/describe/describe.go b/internal/cmd/postgresql/credential/describe/describe.go index ddd7dc09..60586adf 100644 --- a/internal/cmd/postgresql/credential/describe/describe.go +++ b/internal/cmd/postgresql/credential/describe/describe.go @@ -57,7 +57,7 @@ var Cmd = &cobra.Command{ if err != nil { return fmt.Errorf("marshal PostgreSQL credentials: %w", err) } - fmt.Println(string(details)) + cmd.Println(string(details)) return nil }, diff --git a/internal/cmd/postgresql/credential/list/list.go b/internal/cmd/postgresql/credential/list/list.go index 261fed45..21972915 100644 --- a/internal/cmd/postgresql/credential/list/list.go +++ b/internal/cmd/postgresql/credential/list/list.go @@ -51,7 +51,7 @@ var Cmd = &cobra.Command{ } credentials := *resp.CredentialsList if len(credentials) == 0 { - fmt.Printf("No credentials found for instance with ID %s\n", model.InstanceId) + cmd.Printf("No credentials found for instance with ID %s\n", model.InstanceId) return nil } @@ -61,7 +61,7 @@ var Cmd = &cobra.Command{ for _, i := range credentials { table.AddRow(*i.Id) } - table.Render() + table.Render(cmd) return nil }, diff --git a/internal/cmd/postgresql/instance/create/create.go b/internal/cmd/postgresql/instance/create/create.go index 5e0fc290..6583b905 100644 --- a/internal/cmd/postgresql/instance/create/create.go +++ b/internal/cmd/postgresql/instance/create/create.go @@ -85,7 +85,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for PostgreSQL instance creation: %w", err) } - fmt.Printf("Created instance with ID %s\n", instanceId) + cmd.Printf("Created instance with ID %s\n", instanceId) return nil }, } diff --git a/internal/cmd/postgresql/instance/delete/delete.go b/internal/cmd/postgresql/instance/delete/delete.go index ae5855a1..5c0a6ad0 100644 --- a/internal/cmd/postgresql/instance/delete/delete.go +++ b/internal/cmd/postgresql/instance/delete/delete.go @@ -55,7 +55,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for PostgreSQL instance deletion: %w", err) } - fmt.Printf("Deleted instance with ID %s\n", model.InstanceId) + cmd.Printf("Deleted instance with ID %s\n", model.InstanceId) return nil }, } diff --git a/internal/cmd/postgresql/instance/describe/describe.go b/internal/cmd/postgresql/instance/describe/describe.go index 9b9ab88c..58a211af 100644 --- a/internal/cmd/postgresql/instance/describe/describe.go +++ b/internal/cmd/postgresql/instance/describe/describe.go @@ -54,7 +54,7 @@ var Cmd = &cobra.Command{ if err != nil { return fmt.Errorf("marshal PostgreSQL instance: %w", err) } - fmt.Println(string(details)) + cmd.Println(string(details)) return nil }, diff --git a/internal/cmd/postgresql/instance/list/list.go b/internal/cmd/postgresql/instance/list/list.go index 60b79200..e11dd0ed 100644 --- a/internal/cmd/postgresql/instance/list/list.go +++ b/internal/cmd/postgresql/instance/list/list.go @@ -43,7 +43,7 @@ var Cmd = &cobra.Command{ } instances := *resp.Instances if len(instances) == 0 { - fmt.Printf("No instances found for product with ID %s\n", model.ProjectId) + cmd.Printf("No instances found for product with ID %s\n", model.ProjectId) return nil } @@ -53,7 +53,7 @@ var Cmd = &cobra.Command{ for _, i := range instances { table.AddRow(*i.InstanceId, *i.Name, *i.LastOperation.Type, *i.LastOperation.State) } - table.Render() + table.Render(cmd) return nil }, diff --git a/internal/cmd/postgresql/instance/update/update.go b/internal/cmd/postgresql/instance/update/update.go index 9cf87a6f..779c759d 100644 --- a/internal/cmd/postgresql/instance/update/update.go +++ b/internal/cmd/postgresql/instance/update/update.go @@ -86,7 +86,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for PostgreSQL instance update: %w", err) } - fmt.Printf("Updated instance with ID %s\n", instanceId) + cmd.Printf("Updated instance with ID %s\n", instanceId) return nil }, } diff --git a/internal/cmd/postgresql/offerings/list/list.go b/internal/cmd/postgresql/offerings/list/list.go index 2ec5eba1..7334090c 100644 --- a/internal/cmd/postgresql/offerings/list/list.go +++ b/internal/cmd/postgresql/offerings/list/list.go @@ -53,7 +53,7 @@ var Cmd = &cobra.Command{ table.AddSeparator() } table.EnableAutoMergeOnColumns(1) - table.Render() + table.Render(cmd) return nil }, diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index 5dc680ec..acd88684 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -74,7 +74,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for SKE cluster creation: %w", err) } - fmt.Printf("Created cluster with name %s\n", name) + cmd.Printf("Created cluster with name %s\n", name) return nil }, } diff --git a/internal/cmd/ske/cluster/delete/delete.go b/internal/cmd/ske/cluster/delete/delete.go index eca7f4a9..333617c7 100644 --- a/internal/cmd/ske/cluster/delete/delete.go +++ b/internal/cmd/ske/cluster/delete/delete.go @@ -54,7 +54,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for SKE cluster deletion: %w", err) } - fmt.Println("Cluster deleted") + cmd.Println("Cluster deleted") return nil }, } diff --git a/internal/cmd/ske/cluster/list/list.go b/internal/cmd/ske/cluster/list/list.go index feb1cdd1..9dac0ac9 100644 --- a/internal/cmd/ske/cluster/list/list.go +++ b/internal/cmd/ske/cluster/list/list.go @@ -47,7 +47,7 @@ var Cmd = &cobra.Command{ } clusters := *resp.Items if len(clusters) == 0 { - fmt.Printf("No clusters found for project with ID %s\n", model.ProjectId) + cmd.Printf("No clusters found for project with ID %s\n", model.ProjectId) return nil } @@ -57,7 +57,7 @@ var Cmd = &cobra.Command{ for _, cluster := range clusters { table.AddRow(*cluster.Name, *cluster.Status.Aggregated) } - table.Render() + table.Render(cmd) return nil }, diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index bd075e86..e862e002 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -57,7 +57,7 @@ var Cmd = &cobra.Command{ return fmt.Errorf("wait for SKE cluster update: %w", err) } - fmt.Printf("Updated cluster with name %s\n", name) + cmd.Printf("Updated cluster with name %s\n", name) return nil }, } diff --git a/internal/pkg/auth/user_login.go b/internal/pkg/auth/user_login.go index 7c7a1f7c..b043534d 100644 --- a/internal/pkg/auth/user_login.go +++ b/internal/pkg/auth/user_login.go @@ -151,7 +151,6 @@ func getUserAccessAndRefreshTokens(authDomain, clientID, codeVerifier, authoriza req.Header.Add("content-type", "application/x-www-form-urlencoded") res, err := http.DefaultClient.Do(req) if err != nil { - fmt.Printf("HTTP error: %s", err) return "", "", fmt.Errorf("call access token endpoint: %w", err) } @@ -174,14 +173,13 @@ func getUserAccessAndRefreshTokens(authDomain, clientID, codeVerifier, authoriza }{} err = json.Unmarshal(body, &responseData) if err != nil { - fmt.Printf("JSON error: %s", err) - return "", "", err + return "", "", fmt.Errorf("unmarshal response: %w", err) } if responseData.AccessToken == "" { - fmt.Printf("found no access token") + return "", "", fmt.Errorf("found no access token") } if responseData.RefreshToken == "" { - fmt.Printf("found no refresh token") + return "", "", fmt.Errorf("found no refresh token") } return responseData.AccessToken, responseData.RefreshToken, nil diff --git a/internal/pkg/tables/tables.go b/internal/pkg/tables/tables.go index 880a779e..77260116 100644 --- a/internal/pkg/tables/tables.go +++ b/internal/pkg/tables/tables.go @@ -1,10 +1,8 @@ package tables import ( - "fmt" - "os" - "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" ) type Table struct { @@ -14,7 +12,6 @@ type Table struct { // Creates a new table func NewTable() Table { t := table.NewWriter() - t.SetOutputMirror(os.Stdout) return Table{ table: t, } @@ -45,13 +42,11 @@ func (t *Table) EnableAutoMergeOnColumns(columns ...int) { } // Renders the table -func (t *Table) Render() { - fmt.Print("\n") +func (t *Table) Render(cmd *cobra.Command) { t.table.SetStyle(table.StyleLight) t.table.Style().Options.DrawBorder = false t.table.Style().Options.SeparateRows = false t.table.Style().Options.SeparateColumns = true t.table.Style().Options.SeparateHeader = true - t.table.Render() - fmt.Print("\n") + cmd.Printf("\n%s\n", t.table.Render()) }