Skip to content

Commit

Permalink
Merge pull request #9 from stackitcloud/hs/harmonize-prints
Browse files Browse the repository at this point in the history
Harmonize prints to console
  • Loading branch information
hcsa73 authored Nov 21, 2023
2 parents fd64380 + cac2dfe commit 3554dc1
Show file tree
Hide file tree
Showing 30 changed files with 41 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
9 changes: 4 additions & 5 deletions internal/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package login

import (
"fmt"
"os"

"github.com/stackitcloud/stackit-cli/internal/pkg/auth"

Expand All @@ -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
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/config/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/record-set/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/record-set/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/record-set/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/dns/record-set/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
},
Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/dns/record-set/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package list

import (
"context"
"fmt"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/record-set/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/zone/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/zone/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/zone/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/dns/zone/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/dns/zone/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/credential/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/credential/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/credential/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/postgresql/credential/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -61,7 +61,7 @@ var Cmd = &cobra.Command{
for _, i := range credentials {
table.AddRow(*i.Id)
}
table.Render()
table.Render(cmd)

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/instance/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/instance/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/postgresql/instance/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/instance/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/postgresql/offerings/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var Cmd = &cobra.Command{
table.AddSeparator()
}
table.EnableAutoMergeOnColumns(1)
table.Render()
table.Render(cmd)

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/ske/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/ske/cluster/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/ske/cluster/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/ske/cluster/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
8 changes: 3 additions & 5 deletions internal/pkg/auth/user_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions internal/pkg/tables/tables.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package tables

import (
"fmt"
"os"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)

type Table struct {
Expand All @@ -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,
}
Expand Down Expand Up @@ -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())
}

0 comments on commit 3554dc1

Please sign in to comment.