Skip to content

Commit

Permalink
chore: unify CLI output library; use yellow for warnings, red for errors
Browse files Browse the repository at this point in the history
Signed-off-by: Camila Macedo <[email protected]>
  • Loading branch information
camilamacedo86 committed Oct 2, 2023
1 parent 0ac0f5f commit b775791
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 50 deletions.
4 changes: 2 additions & 2 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (d Device) Online(inactiveHoursThreshold int) bool {
return false
}
} else {
logrus.Error(err)
color.Red(fmt.Sprint(err))
return false
}
return true
Expand All @@ -534,7 +534,7 @@ func NewApiClient(serverUrl string, config Config, caCertPath string, version st
}

if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
logrus.Warning("No certs appended, using system certs only")
color.Yellow("No certs appended, using system certs only")
}
tlsCfg.RootCAs = rootCAs
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -63,12 +64,13 @@ func Execute() {
}

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
color.Red(fmt.Sprint(err))
os.Exit(1)
}
}

func init() {
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/fioctl.yaml)")
Expand Down Expand Up @@ -103,12 +105,12 @@ func init() {
func getConfigDir() string {
config, err := homedir.Expand("~/.config")
if err != nil {
fmt.Println(err)
color.Red(fmt.Sprint(err))
os.Exit(1)
}
if _, err := os.Stat(config); errors.Is(err, fs.ErrNotExist) {
if err := os.Mkdir(config, 0755); err != nil {
fmt.Println(err)
color.Red(fmt.Sprint(err))
os.Exit(1)
}
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func initConfig() {
logrus.Debug("Config file not found")
} else {
// Config file was found but another error was produced
fmt.Println("ERROR: ", err)
color.Red("ERROR: ", err)
os.Exit(1)
}
}
Expand Down
6 changes: 4 additions & 2 deletions subcommands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"text/tabwriter"

"github.com/fatih/color"

"github.com/cheynewallace/tabby"
canonical "github.com/docker/go/canonical/json"
homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -138,7 +140,7 @@ func DieNotNil(err error, message ...string) {
parts = append(parts, p)
}
parts = append(parts, err)
fmt.Println(parts...)
color.Red(fmt.Sprint(parts...))
for _, w := range onLastWill {
w()
}
Expand Down Expand Up @@ -177,7 +179,7 @@ func AssertWritable(path string) {
DieNotNil(err)
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, st.Mode())
if err != nil {
fmt.Println("ERROR: File is not writeable:", path)
color.Red(fmt.Sprintf("ERROR: File is not writeable:%s", path))
os.Exit(1)
}
f.Close()
Expand Down
4 changes: 3 additions & 1 deletion subcommands/config/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"strings"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (w *WireguardServerConfig) Unmarshall(configVal string) {
} else if k == "enabled" {
w.Enabled = v != "0"
} else {
fmt.Println("ERROR: Unexpected client config key: ", k)
color.Red(fmt.Sprintf("ERROR: Unexpected client config key: %s", k))
os.Exit(1)
}
}
Expand Down
16 changes: 9 additions & 7 deletions subcommands/devices/config_wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -54,7 +56,7 @@ func (w *WireguardClientConfig) Unmarshall(configVal string) {
} else if k == "pubkey" {
w.PublicKey = strings.TrimSpace(parts[1])
} else {
fmt.Println("ERROR: Unexpected client config key: ", k)
color.Red(fmt.Sprintf("ERROR: Unexpected client config key: %s ", k))
os.Exit(1)
}
}
Expand Down Expand Up @@ -94,7 +96,7 @@ func factoryIps(factory string) map[uint32]bool {
for _, item := range ipList {
ip, err := ipToUint32(item.Ip)
if err != nil {
logrus.Errorf("Unable to compute VPN Address for %s - %s", item.Name, item.Ip)
color.Red(fmt.Sprintf("Unable to compute VPN Address for %s - %s", item.Name, item.Ip))
} else {
ips[ip] = true
}
Expand All @@ -105,13 +107,13 @@ func factoryIps(factory string) map[uint32]bool {
func findVpnAddress(factory string) string {
wsc := config.LoadWireguardServerConfig(factory, api)
if len(wsc.VpnAddress) == 0 || !wsc.Enabled {
fmt.Println("ERROR: A wireguard server has not been configured for this factory")
color.Red("ERROR: A wireguard server has not been configured for this factory")
os.Exit(1)
}
logrus.Debugf("VPN server address is: %s", wsc.VpnAddress)
serverIp, err := ipToUint32(wsc.VpnAddress)
if err != nil {
fmt.Println("ERROR: Wireguard server has an invalid IP Address: ", wsc.VpnAddress)
color.Red("ERROR: Wireguard server has an invalid IP Address: ", wsc.VpnAddress)
os.Exit(1)
}

Expand All @@ -123,7 +125,7 @@ func findVpnAddress(factory string) string {
}
}

fmt.Println("ERROR: Unable to find unique IP address for VPN")
color.Red("ERROR: Unable to find unique IP address for VPN")
os.Exit(1)
return ""
}
Expand All @@ -144,7 +146,7 @@ func doConfigWireguard(cmd *cobra.Command, args []string) {
}
os.Exit(0)
} else if args[1] != "enable" && args[1] != "disable" {
fmt.Printf("Invalid argument: '%s'. Must be 'enable' or 'disable'\n", args[1])
color.Red(fmt.Sprintf("Invalid argument: '%s'. Must be 'enable' or 'disable'\n", args[1]))
os.Exit(0)
}

Expand All @@ -161,7 +163,7 @@ func doConfigWireguard(cmd *cobra.Command, args []string) {

if args[1] == "enable" {
if len(wcc.PublicKey) == 0 {
fmt.Println("ERROR: Device has no public key for VPN")
color.Red("ERROR: Device has no public key for VPN")
os.Exit(1)
}
wcc.Enabled = true
Expand Down
4 changes: 3 additions & 1 deletion subcommands/devices/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -25,7 +27,7 @@ func doDelete(cmd *cobra.Command, args []string) {
for _, name := range args {
fmt.Printf("Deleting %s .. ", name)
if err := api.DeviceDelete(factory, name); err != nil {
fmt.Printf("failed\n%s", err)
color.Red(fmt.Sprintf("failed\n%s", err))
os.Exit(1)
} else {
fmt.Printf("ok\n")
Expand Down
6 changes: 4 additions & 2 deletions subcommands/devices/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"strings"

"github.com/fatih/color"

"github.com/cheynewallace/tabby"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,7 +56,7 @@ func ownerFormatter(d *client.Device) string {
logrus.Debugf("Looking up user %s in factory %s", d.Owner, d.Factory)
users, err := api.UsersList(d.Factory)
if err != nil {
logrus.Errorf("Unable to look up users: %s", err)
color.Red(fmt.Sprintf("Unable to look up users: %s", err))
return "???"
}
id := "<not in factory: " + d.Factory + ">"
Expand Down Expand Up @@ -185,7 +187,7 @@ func showDeviceList(dl *client.DeviceList, showColumns []string) {
var cols = make([]interface{}, len(showColumns))
for idx, c := range showColumns {
if _, ok := Columns[c]; !ok {
fmt.Println("ERROR: Invalid column name:", c)
color.Red("ERROR: Invalid column name:", c)
os.Exit(1)
}
cols[idx] = strings.ToUpper(c)
Expand Down
4 changes: 3 additions & 1 deletion subcommands/devices/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -23,7 +25,7 @@ func doRename(cmd *cobra.Command, args []string) {
logrus.Debugf("Renaming %s -> %s", args[0], args[1])

if err := api.DeviceRename(factory, args[0], args[1]); err != nil {
fmt.Printf("failed\n%s", err)
color.Red(fmt.Sprintf("failed\n%s", err))
os.Exit(1)
}
}
3 changes: 3 additions & 0 deletions subcommands/docker/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"runtime"

"github.com/fatih/color"

"github.com/foundriesio/fioctl/subcommands"
"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -129,6 +131,7 @@ func RunCredsHelper() int {
if subcommands.Config.ClientCredentials.ClientSecret == "" {
msg := "ERROR: Your fioctl configuration does not appear to include oauth2 credentials. Please run `fioctl login` to configure and then try again.\n"
os.Stderr.WriteString(msg)
color.Red(msg)
os.Exit(1)
}
subcommands.Login(NewCommand()) // Ensure a fresh oauth2 access token
Expand Down
3 changes: 3 additions & 0 deletions subcommands/git/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"runtime"
"strings"

"github.com/fatih/color"

"github.com/foundriesio/fioctl/subcommands"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -112,6 +114,7 @@ func RunCredsHelper() int {
if subcommands.Config.ClientCredentials.ClientSecret == "" {
msg := "ERROR: Your fioctl configuration does not appear to include oauth2 credentials. Please run `fioctl login` to configure and then try again.\n"
os.Stderr.WriteString(msg)
color.Red(msg)
os.Exit(1)
}
subcommands.Login(NewCommand()) // Ensure a fresh oauth2 access tokenA
Expand Down
6 changes: 4 additions & 2 deletions subcommands/keys/ca_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -65,7 +67,7 @@ This is optional.`,

func writeFile(filename, contents string, mode os.FileMode) {
if err := os.WriteFile(filename, []byte(contents), mode); err != nil {
fmt.Printf("ERROR: Creating %s: %s", filename, err)
color.Red("ERROR: Creating %s: %s", filename, err)
os.Exit(1)
}
}
Expand All @@ -84,7 +86,7 @@ func doCreateCA(cmd *cobra.Command, args []string) {

if len(hsmModule) > 0 {
if len(hsmPin) == 0 {
fmt.Println("ERROR: --hsm-pin is required with --hsm-module")
color.Red("ERROR: --hsm-pin is required with --hsm-module")
os.Exit(1)
}
os.Setenv("HSM_MODULE", hsmModule)
Expand Down
6 changes: 4 additions & 2 deletions subcommands/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -69,7 +71,7 @@ func doLogin(cmd *cobra.Command, args []string) {
}

if creds.Config.ClientId == "" || creds.Config.ClientSecret == "" {
fmt.Println("Cannot execute login without client ID or client secret.")
color.Red("Cannot execute login without client ID or client secret.")
os.Exit(1)
}

Expand Down Expand Up @@ -106,7 +108,7 @@ func promptForCreds(credsUrl string) (string, string) {
clientSecret := strings.Trim(scanner.Text(), " ")

if clientId == "" || clientSecret == "" {
fmt.Println("Client ID and client credentials are both required.")
color.Red("ERROR: Client ID and client credentials are both required.")
os.Exit(1)
}

Expand Down
5 changes: 3 additions & 2 deletions subcommands/secrets/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package secrets

import (
"fmt"
"os"

"github.com/fatih/color"

"github.com/cheynewallace/tabby"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,7 +35,7 @@ func doList(cmd *cobra.Command, args []string) {
t.AddLine(secret.Name)
}
} else if len(triggers) != 0 {
fmt.Println("ERROR: Factory configuration issue. Factory has unexpected number of triggers.")
color.Red("ERROR: Factory configuration issue. Factory has unexpected number of triggers.")
os.Exit(1)
}
t.Print()
Expand Down
7 changes: 4 additions & 3 deletions subcommands/secrets/update.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package secrets

import (
"fmt"
"os"
"strings"

"github.com/fatih/color"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -42,7 +43,7 @@ func doUpdate(cmd *cobra.Command, args []string) {
for i, arg := range args {
parts := strings.SplitN(arg, "=", 2)
if len(parts) != 2 {
fmt.Println("ERROR: Invalid key=value argument: ", arg)
color.Red("ERROR: Invalid key=value argument: ", arg)
os.Exit(1)
}
secrets[i].Name = parts[0]
Expand All @@ -67,7 +68,7 @@ func doUpdate(cmd *cobra.Command, args []string) {
} else if len(triggers) == 1 {
pt = triggers[0]
} else {
fmt.Println("ERROR: Factory configuration issue. Factory has unexpected number of triggers.")
color.Red("ERROR: Factory configuration issue. Factory has unexpected number of triggers.")
os.Exit(1)
}

Expand Down
Loading

0 comments on commit b775791

Please sign in to comment.