github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/cmd/common/setup.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: 2021-Present The Jackal Authors 3 4 // Package common handles command configuration across all commands 5 package common 6 7 import ( 8 "io" 9 "os" 10 11 "github.com/Racer159/jackal/src/config" 12 "github.com/Racer159/jackal/src/config/lang" 13 "github.com/Racer159/jackal/src/pkg/message" 14 "github.com/pterm/pterm" 15 ) 16 17 // LogLevelCLI holds the log level as input from a command 18 var LogLevelCLI string 19 20 // SetupCLI sets up the CLI logging, interrupt functions, and more 21 func SetupCLI() { 22 ExitOnInterrupt() 23 24 match := map[string]message.LogLevel{ 25 "warn": message.WarnLevel, 26 "info": message.InfoLevel, 27 "debug": message.DebugLevel, 28 "trace": message.TraceLevel, 29 } 30 31 if config.NoColor { 32 message.DisableColor() 33 } 34 35 printViperConfigUsed() 36 37 // No log level set, so use the default 38 if LogLevelCLI != "" { 39 if lvl, ok := match[LogLevelCLI]; ok { 40 message.SetLogLevel(lvl) 41 message.Debug("Log level set to " + LogLevelCLI) 42 } else { 43 message.Warn(lang.RootCmdErrInvalidLogLevel) 44 } 45 } 46 47 // Disable progress bars for CI envs 48 if os.Getenv("CI") == "true" { 49 message.Debug("CI environment detected, disabling progress bars") 50 message.NoProgress = true 51 } 52 53 if !config.SkipLogFile { 54 logFile, err := message.UseLogFile("") 55 if err != nil { 56 message.WarnErr(err, "Error saving a log file to a temporary directory") 57 return 58 } 59 60 pterm.SetDefaultOutput(io.MultiWriter(os.Stderr, logFile)) 61 location := message.LogFileLocation() 62 message.Notef("Saving log file to %s", location) 63 } 64 }