mirror of
https://github.com/daeuniverse/dae.git
synced 2025-01-22 02:07:50 +07:00
feat: add validate command
This commit is contained in:
parent
a6d2628505
commit
5af9c9d46e
19
cmd/cmd.go
19
cmd/cmd.go
@ -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)
|
||||
}
|
||||
|
@ -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
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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user