Skip to content

Commit

Permalink
refactor: Improve error handling and console output in cmd package
Browse files Browse the repository at this point in the history
  • Loading branch information
yarlson committed Nov 5, 2024
1 parent 485efe1 commit fe603c2
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 86 deletions.
6 changes: 3 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func init() {
func runBuild(cmd *cobra.Command, args []string) {
cfg, err := parseConfig("ftl.yaml")
if err != nil {
console.ErrPrintln("Failed to parse config file:", err)
console.Error("Failed to parse config file:", err)
return
}

skipPush, err := cmd.Flags().GetBool("skip-push")
if err != nil {
console.ErrPrintln("Failed to get skip-push flag:", err)
console.Error("Failed to get skip-push flag:", err)
return
}

Expand All @@ -46,7 +46,7 @@ func runBuild(cmd *cobra.Command, args []string) {
ctx := context.Background()

if err := buildAndPushServices(ctx, cfg.Services, builder, skipPush); err != nil {
console.ErrPrintln("Build process failed:", err)
console.Error("Build process failed:", err)
return
}
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cmd
import (
"context"
"fmt"
"github.com/yarlson/ftl/pkg/ssh"
"os"
"path/filepath"

"github.com/yarlson/ftl/pkg/ssh"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

Expand All @@ -33,12 +34,12 @@ func init() {
func runDeploy(cmd *cobra.Command, args []string) {
cfg, err := parseConfig("ftl.yaml")
if err != nil {
console.ErrPrintln("Failed to parse config file:", err)
console.Error("Failed to parse config file:", err)
return
}

if err := deployToServers(cfg); err != nil {
console.ErrPrintln("Deployment failed:", err)
console.Error("Deployment failed:", err)
return
}
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func deployToServer(project string, cfg *config.Config, server config.Server) er
if spinner != nil {
spinner.Fail(fmt.Sprintf("Deployment error: %s", event.Message))
} else {
console.ErrPrintln(fmt.Sprintf("Deployment error: %s", event.Message))
console.Error(fmt.Sprintf("Deployment error: %s", event.Message))
}
return fmt.Errorf("deployment error: %s", event.Message)
case deployment.EventTypeComplete:
Expand Down
10 changes: 5 additions & 5 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ func init() {
func runSetup(cmd *cobra.Command, args []string) {
cfg, err := parseConfig("ftl.yaml")
if err != nil {
console.ErrPrintln("Failed to parse config file:", err)
console.Error("Failed to parse config file:", err)
return
}

dockerCreds, err := getDockerCredentials(cfg.Services)
if err != nil {
console.ErrPrintln("Failed to get Docker credentials:", err)
console.Error("Failed to get Docker credentials:", err)
return
}

newUserPassword, err := getUserPassword()
if err != nil {
console.ErrPrintln("Failed to read password:", err)
console.Error("Failed to read password:", err)
return
}

if dockerCreds.Username != "" && dockerCreds.Password != "" {
if err := server.DockerLogin(context.Background(), dockerCreds.Username, dockerCreds.Password); err != nil {
console.ErrPrintln("Failed to login to Docker Hub:", err)
console.Error("Failed to login to Docker Hub:", err)
return
}
}

for _, s := range cfg.Servers {
if err := setupServer(s, dockerCreds, newUserPassword); err != nil {
console.ErrPrintln(fmt.Sprintf("Failed to setup server %s:", s.Host), err)
console.Error(fmt.Sprintf("Failed to setup server %s:", s.Host), err)
continue
}
console.Success(fmt.Sprintf("Successfully set up server %s", s.Host))
Expand Down
116 changes: 57 additions & 59 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,60 @@ import (
"golang.org/x/term"
)

var (
Info = (&pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.InfoMessageStyle,
Text: " ",
},
}).Println
// Info prints an information message.
func Info(a ...interface{}) {
infoPrinter.Println(a...)
}

Success = (&pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.SuccessMessageStyle,
Text: "",
},
}).Println
var infoPrinter = &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.InfoMessageStyle,
Text: " ",
},
}

Warning = (&pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.WarningMessageStyle,
Text: "!",
},
}).Println
// Success prints a success message.
func Success(a ...interface{}) {
successPrinter.Println(a...)
}

ErrPrintln = (&pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.ErrorMessageStyle,
Text: "",
},
}).Println
var successPrinter = &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.SuccessMessageStyle,
Text: "",
},
}

