chinampa/pkg/runtime/runtime.go

113 lines
1.8 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
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
)
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
}
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
}
func ValidationEnabled() bool {
2022-12-31 05:53:24 +00:00
return isFalseIsh(os.Getenv(env.ValidationDisabled))
2022-12-19 03:04:34 +00:00
}
func VerboseEnabled() bool {
2022-12-31 05:53:24 +00:00
return isTrueIsh(os.Getenv(env.Verbose))
}
func SilenceEnabled() bool {
if isTrueIsh(os.Getenv(env.Silent)) {
return true
}
if VerboseEnabled() {
return false
}
for _, arg := range os.Args {
if arg == "--silent" {
return true
}
}
return false
2022-12-19 03:04:34 +00:00
}
func ColorEnabled() bool {
2022-12-31 05:53:24 +00:00
return isFalseIsh(os.Getenv(env.NoColor)) && !UnstyledHelpEnabled()
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 the resolved environment map.
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
}