Skip to content

Commit 268402d

Browse files
committed
Centralise logging, Implemented global --quiet (-q) flag
1 parent 4327e58 commit 268402d

File tree

102 files changed

+395
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+395
-344
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ The civo cli have multiple global options, that you can use, like this:
197197
--pretty Print pretty the json output
198198
--region string Choose the region to connect to, if you use this option it will use it over the default region
199199
-y, --yes Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactive
200+
-q, --quiet Disables logging and run non-interactive, requires --yes flag wherever user prompts are required
200201
```
201202

202203
## API Keys

cmd/apikey/apikey_current.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package apikey
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/civo/cli/config"
@@ -24,7 +23,7 @@ var apikeyCurrentCmd = &cobra.Command{
2423
if index != "" {
2524
config.Current.Meta.CurrentAPIKey = index
2625
config.SaveConfig()
27-
fmt.Printf("Set the default API Key to be %s\n", utility.Green(index))
26+
utility.Printf("Set the default API Key to be %s\n", utility.Green(index))
2827
}
2928

3029
},

cmd/apikey/apikey_remove.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package apikey
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/civo/cli/common"
@@ -41,13 +40,13 @@ var apikeyRemoveCmd = &cobra.Command{
4140
config.SaveConfig()
4241

4342
if numKeys > len(config.Current.APIKeys) {
44-
fmt.Printf("Removed the API Key %s\n", utility.Green(index))
43+
utility.Printf("Removed the API Key %s\n", utility.Green(index))
4544
} else {
4645
utility.Error("The API Key %q couldn't be found", args[0])
4746
os.Exit(1)
4847
}
4948
} else {
50-
fmt.Println("Operation aborted.")
49+
utility.Println("Operation aborted.")
5150
}
5251

5352
},

cmd/apikey/apikey_save.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package apikey
22

33
import (
44
"bufio"
5-
"fmt"
65
"os"
76
"runtime"
87
"strings"
@@ -59,9 +58,14 @@ var apikeySaveCmd = &cobra.Command{
5958
os.Exit(1)
6059
}
6160

61+
if len(args) == 0 && common.Quiet {
62+
utility.Info("You need to use the non-interactive way when in quiet mode")
63+
os.Exit(1)
64+
}
65+
6266
if len(args) == 0 && !loadAPIKeyFromEnv {
6367
reader := bufio.NewReader(os.Stdin)
64-
fmt.Printf("Enter a nice name for this account/API Key: ")
68+
utility.Printf("Enter a nice name for this account/API Key: ")
6569

6670
name, err = reader.ReadString('\n')
6771
if err != nil {
@@ -73,7 +77,7 @@ var apikeySaveCmd = &cobra.Command{
7377
} else {
7478
name = strings.TrimSuffix(name, "\n")
7579
}
76-
fmt.Printf("Enter the API key: ")
80+
utility.Printf("Enter the API key: ")
7781
apikeyBytes, err := term.ReadPassword(int(syscall.Stdin))
7882
if err != nil {
7983
utility.Error("Error reading api key %v", err)
@@ -140,7 +144,7 @@ var apikeySaveCmd = &cobra.Command{
140144
case "custom":
141145
ow.WriteCustomOutput(common.OutputFields)
142146
default:
143-
fmt.Printf("Saved the API Key %s\n", utility.Green(name))
147+
utility.Printf("Saved the API Key %s\n", utility.Green(name))
144148
}
145149

146150
},

cmd/database/database_backup_create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/adhocore/gronx"
@@ -106,7 +105,7 @@ var dbBackupCreateCmd = &cobra.Command{
106105
case "custom":
107106
ow.WriteCustomOutput(common.OutputFields)
108107
default:
109-
fmt.Printf("Database backup (%s) for database %s has been created\n", utility.Green(name), utility.Green(bk.DatabaseName))
108+
utility.Printf("Database backup (%s) for database %s has been created\n", utility.Green(name), utility.Green(bk.DatabaseName))
110109
}
111110
},
112111
}

cmd/database/database_backup_delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package database
22

33
import (
44
"errors"
5-
"fmt"
65
"strings"
76

87
pluralize "github.com/alejandrojnm/go-pluralize"
@@ -99,10 +98,10 @@ var dbBackupDeleteCmd = &cobra.Command{
9998
case "custom":
10099
ow.WriteCustomOutput(common.OutputFields)
101100
default:
102-
fmt.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(backupList), "database backup"), utility.Green(strings.Join(dbNameList, ", ")))
101+
utility.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(backupList), "database backup"), utility.Green(strings.Join(dbNameList, ", ")))
103102
}
104103
} else {
105-
fmt.Println("Operation aborted")
104+
utility.Println("Operation aborted")
106105
}
107106
},
108107
}

cmd/database/database_backup_list.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/civo/civogo"
@@ -82,7 +81,7 @@ func postgresScheduledBackups(backups *civogo.PaginatedDatabaseBackup) {
8281
ow.WriteCustomOutput(common.OutputFields)
8382
default:
8483
if printMsg {
85-
fmt.Println("Scheduled backup")
84+
utility.Println("Scheduled backup")
8685
}
8786
ow.WriteTable()
8887
}
@@ -119,7 +118,7 @@ func postgresManualBackups(backups *civogo.PaginatedDatabaseBackup) {
119118
ow.WriteCustomOutput(common.OutputFields)
120119
default:
121120
if printMsg {
122-
fmt.Println("Manual backups")
121+
utility.Println("Manual backups")
123122
}
124123
ow.WriteTable()
125124
}
@@ -153,7 +152,7 @@ func mysqlBackups(backups *civogo.PaginatedDatabaseBackup) {
153152
ow.WriteCustomOutput(common.OutputFields)
154153
default:
155154
if printMsg {
156-
fmt.Println("Manual backups")
155+
utility.Println("Manual backups")
157156
}
158157
ow.WriteTable()
159158
}

cmd/database/database_backup_update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/civo/civogo"
@@ -75,7 +74,7 @@ var dbBackupUpdateCmd = &cobra.Command{
7574
case "custom":
7675
ow.WriteCustomOutput(common.OutputFields)
7776
default:
78-
fmt.Printf("Database backup (%s) for database %s has been update\n", utility.Green(bk.Name), utility.Green(bk.DatabaseName))
77+
utility.Printf("Database backup (%s) for database %s has been update\n", utility.Green(bk.Name), utility.Green(bk.DatabaseName))
7978
}
8079
},
8180
}

cmd/database/database_create.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,13 @@ var dbCreateCmd = &cobra.Command{
178178
startTime := utility.StartTime()
179179

180180
stillCreating := true
181-
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
182-
s.Writer = os.Stderr
183-
s.Prefix = fmt.Sprintf("Create a database called %s ", db.Name)
184-
s.Start()
181+
var s *spinner.Spinner
182+
if !common.Quiet {
183+
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
184+
s.Writer = os.Stderr
185+
s.Prefix = fmt.Sprintf("Create a database called %s ", db.Name)
186+
s.Start()
187+
}
185188

186189
for stillCreating {
187190
databaseCheck, err := client.FindDatabase(db.ID)
@@ -191,7 +194,9 @@ var dbCreateCmd = &cobra.Command{
191194
}
192195
if databaseCheck.Status == "Ready" {
193196
stillCreating = false
194-
s.Stop()
197+
if !common.Quiet {
198+
s.Stop()
199+
}
195200
} else {
196201
time.Sleep(2 * time.Second)
197202
}
@@ -208,9 +213,9 @@ var dbCreateCmd = &cobra.Command{
208213
ow.WriteCustomOutput(common.OutputFields)
209214
default:
210215
if executionTime != "" {
211-
fmt.Printf("Database (%s) type %s version %s with ID %s and size %s has been created in %s\nTo get fetch the database credentials use the command:\n\ncivo database credentials %s\n", utility.Green(db.Name), strings.ToLower(db.Software), db.SoftwareVersion, db.ID, db.Size, executionTime, db.Name)
216+
utility.Printf("Database (%s) type %s version %s with ID %s and size %s has been created in %s\nTo get fetch the database credentials use the command:\n\ncivo database credentials %s\n", utility.Green(db.Name), strings.ToLower(db.Software), db.SoftwareVersion, db.ID, db.Size, executionTime, db.Name)
212217
} else {
213-
fmt.Printf("Database (%s) type %s version %s with ID %s and size %s has been created\nTo get fetch the database credentials use the command:\n\ncivo database credentials %s\n", utility.Green(db.Name), strings.ToLower(db.Software), db.SoftwareVersion, db.ID, db.Size, db.Name)
218+
utility.Printf("Database (%s) type %s version %s with ID %s and size %s has been created\nTo get fetch the database credentials use the command:\n\ncivo database credentials %s\n", utility.Green(db.Name), strings.ToLower(db.Software), db.SoftwareVersion, db.ID, db.Size, db.Name)
214219
}
215220
}
216221
},

cmd/database/database_credential.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ var dbCredentialCmd = &cobra.Command{
4646

4747
// Add check for database status
4848
if db.Status == "Pending" {
49-
fmt.Printf("The DB %s is currently being provisioned, please wait...\n", utility.Green(db.Name))
50-
51-
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
52-
s.Writer = os.Stderr
53-
s.Prefix = fmt.Sprintf("Waiting for database (%s)... ", db.Name)
54-
s.Start()
49+
utility.Printf("The DB %s is currently being provisioned, please wait...\n", utility.Green(db.Name))
50+
51+
var s *spinner.Spinner
52+
if !common.Quiet {
53+
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
54+
s.Writer = os.Stderr
55+
s.Prefix = fmt.Sprintf("Waiting for database (%s)... ", db.Name)
56+
s.Start()
57+
}
5558

5659
for db.Status == "Pending" {
5760
db, err = client.FindDatabase(args[0])
@@ -61,14 +64,16 @@ var dbCredentialCmd = &cobra.Command{
6164
}
6265
time.Sleep(2 * time.Second)
6366
}
64-
s.Stop()
67+
if !common.Quiet {
68+
s.Stop()
69+
}
6570
}
6671

6772
connStr := strings.ToLower(db.Software) + "://" + db.DatabaseUserInfo[0].Username + ":" + db.DatabaseUserInfo[0].Password + "@" + db.PublicIPv4 + ":" + fmt.Sprintf("%d", db.DatabaseUserInfo[0].Port)
6873

6974
if connectionString {
7075
for _, user := range db.DatabaseUserInfo {
71-
fmt.Printf("%s://%s:%s@%s:%d\n", strings.ToLower(db.Software), user.Username, user.Password, db.PublicIPv4, user.Port)
76+
utility.Printf("%s://%s:%s@%s:%d\n", strings.ToLower(db.Software), user.Username, user.Password, db.PublicIPv4, user.Port)
7277
}
7378
return
7479
}

cmd/database/database_delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package database
22

33
import (
44
"errors"
5-
"fmt"
65
"strings"
76

87
pluralize "github.com/alejandrojnm/go-pluralize"
@@ -95,10 +94,10 @@ var dbDeleteCmd = &cobra.Command{
9594
case "custom":
9695
ow.WriteCustomOutput(common.OutputFields)
9796
default:
98-
fmt.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(backupList), "database"), utility.Green(strings.Join(dbNameList, ", ")))
97+
utility.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(backupList), "database"), utility.Green(strings.Join(dbNameList, ", ")))
9998
}
10099
} else {
101-
fmt.Println("Operation aborted")
100+
utility.Println("Operation aborted")
102101
}
103102
},
104103
}

cmd/database/database_restore.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
5-
64
"github.com/civo/civogo"
75
"github.com/civo/cli/common"
86
"github.com/civo/cli/config"
@@ -67,10 +65,10 @@ var dbRestoreCmd = &cobra.Command{
6765
case "custom":
6866
ow.WriteCustomOutput(common.OutputFields)
6967
default:
70-
fmt.Printf("Restoring database %s from from backup %s\n", utility.Green(db.Name), utility.Green(backup))
68+
utility.Printf("Restoring database %s from from backup %s\n", utility.Green(db.Name), utility.Green(backup))
7169
}
7270
} else {
73-
fmt.Println("Aborted")
71+
utility.Println("Aborted")
7472
}
7573

7674
},

cmd/database/database_show.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var dbShowCmd = &cobra.Command{
4848
ow := utility.NewOutputWriter()
4949

5050
ow.StartLine()
51-
fmt.Println()
51+
utility.Println()
5252
ow.AppendDataWithLabel("id", db.ID, "ID")
5353
ow.AppendDataWithLabel("name", db.Name, "Name")
5454
ow.AppendDataWithLabel("status", db.Status, "Status")
@@ -73,7 +73,7 @@ var dbShowCmd = &cobra.Command{
7373
ow.WriteCustomOutput(common.OutputFields)
7474
default:
7575
ow.WriteKeyValues()
76-
fmt.Println("To get the credentials, run : civo db credential", db.Name)
76+
utility.Println("To get the credentials, run : civo db credential", db.Name)
7777
}
7878
},
7979
}

cmd/database/database_update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/civo/civogo"
@@ -55,7 +54,7 @@ var dbUpdateCmd = &cobra.Command{
5554
case "custom":
5655
ow.WriteCustomOutput(common.OutputFields)
5756
default:
58-
fmt.Printf("The Database %s was updated\n", utility.Green(findDB.Name))
57+
utility.Printf("The Database %s was updated\n", utility.Green(findDB.Name))
5958
os.Exit(0)
6059
}
6160
},

cmd/database/database_version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package database
22

33
import (
4+
"os"
5+
46
"github.com/civo/cli/common"
57
"github.com/civo/cli/config"
68
"github.com/civo/cli/utility"
79
"github.com/spf13/cobra"
8-
"os"
910
)
1011

1112
var dbVersionListCmd = &cobra.Command{

cmd/domain/domain_create.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package domain
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/civo/cli/common"
@@ -36,10 +35,10 @@ var domainCreateCmd = &cobra.Command{
3635
case "custom":
3736
ow.WriteCustomOutput(common.OutputFields)
3837
default:
39-
fmt.Printf("Created a domain called %s with ID %s\n", utility.Green(domain.Name), utility.Green(domain.ID))
40-
fmt.Println("Please point your domain registrar to Civo nameservers:")
41-
fmt.Printf("%s\n", utility.Green("ns0.civo.com"))
42-
fmt.Printf("%s\n", utility.Green("ns1.civo.com"))
38+
utility.Printf("Created a domain called %s with ID %s\n", utility.Green(domain.Name), utility.Green(domain.ID))
39+
utility.Println("Please point your domain registrar to Civo nameservers:")
40+
utility.Printf("%s\n", utility.Green("ns0.civo.com"))
41+
utility.Printf("%s\n", utility.Green("ns1.civo.com"))
4342
}
4443
},
4544
}

cmd/domain/domain_record_create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package domain
22

33
import (
4-
"fmt"
54
"os"
65
"strconv"
76
"strings"
@@ -83,7 +82,7 @@ var domainRecordCreateCmd = &cobra.Command{
8382
case "custom":
8483
ow.WriteCustomOutput(common.OutputFields)
8584
default:
86-
fmt.Printf("Created %s record %s for %s with a TTL of %s seconds and with a priority of %s with ID %s", utility.Green(string(record.Type)), utility.Green(record.Name), utility.Green(domain.Name), utility.Green(strconv.Itoa(record.TTL)), utility.Green(strconv.Itoa(record.Priority)), utility.Green(record.ID))
85+
utility.Printf("Created %s record %s for %s with a TTL of %s seconds and with a priority of %s with ID %s", utility.Green(string(record.Type)), utility.Green(record.Name), utility.Green(domain.Name), utility.Green(strconv.Itoa(record.TTL)), utility.Green(strconv.Itoa(record.Priority)), utility.Green(record.ID))
8786
}
8887
},
8988
}

0 commit comments

Comments
 (0)