feat: added root, download, list, and serve cmd implementations
This commit is contained in:
parent
1f5775196d
commit
419e9781bf
4 changed files with 229 additions and 16 deletions
59
cmd/root.go
59
cmd/root.go
|
|
@ -3,21 +3,38 @@ package cmd
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
host string
|
||||
path string
|
||||
outputPath string
|
||||
rootPath string
|
||||
logLevel string
|
||||
timeout int
|
||||
)
|
||||
var rootCmd = cobra.Command{
|
||||
Use: "configurator",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
Use: "configurator",
|
||||
Short: "Extensible configuration builder to download files",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
// set the logging level
|
||||
level, err := strToLogLevel(logLevel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to convert log level argument")
|
||||
os.Exit(1)
|
||||
}
|
||||
zerolog.SetGlobalLevel(level)
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
// run initialization code first
|
||||
initEnv()
|
||||
|
||||
// run the main program
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
|
|
@ -26,12 +43,34 @@ func Execute() {
|
|||
|
||||
func init() {
|
||||
// initialize the config a single time
|
||||
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "Set the log level output")
|
||||
}
|
||||
|
||||
func initConfigFromFile(path string) {
|
||||
|
||||
func strToLogLevel(ll string) (zerolog.Level, error) {
|
||||
levels := []string{"debug", "info", "warn", "disabled"}
|
||||
if index := slices.Index(levels, ll); index >= 0 {
|
||||
// handle special case to map index == 3 to zerolog.Disabled == 7
|
||||
switch index {
|
||||
case 3:
|
||||
return zerolog.Disabled, nil
|
||||
}
|
||||
return zerolog.Level(index), nil
|
||||
}
|
||||
return -100, fmt.Errorf(
|
||||
"invalid log level (options: %s)", strings.Join(levels, ", "),
|
||||
) // use 'info' by default
|
||||
}
|
||||
|
||||
func initEnv() {
|
||||
|
||||
func setenv(v *string, key string) {
|
||||
t := os.Getenv(key)
|
||||
if t != "" {
|
||||
*v = t
|
||||
}
|
||||
}
|
||||
|
||||
// func setenv(cmd *cobra.Command, varname string, envvar string) {
|
||||
// v := os.Getenv(envvar)
|
||||
// if v != "" {
|
||||
// cmd.Flags().Set(varname, v)
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue