Skip to content

Commit

Permalink
feat: silent flag in .air.toml, suppressing all air prints (#641)
Browse files Browse the repository at this point in the history
* feat: silent flag in .air.toml, suppressing all air prints

useful if the app being watched has structured outputs that are being
piped into subsequent program/s.

* update: add silent to air_example.toml
  • Loading branch information
jesses-code-adventures authored Sep 25, 2024
1 parent 9f5042d commit 0dfcd29
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions air_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ rerun_delay = 500
time = false
# Only show main log (silences watcher, build, runner)
main_only = false
# silence all logs produced by air
silent = false

[color]
# Customize each part's color. If no color found, use the raw app log.
Expand Down
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,21 @@ func GetVersionInfo() versionInfo { //revive:disable:unexported-return
}
}

func main() {
func printSplash() {
versionInfo := GetVersionInfo()
fmt.Printf(`
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ %s, built with Go %s
`, versionInfo.airVersion, versionInfo.goVersion)
}

func main() {
if showVersion {
printSplash()
return
}

if debugMode {
fmt.Println("[debug] mode")
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

Expand All @@ -96,6 +95,12 @@ func main() {
return
}
cfg.WithArgs(cmdArgs)
if !cfg.Log.Silent {
printSplash()
}
if debugMode && !cfg.Log.Silent {
fmt.Println("[debug] mode")
}
r, err := runner.NewEngineWithConfig(cfg, debugMode)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 3 additions & 4 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c *cfgBuild) RegexCompiled() ([]*regexp.Regexp, error) {
type cfgLog struct {
AddTime bool `toml:"time"`
MainOnly bool `toml:"main_only"`
Silent bool `toml:"silent"`
}

type cfgColor struct {
Expand Down Expand Up @@ -186,7 +187,7 @@ func defaultPathConfig() (*Config, error) {
for _, name := range []string{dftTOML, dftConf} {
cfg, err := readConfByName(name)
if err == nil {
if name == dftConf {
if name == dftConf && !cfg.Log.Silent {
fmt.Println("`.air.conf` will be deprecated soon, recommend using `.air.toml`.")
}
return cfg, nil
Expand Down Expand Up @@ -237,6 +238,7 @@ func defaultConfig() Config {
log := cfgLog{
AddTime: false,
MainOnly: false,
Silent: false,
}
color := cfgColor{
Main: "magenta",
Expand Down Expand Up @@ -305,9 +307,6 @@ func (c *Config) preprocess() error {
if c.TestDataDir == "" {
c.TestDataDir = "testdata"
}
if err != nil {
return err
}
ed := c.Build.ExcludeDir
for i := range ed {
ed[i] = cleanPath(ed[i])
Expand Down
3 changes: 3 additions & 0 deletions runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func newLogFunc(colorname string, cfg cfgLog) logFunc {
return func(msg string, v ...interface{}) {
// There are some escape sequences to format color in terminal, so cannot
// just trim new line from right.
if cfg.Silent {
return
}
msg = strings.ReplaceAll(msg, "\n", "")
msg = strings.TrimSpace(msg)
if len(msg) == 0 {
Expand Down
18 changes: 18 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@ const (
)

func (e *Engine) mainLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return
}
e.logWithLock(func() {
e.logger.main()(format, v...)
})
}

func (e *Engine) mainDebug(format string, v ...interface{}) {
if e.config.Log.Silent {
return
}
if e.debugMode {
e.mainLog(format, v...)
}
}

func (e *Engine) buildLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return
}
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.build()(format, v...)
Expand All @@ -41,6 +50,9 @@ func (e *Engine) buildLog(format string, v ...interface{}) {
}

func (e *Engine) runnerLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return
}
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.runner()(format, v...)
Expand All @@ -49,6 +61,9 @@ func (e *Engine) runnerLog(format string, v ...interface{}) {
}

func (e *Engine) watcherLog(format string, v ...interface{}) {
if e.config.Log.Silent {
return
}
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.watcher()(format, v...)
Expand All @@ -57,6 +72,9 @@ func (e *Engine) watcherLog(format string, v ...interface{}) {
}

func (e *Engine) watcherDebug(format string, v ...interface{}) {
if e.config.Log.Silent {
return
}
if e.debugMode {
e.watcherLog(format, v...)
}
Expand Down

0 comments on commit 0dfcd29

Please sign in to comment.