Skip to content

Commit

Permalink
updates based on review from @lgfa29
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Mar 28, 2023
1 parent 2f10f25 commit ddab4e6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 59 deletions.
6 changes: 1 addition & 5 deletions command/alloc_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/hashicorp/nomad/helper/cli"
"github.com/hashicorp/nomad/command/ui"
"github.com/posener/complete"
)

Expand Down Expand Up @@ -291,10 +291,6 @@ func (l *AllocLogsCommand) handleSingleFile(client *api.Client, alloc *api.Alloc
}
}

if readErr != nil {
return readErr
}

defer r.Close()
if _, err := io.Copy(os.Stdout, r); err != nil {
return fmt.Errorf("error following logs: %s", err)
Expand Down
63 changes: 9 additions & 54 deletions helper/cli/ui.go → command/ui/ui.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package cli

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"

"github.com/bgentry/speakeasy"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/mitchellh/cli"
"io"
)

// LogUI is an implementation of the cli.Ui interface which can be used for
Expand All @@ -23,6 +16,10 @@ type LogUI struct {
writer io.Writer
errorWriter io.Writer

// underlyingUI stores the basic UI that was used to create this logUI. It
// allows us to call the ask functions and not implement them again.
underlyingUI cli.Ui

isColor bool
outputColor cli.UiColor
infoColor cli.UiColor
Expand All @@ -49,6 +46,7 @@ func NewLogUI(ui cli.Ui) (cli.Ui, error) {
logUI.infoColor = coloredUI.InfoColor
logUI.errorColor = coloredUI.ErrorColor
logUI.warnColor = coloredUI.WarnColor
logUI.underlyingUI = coloredUI.Ui

if basicUI, ok := coloredUI.Ui.(*cli.BasicUi); ok {
logUI.reader = basicUI.Reader
Expand All @@ -60,6 +58,7 @@ func NewLogUI(ui cli.Ui) (cli.Ui, error) {
logUI.reader = basicUI.Reader
logUI.writer = basicUI.Writer
logUI.errorWriter = basicUI.ErrorWriter
logUI.underlyingUI = basicUI
found = true
}

Expand All @@ -72,56 +71,12 @@ func NewLogUI(ui cli.Ui) (cli.Ui, error) {

// Ask implements the Ask function of the cli.Ui interface.
func (l *LogUI) Ask(query string) (string, error) {
return l.ask(l.colorize(query, l.outputColor), false)
return l.underlyingUI.Ask(l.colorize(query, l.outputColor))
}

// AskSecret implements the AskSecret function of the cli.Ui interface.
func (l *LogUI) AskSecret(query string) (string, error) {
return l.ask(l.colorize(query, l.outputColor), true)
}

func (l *LogUI) ask(query string, secret bool) (string, error) {
if _, err := fmt.Fprint(l.writer, query+" "); err != nil {
return "", err
}

// Register for interrupts so that we can catch it and immediately
// return...
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
defer signal.Stop(sigCh)

// Ask for input in a go-routine so that we can ignore it.
errCh := make(chan error, 1)
lineCh := make(chan string, 1)
go func() {
var line string
var err error
if secret && isatty.IsTerminal(os.Stdin.Fd()) {
line, err = speakeasy.Ask("")
} else {
r := bufio.NewReader(l.reader)
line, err = r.ReadString('\n')
}
if err != nil {
errCh <- err
return
}

lineCh <- strings.TrimRight(line, "\r\n")
}()

select {
case err := <-errCh:
return "", err
case line := <-lineCh:
return line, nil
case <-sigCh:
// Print a newline so that any further output starts properly
// on a new line.
_, _ = fmt.Fprintln(l.writer)
return "", errors.New("interrupted")
}
return l.underlyingUI.AskSecret(l.colorize(query, l.outputColor))
}

// Output implements the Output function of the cli.Ui interface.
Expand Down
File renamed without changes.

0 comments on commit ddab4e6

Please sign in to comment.