ErrPrintf = (&pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.ErrorMessageStyle,
Text: "✘",
},
}).Printf
// Warning prints a warning message.
func Warning(a ...interface{}) {
warningPrinter.Println(a...)
}

Input = pterm.FgYellow.Print
)
var warningPrinter = &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.WarningMessageStyle,
Text: "!",
},
}

// Error prints an error message with a newline.
func Error(a ...interface{}) {
errorPrinter.Println(a...)
}

var errorPrinter = &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.ErrorMessageStyle,
Text: "✘",
},
}

// Input prints an input prompt.
func Input(a ...interface{}) {
pterm.FgYellow.Print(a...)
}

// ReadLine reads a line from standard input.
func ReadLine() (string, error) {
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
Expand All @@ -58,11 +73,14 @@ func ReadLine() (string, error) {
return strings.TrimSpace(line), nil
}

// ReadPassword reads a password from standard input without echoing.
func ReadPassword() (string, error) {
Input("Password: ")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
Input("\n") // Move to the next line after input
return string(password), nil
}

Expand All @@ -75,32 +93,12 @@ func NewSpinner(initialText string) *pterm.SpinnerPrinter {
TimerRoundingFactor: time.Second,
TimerStyle: &pterm.ThemeDefault.TimerStyle,
MessageStyle: pterm.NewStyle(pterm.FgYellow),
InfoPrinter: &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.InfoMessageStyle,
Text: " ",
},
},
SuccessPrinter: &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.SuccessMessageStyle,
Text: "√",
},
},
FailPrinter: &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.ErrorMessageStyle,
Text: "✘",
},
},
WarningPrinter: &pterm.PrefixPrinter{
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.WarningMessageStyle,
Text: "!",
},
},
InfoPrinter: infoPrinter,
SuccessPrinter: successPrinter,
FailPrinter: errorPrinter,
WarningPrinter: warningPrinter,
}

sp, _ := spinner.Start(initialText)
return sp
spinnerPrinter, _ := spinner.Start(initialText)
return spinnerPrinter
}
5 changes: 3 additions & 2 deletions pkg/imagesync/imagesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package imagesync

import (
"context"
"github.com/yarlson/ftl/pkg/ssh"
"github.com/yarlson/ftl/tests"
"io"
"os"
"testing"

"github.com/yarlson/ftl/pkg/ssh"
"github.com/yarlson/ftl/tests"

"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/stretchr/testify/require"
Expand Down
5 changes: 3 additions & 2 deletions pkg/runner/remote/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"context"
"fmt"
"github.com/bramvdbogaerde/go-scp"
"golang.org/x/crypto/ssh"
"io"
"os"
"strings"

"github.com/bramvdbogaerde/go-scp"
"golang.org/x/crypto/ssh"
)

type Runner struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package server
import (
"context"
"fmt"
ssh2 "github.com/yarlson/ftl/pkg/ssh"
"os"
"path/filepath"
"strings"
"time"

ssh2 "github.com/yarlson/ftl/pkg/ssh"

"golang.org/x/crypto/ssh"

"github.com/yarlson/ftl/pkg/config"
Expand Down
3 changes: 2 additions & 1 deletion pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ssh

import (
"fmt"
"golang.org/x/crypto/ssh"
"os"
"path/filepath"
"strings"
"time"

"golang.org/x/crypto/ssh"
)

// NewSSHClientWithKey creates a new ssh.Client using a private key
Expand Down
3 changes: 2 additions & 1 deletion pkg/ssh/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ssh

import (
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFindSSHKey(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bytes"
"context"
"fmt"
"github.com/bramvdbogaerde/go-scp"
ssh2 "github.com/yarlson/ftl/pkg/ssh"
"golang.org/x/crypto/ssh"
"io"
"net"
"os"
"path/filepath"
"strings"

"github.com/bramvdbogaerde/go-scp"
ssh2 "github.com/yarlson/ftl/pkg/ssh"
"golang.org/x/crypto/ssh"
)

type Tunnel struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/tunnel/tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/rand"
"encoding/pem"
"fmt"
ssh2 "github.com/yarlson/ftl/pkg/ssh"
"io"
"net"
"net/http"
Expand All @@ -15,6 +14,8 @@ import (
"testing"
"time"

ssh2 "github.com/yarlson/ftl/pkg/ssh"

"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/stretchr/testify/assert"
Expand Down
9 changes: 5 additions & 4 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package tests
import (
"context"
"fmt"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"io"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
Expand Down

0 comments on commit fe603c2

Please sign in to comment.