feat: add validate command

This commit is contained in:
mzz2017 2023-02-04 22:21:27 +08:00
parent a6d2628505
commit 5af9c9d46e
3 changed files with 39 additions and 20 deletions

View File

@ -1,7 +1,6 @@
package cmd
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -25,21 +24,5 @@ func Execute() error {
func init() {
rootCmd.AddCommand(runCmd)
}
func NewLogger(verbose int) *logrus.Logger {
log := logrus.New()
var level logrus.Level
switch verbose {
case 0:
level = logrus.WarnLevel
case 1:
level = logrus.InfoLevel
default:
level = logrus.TraceLevel
}
log.SetLevel(level)
return log
rootCmd.AddCommand(validateCmd)
}

View File

@ -45,7 +45,7 @@ func Run(log *logrus.Logger) (err error) {
internal.AutoSu()
// Read config from --config cfgFile.
param, err := readConfig()
param, err := readConfig(cfgFile)
if err != nil {
return fmt.Errorf("readConfig: %w", err)
}
@ -100,7 +100,7 @@ func Run(log *logrus.Logger) (err error) {
return nil
}
func readConfig() (params *config.Params, err error) {
func readConfig(cfgFile string) (params *config.Params, err error) {
b, err := os.ReadFile(cfgFile)
if err != nil {
return nil, err

36
cmd/validate.go Normal file
View File

@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) since 2023, mzz2017 <mzz@tuta.io>
*/
package cmd
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
var (
validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate dae config",
Run: func(cmd *cobra.Command, args []string) {
if cfgFile == "" {
logrus.Fatalln("Argument \"--config\" or \"-c\" is required but not provided.")
}
// Read config from --config cfgFile.
_, err := readConfig(cfgFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("OK")
},
}
)
func init() {
validateCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
}