Skip to content

Commit

Permalink
Merge pull request #339 from manifoldco/print-only-attached-to-terminal
Browse files Browse the repository at this point in the history
Only print to stdout if attached to terminal
  • Loading branch information
ianlivingstone committed Dec 17, 2017
2 parents 07b1080 + 009ff71 commit 485e1f3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# CHANGELOG

## v0.28.1

**Fixes**

- Torus will only print out secondary information such as when it's attempting
to authenticate using credentials from `TORUS_EMAIL`, `TORUS_PASSWORD`,
`TORUS_TOKEN_ID`, and `TORUS_TOKEN_SECRET` if stdout is attached to a
terminal window.

## v0.28.0

_2017-12-05_
Expand Down
17 changes: 8 additions & 9 deletions cmd/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"
"os"
"reflect"
"strings"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/manifoldco/torus-cli/dirprefs"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/prefs"
"github.com/manifoldco/torus-cli/ui"
)

const downloadURL = "https://www.torus.sh/install"
Expand Down Expand Up @@ -87,8 +87,7 @@ func ensureDaemon(ctx *cli.Context) error {
return errs.NewExitError("The daemon version is incorrect. Check for stale processes.")
}

fmt.Println("The daemon version is out of date and is being restarted.")
fmt.Println("You will need to login again.")
ui.Warn("The daemon version is out of date and is being restarted. You will need to login again.")

_, err = stopDaemon(proc)
if err != nil {
Expand Down Expand Up @@ -134,28 +133,28 @@ func ensureSession(ctx *cli.Context) error {
tokenSecret, hasTokenSecret := os.LookupEnv("TORUS_TOKEN_SECRET")

if hasEmail && hasPassword {
fmt.Println("Attempting to login with email: " + email)
ui.Info("Attempting to login as a user with email: %s", email)

err := client.Session.UserLogin(bgCtx, email, password)
if err != nil {
fmt.Println("Could not log in.\n" + err.Error())
ui.Error("Could not log in, encountered an error: %s", err)
} else {
return nil
}
}

if hasTokenID && hasTokenSecret {
fmt.Println("Attempting to login with token id: " + tokenID)
ui.Info("Attempting to login with machine token id: %s", tokenID)

err := client.Session.MachineLogin(bgCtx, tokenID, tokenSecret)
if err != nil {
fmt.Println("Could not log in\n" + err.Error())
ui.Error("Could not log in, encountered an error: %s", err)
} else {
return nil
}
}

msg := "You must be logged in to run '" + ctx.Command.FullName() + "'.\n" +
msg := "\nYou must be logged in to run '" + ctx.Command.FullName() + "'.\n" +
"Login using 'login' or create an account using 'signup'."
return errs.NewExitError(msg)
}
Expand Down Expand Up @@ -351,7 +350,7 @@ func checkUpdates(ctx *cli.Context) error {
}

if updateInfo.NeedsUpdate {
fmt.Printf("New version %s available! Visit %s to download\n", updateInfo.Version, downloadURL)
ui.Info("A new version of Torus is available (%s)! You can download it from %s.", updateInfo.Version, downloadURL)
}

return nil
Expand Down
53 changes: 53 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"bytes"
"fmt"
"io"
"os"

"github.com/chzyer/readline"
Expand Down Expand Up @@ -104,6 +105,58 @@ func (u *UI) Hint(str string, noPadding bool, label *string) {
fmt.Fprintln(readline.Stdout, ansiwrap.WrapIndent(hintLabel+str, u.Cols, u.Indent, u.Indent+rc))
}

// Info calls Info on the default UI
func Info(format string, args ...interface{}) { defUI.Info(format, args...) }

// Info handles outputting secondary information to the user such as messages
// about progress but are the actual result of an operation. For example,
// printing out that we're attempting to log a user in using the specific
// environment variables.
//
// Only printed if stdout is attached to a terminal.
func (u *UI) Info(format string, args ...interface{}) {
if !readline.IsTerminal(int(os.Stdout.Fd())) {
return
}

u.Line(format, args...)
}

// Warn calls Warn on the default UI
func Warn(format string, args ...interface{}) { defUI.Warn(format, args...) }

// Warn handles outputting warning information to the user such as
// messages about needing to be logged in.
//
// The warning is printed out to stderr if stdout is not attached to a
// terminal.
func (u *UI) Warn(format string, args ...interface{}) {
var w io.Writer = readline.Stdout
if !readline.IsTerminal(int(os.Stdout.Fd())) {
w = readline.Stderr
}

o := fmt.Sprintf(format, args...)
fmt.Fprintln(w, ansiwrap.WrapIndent(o, u.Cols, u.Indent, u.Indent))
}

// Error calls Error on the default UI
func Error(format string, args ...interface{}) { defUI.Error(format, args...) }

// Error handles outputting error information to the user such as the fact they
// couldn't log in due to an error.
//
// The error is printed out to stderr if stdout is not attached to a termainl
func (u *UI) Error(format string, args ...interface{}) {
var w io.Writer = readline.Stdout
if !readline.IsTerminal(int(os.Stdout.Fd())) {
w = readline.Stderr
}

o := fmt.Sprintf(format, args...)
fmt.Fprintln(w, ansiwrap.WrapIndent(o, u.Cols, u.Indent, u.Indent))
}

// Child calls Child on the default UI
func Child(indent int) *UI { return defUI.Child(indent) }

Expand Down

0 comments on commit 485e1f3

Please sign in to comment.