Skip to content

Commit

Permalink
Add -q and -qq options (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinhlybin authored Sep 23, 2024
1 parent fc4addf commit beef5d4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .docker-env/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ compose_default_profile: app
compose_sidecar_profile: sidecar

# Debug options
show_commands: true
show_executed_commands: true

# Env files to load environmental variables used in the docker-compose.yml file
# for substitution in the services section
Expand Down
5 changes: 5 additions & 0 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/marcinhlybin/docker-env/addons"
"github.com/marcinhlybin/docker-env/config"
"github.com/marcinhlybin/docker-env/logger"
"github.com/marcinhlybin/docker-env/project"
"github.com/marcinhlybin/docker-env/registry"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -42,6 +43,10 @@ func initializeConfig(c *cli.Context) (*config.Config, error) {
if err := cfg.LoadConfig(c.String("config")); err != nil {
return nil, err
}

// Show executed commands
logger.ShowExecutedCommands(cfg.ShowExecutedCommands)

return cfg, nil
}

Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Config struct {
PostStartHook string `yaml:"post_start_hook"`
PostStopHook string `yaml:"post_stop_hook"`
RequiredVars []string `yaml:"required_vars"`
ShowCommands bool `yaml:"show_commands"`
ShowExecutedCommands bool `yaml:"show_executed_commands"`
}

func NewConfig() *Config {
Expand All @@ -51,7 +51,7 @@ func NewConfig() *Config {
PostStartHook: "",
PostStopHook: "",
RequiredVars: []string{},
ShowCommands: true,
ShowExecutedCommands: true,
}
}

Expand Down Expand Up @@ -118,6 +118,6 @@ func (c *Config) ShowConfig() error {
fmt.Println("VSCode default service:", c.VscodeDefaultService)
fmt.Println("VSCode default directory:", c.VscodeDefaultDir)
fmt.Println()
fmt.Println("Show commands:", c.ShowCommands)
fmt.Println("Show executed commands:", c.ShowExecutedCommands)
return nil
}
8 changes: 3 additions & 5 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type DockerCmd struct {
}

func NewDockerCmd(cfg *config.Config) *DockerCmd {
logger.ShowCommands(cfg.ShowCommands)

return &DockerCmd{
Config: cfg,
Cmd: "docker",
Expand Down Expand Up @@ -90,12 +88,12 @@ func (dc *DockerCmd) Execute() error {
cmd := exec.Command(dc.Cmd, dc.Args...)
logger.Execute(cmd.String())

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
// cmd.Stdin = logger.Stdin()
cmd.Stdout = logger.Stdout()

// Capture stderr output
var stderrBuf bytes.Buffer
cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
cmd.Stderr = io.MultiWriter(logger.Stderr(), &stderrBuf)

if err := cmd.Run(); err != nil {
// Log the captured stderr output
Expand Down
55 changes: 51 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,60 @@ package logger

import (
"fmt"
"io"
"os"

"github.com/pterm/pterm"
)

var isQuiet bool
var isQuieter bool
var showExecutedCommands bool

// Default prefix texts
var (
infoPrefixText = "INFO"
executePrefixText = "EXECUTE"
warningPrefixText = "WARNING"
errorPrefixText = "ERROR"
debugPrefixText = "DEBUG"
executeEnabled = true
)

// Stdin returns os.Stdin
func Stdin() *os.File {
return os.Stdin
}

// Stdout returns os.Stdout or os.DevNull depending on the verbosity level
func Stdout() io.Writer {
if isQuieter {
return io.Discard
}
return os.Stdout
}

// Stderr returns os.Stderr or os.DevNull depending on the verbosity level
func Stderr() io.Writer {
if isQuieter {
return io.Discard
}
return os.Stderr
}

// Disables info messages
func SetQuiet(quiet bool) {
isQuiet = quiet
}

// Disables commands output
func SetQuieter(quiet bool) {
if quiet {
SetQuiet(quiet)
}
isQuieter = quiet

}

func SetPrefix(prefix string) {
infoPrefixText = prefix
executePrefixText = prefix
Expand All @@ -28,11 +69,14 @@ func SetDebug(debug bool) {
}
}

func ShowCommands(show bool) {
executeEnabled = show
func ShowExecutedCommands(showCommands bool) {
showExecutedCommands = showCommands
}

func Info(format string, args ...any) {
if isQuiet {
return
}
pterm.Info.Prefix = pterm.Prefix{
Text: infoPrefixText,
Style: pterm.NewStyle(pterm.BgGreen, pterm.FgBlack),
Expand All @@ -43,6 +87,9 @@ func Info(format string, args ...any) {
}

func Warning(format string, args ...any) {
if isQuieter {
return
}
pterm.Warning.Prefix = pterm.Prefix{
Text: warningPrefixText,
Style: pterm.NewStyle(pterm.BgYellow, pterm.FgBlack),
Expand Down Expand Up @@ -73,7 +120,7 @@ func Error(format string, args ...any) {
}

func Execute(msg string) {
if !executeEnabled {
if isQuiet || !showExecutedCommands {
return
}
pterm.Info.Prefix = pterm.Prefix{
Expand Down
24 changes: 21 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ If environment name is not specified current branch name is used.`,
Aliases: []string{"d"},
Usage: "enable debug mode",
},
&cli.BoolFlag{
Name: "quiet",
Aliases: []string{"q"},
Usage: "disable info messages",
},
&cli.BoolFlag{
Name: "quieter",
Aliases: []string{"qq"},
Usage: "disable docker output",
},
},
Before: func(c *cli.Context) error {
// Show help if no arguments
Expand All @@ -49,9 +59,17 @@ If environment name is not specified current branch name is used.`,
}

// Enable debug mode
if c.Bool("debug") {
logger.SetDebug(true)
}
showDebug := c.Bool("debug")
logger.SetDebug(showDebug)

// Disable info messages
// Docker output is still visible
quiet := c.Bool("quiet")
logger.SetQuiet(quiet)

// Disable all output except errors
quieter := c.Bool("quieter")
logger.SetQuieter(quieter)

return nil
},
Expand Down

0 comments on commit beef5d4

Please sign in to comment.