-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (90 loc) · 2.85 KB
/
main.go
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
package main
import (
"fmt"
"log/slog"
"os"
"github.com/spf13/cobra"
"versioning-cli/internal/commands"
"versioning-cli/internal/constants"
"versioning-cli/internal/log"
"versioning-cli/internal/types/level"
)
var (
version = "development" // see go linking for compile-time variable overwrites
commit = "n/a" // see go linking for compile-time variable overwrites
date = "latest" // see go linking for compile-time variable overwrites
)
var (
// logging is a variable that represents the current log level configuration.
logging = level.Error
)
func main() {
var root = &cobra.Command{
Use: constants.Name(),
Short: fmt.Sprintf("%s - Versioning Utilities CLI", constants.Name()),
Long: fmt.Sprintf("%s is a tool that facilitates development-related versioning.", constants.Name()),
Example: "",
ValidArgs: nil,
ValidArgsFunction: nil,
Args: nil,
ArgAliases: nil,
BashCompletionFunction: "",
Deprecated: "",
Annotations: nil,
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
var l level.Type
if e := l.Set(logging.String()); e != nil {
return e
} else {
log.Default(l.String())
}
if e := os.Setenv("LOG_LEVEL", logging.String()); e != nil {
slog.Log(ctx, log.Error, "Unable to Set Log Level Environment Variable", slog.String("error", e.Error()))
e = fmt.Errorf("unable to set LOG_LEVEL environment variable: %w", e)
return e
}
slog.Log(ctx, log.Trace, "Root", slog.Group("command",
slog.String("name", cmd.Name()),
slog.String("version", version),
slog.String("commit", commit),
slog.String("date", date),
slog.Group("flags",
slog.String("log-level", logging.String()),
),
slog.Group("environment",
slog.String("LOG_LEVEL", os.Getenv("LOG_LEVEL")),
),
))
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
// @todo Logic to check if a newer version is available
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) == 0 {
if e := cmd.Help(); e != nil {
slog.Log(ctx, log.Error, "Unable to Execute Command Help() Command", slog.String("error", e.Error()))
}
}
return nil
},
PostRun: nil,
CompletionOptions: cobra.CompletionOptions{},
TraverseChildren: true,
Hidden: false,
SilenceErrors: false,
SilenceUsage: false,
}
root.PersistentFlags().VarP(&logging, "verbosity", "v", "sets and configures logging verbosity")
commands.Execute(root)
}
func init() {
if e := os.Setenv("VERSION", version); e != nil {
exception := fmt.Errorf("unable to set VERSION: %w", e)
panic(exception)
}
}