mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-20 12:49:17 +07:00
feat: add validate command
This commit is contained in:
19
cmd/cmd.go
19
cmd/cmd.go
@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,21 +24,5 @@ func Execute() error {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(runCmd)
|
rootCmd.AddCommand(runCmd)
|
||||||
}
|
rootCmd.AddCommand(validateCmd)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ func Run(log *logrus.Logger) (err error) {
|
|||||||
internal.AutoSu()
|
internal.AutoSu()
|
||||||
|
|
||||||
// Read config from --config cfgFile.
|
// Read config from --config cfgFile.
|
||||||
param, err := readConfig()
|
param, err := readConfig(cfgFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("readConfig: %w", err)
|
return fmt.Errorf("readConfig: %w", err)
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ func Run(log *logrus.Logger) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig() (params *config.Params, err error) {
|
func readConfig(cfgFile string) (params *config.Params, err error) {
|
||||||
b, err := os.ReadFile(cfgFile)
|
b, err := os.ReadFile(cfgFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
36
cmd/validate.go
Normal file
36
cmd/validate.go
Normal 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")
|
||||||
|
}
|
Reference in New Issue
Block a user