Skip to content

Commit

Permalink
Add support for piped input to rules (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Jul 27, 2021
1 parent 711dbbd commit b303da9
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 162 deletions.
39 changes: 11 additions & 28 deletions internal/ansi/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ansi

import (
"fmt"
"io"
"os"

"github.com/auth0/auth0-cli/internal/iostream"
"github.com/logrusorgru/aurora"
"github.com/mattn/go-isatty"
"github.com/tidwall/pretty"
)

Expand All @@ -29,22 +28,23 @@ var DisableColors = false
// `CLICOLOR_FORCE`. Cf. https://bixense.com/clicolors/
var EnvironmentOverrideColors = true

var color = Color()

// Bold returns bolded text if the writer supports colors
func Bold(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Bold(text))
}

// Color returns an aurora.Aurora instance with colors enabled or disabled
// depending on whether the writer supports colors.
func Color(w io.Writer) aurora.Aurora {
return aurora.NewAurora(shouldUseColors(w))
func Color() aurora.Aurora {
return aurora.NewAurora(shouldUseColors())
}

// ColorizeJSON returns a colorized version of the input JSON, if the writer
// supports colors.
func ColorizeJSON(json string, darkStyle bool, w io.Writer) string {
if !shouldUseColors(w) {
func ColorizeJSON(json string, darkStyle bool) string {
if !shouldUseColors() {
return json
}

Expand All @@ -58,8 +58,6 @@ func ColorizeJSON(json string, darkStyle bool, w io.Writer) string {

// ColorizeStatus returns a colorized number for HTTP status code
func ColorizeStatus(status int) aurora.Value {
color := Color(os.Stdout)

switch {
case status >= 500:
return color.Red(status).Bold()
Expand All @@ -72,68 +70,58 @@ func ColorizeStatus(status int) aurora.Value {

// Faint returns slightly offset color text if the writer supports it
func Faint(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Faint(text))
}

// Italic returns italicized text if the writer supports it.
func Italic(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Italic(text))
}

// Red returns text colored red
func Red(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Red(text))
}

// BrightRed returns text colored bright red
func BrightRed(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.BrightRed(text))
}

// Green returns text colored green
func Green(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Green(text))
}

// Yellow returns text colored yellow
func Yellow(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Yellow(text))
}

// BrightYellow returns text colored bright yellow
func BrightYellow(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.BrightYellow(text))
}

// Blue returns text colored blue
func Blue(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Blue(text))
}

// Magenta returns text colored magenta
func Magenta(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.Magenta(text))
}

// Cyan returns text colored cyan
func Cyan(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.BrightCyan(text))
}

// Linkify returns an ANSI escape sequence with an hyperlink, if the writer
// supports colors.
func Linkify(text, url string, w io.Writer) string {
if !shouldUseColors(w) {
func Linkify(text, url string) string {
if !shouldUseColors() {
return text
}

Expand All @@ -144,16 +132,11 @@ func Linkify(text, url string, w io.Writer) string {

// StrikeThrough returns struck though text if the writer supports colors
func StrikeThrough(text string) string {
color := Color(os.Stdout)
return color.Sprintf(color.StrikeThrough(text))
}

func IsTerminal() bool {
return isatty.IsTerminal(os.Stdout.Fd())
}

func shouldUseColors(w io.Writer) bool {
useColors := ForceColors || IsTerminal()
func shouldUseColors() bool {
useColors := ForceColors || iostream.IsOutputTerminal()

if EnvironmentOverrideColors {
force, ok := os.LookupEnv("CLICOLOR_FORCE")
Expand Down
6 changes: 3 additions & 3 deletions internal/ansi/spinner.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ansi

import (
"os"
"time"

"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/iostream"
"github.com/briandowns/spinner"
)

Expand Down Expand Up @@ -34,11 +34,11 @@ func loading(initialMsg, doneMsg, failMsg string, fn func() error) error {
go func() {
defer close(done)

s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(iostream.Messages))
s.Prefix = initialMsg
s.FinalMSG = doneMsg
s.HideCursor = true
s.Writer = os.Stderr
s.Writer = iostream.Messages

if err := s.Color(spinnerColor); err != nil {
panic(auth0.Error(err, "failed setting spinner color"))
Expand Down
10 changes: 5 additions & 5 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"time"

"github.com/auth0/auth0-cli/internal/analytics"
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/buildinfo"
"github.com/auth0/auth0-cli/internal/display"
"github.com/auth0/auth0-cli/internal/iostream"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/jwt"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -516,11 +516,11 @@ func canPrompt(cmd *cobra.Command) bool {
return false
}

return ansi.IsTerminal() && !noInput
return iostream.IsInputTerminal() && iostream.IsOutputTerminal() && !noInput
}

func shouldPrompt(cmd *cobra.Command, flag string) bool {
return canPrompt(cmd) && !cmd.Flags().Changed(flag)
func shouldPrompt(cmd *cobra.Command, flag *Flag) bool {
return canPrompt(cmd) && !flag.IsSet(cmd)
}

func shouldPromptWhenFlagless(cmd *cobra.Command, flag string) bool {
Expand All @@ -536,7 +536,7 @@ func shouldPromptWhenFlagless(cmd *cobra.Command, flag string) bool {
}

func prepareInteractivity(cmd *cobra.Command) {
if canPrompt(cmd) {
if canPrompt(cmd) || !iostream.IsInputTerminal() {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
_ = cmd.Flags().SetAnnotation(flag.Name, cobra.BashCompOneRequiredFlag, []string{"false"})
})
Expand Down
11 changes: 5 additions & 6 deletions internal/cli/completion.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cli

import (
"os"

"github.com/auth0/auth0-cli/internal/iostream"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -57,22 +56,22 @@ PS> auth0 completion powershell > auth0.ps1
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
err := cmd.Root().GenBashCompletion(os.Stdout)
err := cmd.Root().GenBashCompletion(iostream.Output)
if err != nil {
cli.renderer.Errorf("An unexpected error occurred while setting up completion: %v", err.Error())
}
case "zsh":
err := cmd.Root().GenZshCompletion(os.Stdout)
err := cmd.Root().GenZshCompletion(iostream.Output)
if err != nil {
cli.renderer.Errorf("An unexpected error occurred while setting up completion: %v", err.Error())
}
case "fish":
err := cmd.Root().GenFishCompletion(os.Stdout, true)
err := cmd.Root().GenFishCompletion(iostream.Output, true)
if err != nil {
cli.renderer.Errorf("An unexpected error occurred while setting up completion: %v", err.Error())
}
case "powershell":
err := cmd.Root().GenPowerShellCompletion(os.Stdout)
err := cmd.Root().GenPowerShellCompletion(iostream.Output)
if err != nil {
cli.renderer.Errorf("An unexpected error occurred while setting up completion: %v", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func shouldAsk(cmd *cobra.Command, f *Flag, isUpdate bool) bool {
return shouldPromptWhenFlagless(cmd, f.LongForm)
}

return shouldPrompt(cmd, f.LongForm)
return shouldPrompt(cmd, f)
}

func markFlagRequired(cmd *cobra.Command, f *Flag, isUpdate bool) error {
Expand Down
Loading

0 comments on commit b303da9

Please sign in to comment.