cli: Use glint to determine if os.Stdout is tty (#10926)

Use glint to determine if os.Stdout is a terminal.

glint Terminal renderer expects os.Stdout [not only to be a terminal, but also to have non-zero size](b492b545f6/renderer_term.go (L39-L46)). It's unclear how this condition arises, but this additional check causes Nomad to render deployments progress through glint when glint cannot support it.

By using golint to perform the check, we eliminate the risk of mis-judgement.
This commit is contained in:
Mahmood Ali
2021-07-23 11:27:47 -04:00
committed by GitHub
parent 36d2f9f992
commit 44ea61ef3a
4 changed files with 23 additions and 7 deletions

View File

@@ -182,15 +182,28 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
}
func (c *DeploymentStatusCommand) monitor(client *api.Client, deployID string, index uint64, verbose bool) {
_, isStdoutTerminal := term.GetFdInfo(os.Stdout)
// TODO if/when glint offers full Windows support take out the runtime check
if isStdoutTerminal && runtime.GOOS != "windows" {
if isStdoutTerminal() {
c.ttyMonitor(client, deployID, index, verbose)
} else {
c.defaultMonitor(client, deployID, index, verbose)
}
}
func isStdoutTerminal() bool {
// TODO if/when glint offers full Windows support take out the runtime check
if runtime.GOOS == "windows" {
return false
}
// glint checks if the writer is a tty with additional
// checks (e.g. terminal has non-0 size)
r := &glint.TerminalRenderer{
Output: os.Stdout,
}
return r.LayoutRoot() != nil
}
// Uses glint for printing in place. Same logic as the defaultMonitor function
// but only used for tty and non-Windows machines since glint doesn't work with
// cmd/PowerShell and non-interactive interfaces
@@ -214,9 +227,9 @@ func (c *DeploymentStatusCommand) ttyMonitor(client *api.Client, deployID string
d.Set(spinner)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go d.Render(ctx)
defer cancel()
q := api.QueryOptions{
AllowStale: true,