Skip to content

Commit

Permalink
Add a --quiet (-q) flag to silence stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Oct 1, 2024
1 parent 7779574 commit 7affd40
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
15 changes: 8 additions & 7 deletions commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ func newCompletionCommand(cnf *config.Config) *cobra.Command {
}
var b bytes.Buffer
c := &legacy.CLIWrapper{
Config: cnf,
Version: version,
CustomPharPath: viper.GetString("phar-path"),
Debug: viper.GetBool("debug"),
Stdout: &b,
Stderr: cmd.ErrOrStderr(),
Stdin: cmd.InOrStdin(),
Config: cnf,
Version: version,
CustomPharPath: viper.GetString("phar-path"),
Debug: viper.GetBool("debug"),
DisableInteraction: viper.GetBool("no-interaction"),
Stdout: &b,
Stderr: cmd.ErrOrStderr(),
Stdin: cmd.InOrStdin(),
}

if err := c.Init(); err != nil {
Expand Down
15 changes: 8 additions & 7 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ func newListCommand(cnf *config.Config) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
var b bytes.Buffer
c := &legacy.CLIWrapper{
Config: cnf,
Version: version,
CustomPharPath: viper.GetString("phar-path"),
Debug: viper.GetBool("debug"),
Stdout: &b,
Stderr: cmd.ErrOrStderr(),
Stdin: cmd.InOrStdin(),
Config: cnf,
Version: version,
CustomPharPath: viper.GetString("phar-path"),
Debug: viper.GetBool("debug"),
DisableInteraction: viper.GetBool("no-interaction"),
Stdout: &b,
Stderr: cmd.ErrOrStderr(),
Stdin: cmd.InOrStdin(),
}
if err := c.Init(); err != nil {
debugLog("%s\n", color.RedString(err.Error()))
Expand Down
22 changes: 15 additions & 7 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -52,6 +53,11 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
if viper.GetBool("quiet") {
viper.Set("no-interaction", true)
viper.Set("debug", false)
cmd.SetErr(io.Discard)
}
if viper.GetBool("version") {
versionCommand.Run(cmd, []string{})
os.Exit(0)
Expand All @@ -65,13 +71,14 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
},
Run: func(cmd *cobra.Command, _ []string) {
c := &legacy.CLIWrapper{
Config: cnf,
Version: version,
CustomPharPath: viper.GetString("phar-path"),
Debug: viper.GetBool("debug"),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
Stdin: cmd.InOrStdin(),
Config: cnf,
Version: version,
CustomPharPath: viper.GetString("phar-path"),
Debug: viper.GetBool("debug"),
DisableInteraction: viper.GetBool("no-interaction"),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
Stdin: cmd.InOrStdin(),
}
if err := c.Init(); err != nil {
debugLog("%s\n", color.RedString(err.Error()))
Expand Down Expand Up @@ -118,6 +125,7 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
cmd.PersistentFlags().Bool("debug", false, "Enable debug logging")
cmd.PersistentFlags().Bool("no-interaction", false, "Enable non-interactive mode")
cmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output")
cmd.PersistentFlags().BoolP("quiet", "q", false, "Suppress any messages and errors (stderr), while continuing to display necessary output (stdout). This implies --no-interaction.")

Check failure on line 128 in commands/root.go

View workflow job for this annotation

GitHub Actions / test

line is 181 characters (lll)

projectInitCmd := commands.NewPlatformifyCmd(assets)
projectInitCmd.SetHelpFunc(func(_ *cobra.Command, _ []string) {
Expand Down
18 changes: 11 additions & 7 deletions internal/legacy/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ func fileChanged(filename string, content []byte) (bool, error) {

// CLIWrapper wraps the legacy CLI
type CLIWrapper struct {
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
Config *config.Config
Version string
CustomPharPath string
Debug bool
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
Config *config.Config
Version string
CustomPharPath string
Debug bool
DisableInteraction bool
}

func (c *CLIWrapper) cacheDir() string {
Expand Down Expand Up @@ -170,6 +171,9 @@ func (c *CLIWrapper) Exec(ctx context.Context, args ...string) error {
if c.Debug {
cmd.Env = append(cmd.Env, envPrefix+"CLI_DEBUG=1")
}
if c.DisableInteraction {
cmd.Env = append(cmd.Env, envPrefix+"NO_INTERACTION=1")
}
cmd.Env = append(cmd.Env, fmt.Sprintf(
"%sUSER_AGENT={APP_NAME_DASH}/%s ({UNAME_S}; {UNAME_R}; PHP %s; WRAPPER %s)",
envPrefix,
Expand Down

0 comments on commit 7affd40

Please sign in to comment.