chinampa/pkg/runtime/runtime.go

163 lines
3.3 KiB
Go
Raw Normal View History

2022-12-19 03:04:34 +00:00
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
2022-12-31 05:53:24 +00:00
// SPDX-License-Identifier: Apache-2.0
/*
Package runtime presents environment related information useful during your program's runtime.
*/
2022-12-19 03:04:34 +00:00
package runtime
import (
"os"
"strconv"
2022-12-31 05:53:24 +00:00
"git.rob.mx/nidito/chinampa/pkg/env"
2022-12-19 03:04:34 +00:00
)
// Executable is the name of our binary, and should be set
// using `chinampa.Execute(config chinampa.Config)`.
2022-12-29 19:05:58 +00:00
var Executable = "chinampa"
2022-12-19 03:04:34 +00:00
var falseIshValues = []string{
"",
"0",
"no",
"false",
"disable",
"disabled",
"off",
"never",
}
var trueIshValues = []string{
"1",
"yes",
"true",
"enable",
"enabled",
"on",
"always",
}
func isFalseIsh(val string) bool {
for _, negative := range falseIshValues {
if val == negative {
return true
}
}
return false
}
func isTrueIsh(val string) bool {
for _, positive := range trueIshValues {
if val == positive {
return true
}
}
return false
}
var _flags map[string]bool
// ResetParsedFlagsCache resets the cached parsed global flags.
func ResetParsedFlagsCache() {
_flags = nil
}
func flagInArgs(name string) bool {
if _flags == nil {
_flags = map[string]bool{}
for _, arg := range os.Args {
switch arg {
case "--verbose":
_flags["verbose"] = true
delete(_flags, "silent")
case "--silent":
_flags["silent"] = true
delete(_flags, "verbose")
case "--color":
_flags["color"] = true
delete(_flags, "no-color")
case "--no-color":
_flags["no-color"] = true
delete(_flags, "color")
case "--skip-validation":
_flags["skip-validation"] = true
}
}
}
_, ok := _flags[name]
return ok
}
// DebugEnabled tells if debugging was requested.
2022-12-19 03:04:34 +00:00
func DebugEnabled() bool {
2022-12-31 05:53:24 +00:00
return isTrueIsh(os.Getenv(env.Debug))
2022-12-19 03:04:34 +00:00
}
// DebugEnabled tells if debugging was requested.
2022-12-19 03:04:34 +00:00
func ValidationEnabled() bool {
return !flagInArgs("skip-validation") && isFalseIsh(os.Getenv(env.ValidationDisabled))
2022-12-19 03:04:34 +00:00
}
// VerboseEnabled tells if verbose output was requested.
2022-12-19 03:04:34 +00:00
func VerboseEnabled() bool {
if flagInArgs("silent") {
return false
2023-03-20 06:15:53 +00:00
}
return isTrueIsh(os.Getenv(env.Verbose)) || flagInArgs("verbose")
2022-12-31 05:53:24 +00:00
}
// SilenceEnabled tells if silencing of output was requested.
2022-12-31 05:53:24 +00:00
func SilenceEnabled() bool {
if flagInArgs("verbose") {
2023-01-02 06:54:49 +00:00
return false
}
if flagInArgs("silent") {
return true
}
2023-01-02 06:54:49 +00:00
return isTrueIsh(os.Getenv(env.Silent)) || flagInArgs("silent")
2022-12-19 03:04:34 +00:00
}
// ColorEnabled tells if colorizing output was requested.
2022-12-19 03:04:34 +00:00
func ColorEnabled() bool {
if flagInArgs("color") {
return true
}
// we're talking to ttys, we want color unless NO_COLOR/--no-color
return !(isTrueIsh(os.Getenv(env.NoColor)) || UnstyledHelpEnabled() || flagInArgs("no-color"))
2022-12-19 03:04:34 +00:00
}
// UnstyledHelpEnabled tells if help should be printed without formatting.
2022-12-19 03:04:34 +00:00
func UnstyledHelpEnabled() bool {
2022-12-31 05:53:24 +00:00
return isTrueIsh(os.Getenv(env.HelpUnstyled))
2022-12-19 03:04:34 +00:00
}
// EnvironmentMap returns a map of environment keys for color, debugging and verbosity and their values, ready for `os.Setenv`.
2022-12-19 03:04:34 +00:00
func EnvironmentMap() map[string]string {
2022-12-31 05:53:24 +00:00
res := map[string]string{}
2022-12-19 03:04:34 +00:00
trueString := strconv.FormatBool(true)
if !ColorEnabled() {
2022-12-31 05:53:24 +00:00
res[env.NoColor] = trueString
} else if isTrueIsh(os.Getenv(env.ForceColor)) {
res[env.ForceColor] = "always"
2022-12-19 03:04:34 +00:00
}
if DebugEnabled() {
2022-12-31 05:53:24 +00:00
res[env.Debug] = trueString
2022-12-19 03:04:34 +00:00
}
if VerboseEnabled() {
2022-12-31 05:53:24 +00:00
res[env.Verbose] = trueString
} else if SilenceEnabled() {
res[env.Silent] = trueString
2022-12-19 03:04:34 +00:00
}
2022-12-31 05:53:24 +00:00
return res
2022-12-19 03:04:34 +00:00
}