Skip to content

Commit

Permalink
refactor: Implement graceful shutdown and improve console output hand…
Browse files Browse the repository at this point in the history
…ling
  • Loading branch information
yarlson committed Dec 1, 2024
1 parent ea44cd7 commit b15c5ef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ package main

import (
"fmt"
"github.com/yarlson/ftl/pkg/console"
"os"
"os/signal"
"syscall"

"github.com/yarlson/ftl/cmd"
)

func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
console.Reset()
os.Exit(1)
}()

defer console.Reset()

if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
20 changes: 18 additions & 2 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ import (
"golang.org/x/term"
)

const (
var (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
showCursor = "\033[?25h"
)

func init() {
if _, exists := os.LookupEnv("NO_COLOR"); exists {
colorReset = ""
colorRed = ""
colorGreen = ""
colorYellow = ""
showCursor = ""
}
}

// Info prints an information message.
func Info(a ...interface{}) {
message := fmt.Sprint(a...)
Expand Down Expand Up @@ -58,7 +69,6 @@ func ReadLine() (string, error) {

// ReadPassword reads a password from standard input without echoing.
func ReadPassword() (string, error) {
Input("")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
Expand All @@ -71,3 +81,9 @@ func ReadPassword() (string, error) {
func Print(a ...interface{}) {
fmt.Println(a...)
}

// Reset ensures the cursor is visible and terminal is in a normal state.
func Reset() {
fmt.Print(showCursor)
_ = os.Stdout.Sync()
}
2 changes: 0 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func setupServer(ctx context.Context, cfg config.Server, dockerCreds DockerCrede
runner := remote.NewRunner(sshClient)
cfg.RootSSHKey = string(rootKey)

console.Print("Setting up server...")

spinner = sm.AddSpinner("software", fmt.Sprintf("[%s] Installing software", cfg.Host))
if err := installSoftware(ctx, runner); err != nil {
spinner.ErrorWithMessagef("Failed to install software: %v", err)
Expand Down

0 comments on commit b15c5ef

Please sign in to comment.