Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
140 lines (125 loc) · 3.13 KB
/
Copy pathparse.go
File metadata and controls
140 lines (125 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package alog
import (
"log/slog"
"os"
"strconv"
"strings"
)
// Env settings for the package.
const (
// EnvVerbosity is an environment variable (suffix) to use for manipulating with verbosity.
// verbosity takes integer values like 0, 1, 2, etc.
// It can be used with ${APP}_VERBOSITY where ${APP} is set over [Env], [SetEnv] helpers.
EnvVerbosity = "VERBOSITY"
// EnvSilent is an environment variable to use for manipulating with logger in general to
// switch it off.
EnvSilent = "SILENT"
// FlagSilent is a full argument flag for silent option.
FlagSilent = "--silent"
)
func flagNormalize(flag string) string {
return strings.ToUpper(strings.ReplaceAll(flag, "-", "_"))
}
// verbosityFromEnv reads verbosity level from environment variables.
// It might resolve into:
// - 0 (default) if environment variable wasn't found
// - 0 (default) if variable can no be read in a integer format (e.g. keeps string)
// - n read successfully
func verbosityFromEnv() (verbosity int, valid bool) {
rawValue, found := os.LookupEnv(envA.Load().(envFunc)(EnvVerbosity))
if !found {
return 0, false
}
val, err := strconv.ParseInt(rawValue, 10, 64)
if err != nil {
return 0, false
}
return int(val), true
}
// ParseVerbosityInt returns verbosity in [int] form.
func ParseVerbosityInt() (verbosity int) {
return parseVerbosity()
}
// parseVerbosity parses verbosity from cli arguments.
func parseVerbosity() (verbosity int) {
var valid bool
if verbosity, valid = verbosityFromEnv(); valid {
return
}
for _, arg := range os.Args {
// reads `-v=N` format, note -v 1337 format would be ignored.
// TODO: make it happen?
if strings.HasPrefix(arg, "-v=") {
if _, value, found := strings.Cut(arg, "-v="); found {
v, _ := strconv.ParseInt(value, 10, 64)
verbosity += int(v)
}
continue
}
// Note, we read -vv..vvv format after -v=N is parsed.
if strings.HasPrefix(arg, "-v") {
verbosity += strings.Count(arg, "v")
}
}
return
}
// ParseVerbosity parses verbosity level for the application
//
// Emergency: -2
// Error: -1
// Warning: 0
// Notice: 1
// Info: 2
// Debug: 3
// Trace: 4
func ParseVerbosity() (level slog.Level) {
lvl := parseVerbosity()
switch lvl {
case -2:
return LevelEmergency
case -1:
return LevelError
case 0:
return LevelWarning
case 1:
return LevelNotice
case 2:
return LevelInfo
case 3:
return LevelDebug
case 4:
return LevelTrace
default:
return LevelTrace
}
}
func parseSilentFromEnv() (silent bool, valid bool) {
rawValue, found := os.LookupEnv(envA.Load().(envFunc)(EnvSilent))
if !found {
return false, false
}
val, err := strconv.ParseBool(rawValue)
if err != nil {
return false, false
}
return val, true
}
// ParseVerbosity from arguments
func parseSilent() (silent bool) {
var valid bool
if silent, valid = parseSilentFromEnv(); valid {
return
}
for _, arg := range os.Args {
// ignores --silent=false, so it can only enable silent mode.
if strings.HasPrefix(arg, FlagSilent) {
silent = true
break
}
}
return
}
// ParseSilent identifies if silent is set over cli arguments or environment variables.
func ParseSilent() bool {
return parseSilent()
}