Skip to content

Commit

Permalink
feat: Better support for non-interactive mode
Browse files Browse the repository at this point in the history
* When stdout is not TTY, then do not display spinner and status
  text and the output of rhc should not be colorful, because
  output is redirected to some file. When temporary status
  messages and escape sequences were added to log file, then
  viewing such file could be complicated.
* This commit closes #14 issue
  • Loading branch information
jirihnidek committed Oct 10, 2022
1 parent 58fb6c5 commit 3c76e58
Showing 1 changed file with 95 additions and 38 deletions.
133 changes: 95 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"golang.org/x/sys/unix"
"os"
"strings"
"text/tabwriter"
Expand All @@ -21,9 +22,15 @@ const redColor = "\u001B[31m"
const greenColor = "\u001B[32m"
const endColor = "\u001B[0m"

const successPrefix = greenColor + "●" + endColor
const failPrefix = redColor + "●" + endColor
const errorPrefix = redColor + "!" + endColor
// Colorful prefixes
const ttySuccessPrefix = greenColor + "●" + endColor
const ttyFailPrefix = redColor + "●" + endColor
const ttyErrorPrefix = redColor + "!" + endColor

// Black & white prefixes. Unicode characters
const bwSuccessPrefix = "✓"
const bwFailPrefix = "𐄂"
const bwErrorPrefix = "!"

func main() {
app := cli.NewApp()
Expand All @@ -42,6 +49,28 @@ func main() {
log.SetFlags(0)
log.SetPrefix("")

// Detect if the output goes to TTY or some file
var isTTY bool
_, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
if err != nil {
isTTY = false
} else {
isTTY = true
}

var successPrefix string
var failPrefix string
var errorPrefix string
if isTTY {
successPrefix = ttySuccessPrefix
failPrefix = ttyFailPrefix
errorPrefix = ttyErrorPrefix
} else {
successPrefix = bwSuccessPrefix
failPrefix = bwFailPrefix
errorPrefix = bwErrorPrefix
}

app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "generate-man-page",
Expand Down Expand Up @@ -128,47 +157,59 @@ func main() {
}
}

s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Connecting to Red Hat Subscription Management..."
s.Start()
var s *spinner.Spinner
if isTTY {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Connecting to Red Hat Subscription Management..."
s.Start()
}
var err error
if c.String("organization") != "" {
err = registerActivationKey(c.String("organization"), c.StringSlice("activation-key"), c.String("server"))
} else {
err = registerPassword(username, password, c.String("server"))
}
if err != nil {
if isTTY {
s.Stop()
}
if err != nil {
return cli.Exit(err, 1)
}
s.Stop()
fmt.Printf(successPrefix + " Connected to Red Hat Subscription Management\n")
} else {
fmt.Printf(successPrefix + " This system is already connected to Red Hat Subscription Management\n")
}
durations["rhsm"] = time.Since(start)

start = time.Now()
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)

s.Suffix = " Connecting to Red Hat Insights..."
s.Start()
if err := registerInsights(); err != nil {
var s *spinner.Spinner
if isTTY {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Connecting to Red Hat Insights..."
s.Start()
}
err = registerInsights()
if isTTY {
s.Stop()
}
if err != nil {
return cli.Exit(err, 1)
}
s.Stop()
fmt.Printf(successPrefix + " Connected to Red Hat Insights\n")
durations["insights"] = time.Since(start)

start = time.Now()
s.Suffix = fmt.Sprintf(" Activating the %v daemon", BrandName)
s.Start()
if err := activate(); err != nil {
if isTTY {
s.Suffix = fmt.Sprintf(" Activating the %v daemon", BrandName)
s.Start()
}
err = activate()
if isTTY {
s.Stop()
}
if err != nil {
return cli.Exit(err, 1)
}
s.Stop()
fmt.Printf(successPrefix+" Activated the %v daemon\n", BrandName)
durations[BrandName] = time.Since(start)

Expand Down Expand Up @@ -205,40 +246,52 @@ func main() {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)

start = time.Now()
s.Suffix = fmt.Sprintf(" Deactivating the %v daemon", BrandName)
s.Start()
if err := deactivate(); err != nil {
errorMessages[BrandName] = fmt.Errorf("cannot deactivate daemon: %w", err)
if isTTY {
s.Suffix = fmt.Sprintf(" Deactivating the %v daemon", BrandName)
s.Start()
}
err = deactivate()
if isTTY {
s.Stop()
}
if err != nil {
errorMessages[BrandName] = fmt.Errorf("cannot deactivate daemon: %w", err)
fmt.Printf(errorPrefix+" Cannot deactivate the %v daemon\n", BrandName)
} else {
s.Stop()
fmt.Printf(failPrefix+" Deactivated the %v daemon\n", BrandName)
}
durations[BrandName] = time.Since(start)

start = time.Now()
s.Suffix = " Disconnecting from Red Hat Insights..."
s.Start()
if err := unregisterInsights(); err != nil {
errorMessages["insights"] = fmt.Errorf("cannot disconnect from Red Hat Insights: %w", err)
if isTTY {
s.Suffix = " Disconnecting from Red Hat Insights..."
s.Start()
}
err = unregisterInsights()
if isTTY {
s.Stop()
}
if err != nil {
errorMessages["insights"] = fmt.Errorf("cannot disconnect from Red Hat Insights: %w", err)
fmt.Printf(errorPrefix + " Cannot disconnect from Red Hat Insights\n")
} else {
s.Stop()
fmt.Print(failPrefix + " Disconnected from Red Hat Insights\n")
}
durations["insights"] = time.Since(start)

start = time.Now()
s.Suffix = " Disconnecting from Red Hat Subscription Management..."
s.Start()
if err := unregister(); err != nil {
errorMessages["rhsm"] = fmt.Errorf("cannot disconnect from Red Hat Subscription Management: %w", err)
if isTTY {
s.Suffix = " Disconnecting from Red Hat Subscription Management..."
s.Start()
}
err = unregister()
if isTTY {
s.Stop()
}
if err != nil {
errorMessages["rhsm"] = fmt.Errorf("cannot disconnect from Red Hat Subscription Management: %w", err)
fmt.Printf(errorPrefix + " Cannot disconnect from Red Hat Subscription Management\n")
} else {
s.Stop()
fmt.Printf(failPrefix + " Disconnected from Red Hat Subscription Management\n")
}
durations["rhsm"] = time.Since(start)
Expand Down Expand Up @@ -312,12 +365,16 @@ func main() {
fmt.Printf(successPrefix + " Connected to Red Hat Subscription Management\n")
}

s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)

s.Suffix = " Checking Red Hat Insights..."
s.Start()
var s *spinner.Spinner
if isTTY {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Checking Red Hat Insights..."
s.Start()
}
isRegistered, err := insightsIsRegistered()
s.Stop()
if isTTY {
s.Stop()
}

if isRegistered {
fmt.Print(successPrefix + " Connected to Red Hat Insights\n")
Expand Down

0 comments on commit 3c76e58

Please sign in to comment.