Files
umbrix-libcore/cmd/cmd_parse.go

53 lines
1.1 KiB
Go
Raw Normal View History

package cmd
2024-01-15 17:59:45 +03:30
import (
"fmt"
"os"
"path/filepath"
2024-03-22 16:25:56 +00:00
"github.com/hiddify/hiddify-core/config"
2024-01-15 17:59:45 +03:30
"github.com/sagernet/sing-box/log"
"github.com/spf13/cobra"
)
var commandParseOutputPath string
var commandParse = &cobra.Command{
Use: "parse",
Short: "Parse configuration",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := parse(args[0])
if err != nil {
log.Fatal(err)
}
},
}
func init() {
commandParse.Flags().StringVarP(&commandParseOutputPath, "output", "o", "", "write result to file path instead of stdout")
2024-05-29 18:47:04 +02:00
2024-01-15 17:59:45 +03:30
mainCommand.AddCommand(commandParse)
}
func parse(path string) error {
if workingDir != "" {
path = filepath.Join(workingDir, path)
}
config, err := config.ParseConfig(path, true)
if err != nil {
return err
}
if commandParseOutputPath != "" {
outputPath, _ := filepath.Abs(filepath.Join(workingDir, commandParseOutputPath))
2024-01-29 21:55:01 +01:00
err = os.WriteFile(outputPath, config, 0644)
2024-01-15 17:59:45 +03:30
if err != nil {
return err
}
fmt.Println("result successfully written to ", outputPath)
} else {
os.Stdout.Write(config)
}
return nil
}