Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --quiet (-q) flag to silence stderr #207

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 17 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,10 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
if viper.GetBool("quiet") && !viper.GetBool("debug") && !viper.GetBool("verbose") {
viper.Set("no-interaction", true)
cmd.SetErr(io.Discard)
}
if viper.GetBool("version") {
versionCommand.Run(cmd, []string{})
os.Exit(0)
Expand All @@ -65,13 +70,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 +124,10 @@ 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. Ignored in verbose mode.",
)

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
Loading