feat: added config implementation using viper
This commit is contained in:
parent
7713c2e55d
commit
481a0782c5
6 changed files with 215 additions and 64 deletions
115
cmd/config.go
Normal file
115
cmd/config.go
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.towk2.me/towk/makeshift/pkg/util"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const CONFIG_FILE = `---
|
||||
#
|
||||
# Makeshift Config
|
||||
#
|
||||
# Repository: https://git.towk2.me/towk/makeshift
|
||||
#
|
||||
# Default configuration file for 'makeshift' CLI and service.
|
||||
# This file was autogenerated using 'makeshift config new' command.
|
||||
#
|
||||
|
||||
# Set the service host.
|
||||
host: http://localhost:5050
|
||||
|
||||
# Set the path to the file or directory to download.
|
||||
path: help.txt
|
||||
|
||||
# Set the log file path. Logs will be written to this location.
|
||||
log-file: logs/makeshift.log
|
||||
|
||||
# Set the log level. Possible values include 'debug', 'info', 'warn',
|
||||
# 'error', 'disabled', and 'trace'.
|
||||
log-level: info
|
||||
|
||||
# Set the plugins to use when downloading files and/or directories.
|
||||
plugins:
|
||||
- smd
|
||||
- jinja2
|
||||
|
||||
# Set the positional arguments to pass to ALL plugins when downloading
|
||||
# files and directories. NOTE: These arguments may be ignored or not
|
||||
# used within certain plugins.
|
||||
plugin-args:
|
||||
|
||||
# Set the key-word arguments stored in the data that is passed to ALL
|
||||
# plugins when downloading files and directories. NOTE: These arguments
|
||||
# may be ignored or not used within certain plugins.
|
||||
plugin-kwargs:
|
||||
|
||||
# Set the profiles to use when downloading files and/or directories.
|
||||
profiles:
|
||||
- default
|
||||
|
||||
# Set whether to extract the archive when downloading directories.
|
||||
extract: false
|
||||
|
||||
# Set whether to remove an archive after downloading and extracting.
|
||||
# This requires that the 'extract' flag be set to 'true'.
|
||||
remove-archive: false
|
||||
|
||||
# Set the path to a CA certificate.
|
||||
cacert: ""
|
||||
|
||||
# Set the path to a CA key file.
|
||||
keyfile: ""
|
||||
`
|
||||
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Manage makeshift config file",
|
||||
}
|
||||
|
||||
var configNewCmd = &cobra.Command{
|
||||
Use: "new [path]",
|
||||
Example: `
|
||||
# create a new default config at specified path
|
||||
makeshift config new configs/makeshift.yaml
|
||||
`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Create a new config file",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
for _, path := range args {
|
||||
var (
|
||||
overwrite, _ = cmd.Flags().GetBool("overwrite")
|
||||
|
||||
exists bool
|
||||
err error
|
||||
)
|
||||
if exists, err = util.PathExists(path); err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("path", path).
|
||||
Msg("failed to determine if path exists")
|
||||
} else {
|
||||
if exists && !overwrite {
|
||||
log.Error().Msg("file exists and '--overwrite' flag not passed")
|
||||
return
|
||||
}
|
||||
err := os.WriteFile(path, []byte(CONFIG_FILE), 0o644)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("path", path).
|
||||
Msg("failed to write diefault config file to path")
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
configNewCmd.Flags().BoolP("overwrite", "f", false, "Set whether to overwrite an existing file")
|
||||
configCmd.AddCommand(configNewCmd)
|
||||
rootCmd.AddCommand(configCmd)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue