Skip to content

Commit 485e1f3

Browse files
Merge pull request #339 from manifoldco/print-only-attached-to-terminal
Only print to stdout if attached to terminal
2 parents 07b1080 + 009ff71 commit 485e1f3

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# CHANGELOG
22

3+
## v0.28.1
4+
5+
**Fixes**
6+
7+
- Torus will only print out secondary information such as when it's attempting
8+
to authenticate using credentials from `TORUS_EMAIL`, `TORUS_PASSWORD`,
9+
`TORUS_TOKEN_ID`, and `TORUS_TOKEN_SECRET` if stdout is attached to a
10+
terminal window.
11+
312
## v0.28.0
413

514
_2017-12-05_

cmd/middleware.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"reflect"
87
"strings"
@@ -17,6 +16,7 @@ import (
1716
"github.com/manifoldco/torus-cli/dirprefs"
1817
"github.com/manifoldco/torus-cli/errs"
1918
"github.com/manifoldco/torus-cli/prefs"
19+
"github.com/manifoldco/torus-cli/ui"
2020
)
2121

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

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

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

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

139138
err := client.Session.UserLogin(bgCtx, email, password)
140139
if err != nil {
141-
fmt.Println("Could not log in.\n" + err.Error())
140+
ui.Error("Could not log in, encountered an error: %s", err)
142141
} else {
143142
return nil
144143
}
145144
}
146145

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

150149
err := client.Session.MachineLogin(bgCtx, tokenID, tokenSecret)
151150
if err != nil {
152-
fmt.Println("Could not log in\n" + err.Error())
151+
ui.Error("Could not log in, encountered an error: %s", err)
153152
} else {
154153
return nil
155154
}
156155
}
157156

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

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

357356
return nil

ui/ui.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ui
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"os"
78

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

108+
// Info calls Info on the default UI
109+
func Info(format string, args ...interface{}) { defUI.Info(format, args...) }
110+
111+
// Info handles outputting secondary information to the user such as messages
112+
// about progress but are the actual result of an operation. For example,
113+
// printing out that we're attempting to log a user in using the specific
114+
// environment variables.
115+
//
116+
// Only printed if stdout is attached to a terminal.
117+
func (u *UI) Info(format string, args ...interface{}) {
118+
if !readline.IsTerminal(int(os.Stdout.Fd())) {
119+
return
120+
}
121+
122+
u.Line(format, args...)
123+
}
124+
125+
// Warn calls Warn on the default UI
126+
func Warn(format string, args ...interface{}) { defUI.Warn(format, args...) }
127+
128+
// Warn handles outputting warning information to the user such as
129+
// messages about needing to be logged in.
130+
//
131+
// The warning is printed out to stderr if stdout is not attached to a
132+
// terminal.
133+
func (u *UI) Warn(format string, args ...interface{}) {
134+
var w io.Writer = readline.Stdout
135+
if !readline.IsTerminal(int(os.Stdout.Fd())) {
136+
w = readline.Stderr
137+
}
138+
139+
o := fmt.Sprintf(format, args...)
140+
fmt.Fprintln(w, ansiwrap.WrapIndent(o, u.Cols, u.Indent, u.Indent))
141+
}
142+
143+
// Error calls Error on the default UI
144+
func Error(format string, args ...interface{}) { defUI.Error(format, args...) }
145+
146+
// Error handles outputting error information to the user such as the fact they
147+
// couldn't log in due to an error.
148+
//
149+
// The error is printed out to stderr if stdout is not attached to a termainl
150+
func (u *UI) Error(format string, args ...interface{}) {
151+
var w io.Writer = readline.Stdout
152+
if !readline.IsTerminal(int(os.Stdout.Fd())) {
153+
w = readline.Stderr
154+
}
155+
156+
o := fmt.Sprintf(format, args...)
157+
fmt.Fprintln(w, ansiwrap.WrapIndent(o, u.Cols, u.Indent, u.Indent))
158+
}
159+
107160
// Child calls Child on the default UI
108161
func Child(indent int) *UI { return defUI.Child(indent) }
109162

0 commit comments

Comments
 (0)