revert changes to format command

This commit is contained in:
jebbs 2022-10-14 09:45:00 +08:00
parent 33f561ba46
commit 8bb85f0855

View File

@ -1,11 +1,13 @@
package main package main
import ( import (
"bytes"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/sagernet/sing-box/common/conf"
"github.com/sagernet/sing-box/common/conf/mergers" "github.com/sagernet/sing-box/common/conf/mergers"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
@ -13,12 +15,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var commandFormatWrite string var commandFormatFlagWrite bool
var commandFormatEncoding string
var commandFormat = &cobra.Command{ var commandFormat = &cobra.Command{
Use: "format", Use: "format",
Short: "Format configuration", Short: "Format configuration (json only)",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := format() err := format()
if err != nil { if err != nil {
@ -29,55 +30,58 @@ var commandFormat = &cobra.Command{
} }
func init() { func init() {
commandFormat.Flags().StringVarP(&commandFormatWrite, "write", "w", "", "write result to file instead of stdout") commandFormat.Flags().BoolVarP(&commandFormatFlagWrite, "write", "w", false, "write result to (source) file instead of stdout")
commandFormat.Flags().StringVarP(&commandFormatEncoding, "format", "f", string(mergers.FormatJSON), "output format: json, yaml, toml")
mainCommand.AddCommand(commandFormat) mainCommand.AddCommand(commandFormat)
} }
func format() error { func format() error {
var ( if len(configPaths) > 1 {
configContent []byte return E.New("only one file can be formatted at a time")
err error }
) configPath := configPaths[0]
format := mergers.ParseFormat(configFormat) format := mergers.ParseFormat(configFormat)
encode := mergers.ParseFormat(commandFormatEncoding) isJSON := strings.EqualFold(filepath.Ext(configPath), ".json")
if encode == mergers.FormatAuto { // only json is supported, since:
encode = mergers.FormatJSON // 1. all other foramts will lost comments after formatting
} // 2. fields order is shuffled for yaml and toml, because of the nature of the go map
if len(configPaths) == 1 && configPaths[0] == "stdin" { if format != mergers.FormatJSON &&
configContent, err = conf.ReaderToJSON(os.Stdin, format) (format == mergers.FormatAuto && !isJSON) {
} else { return E.New("only json format is supported")
configContent, err = conf.FilesToJSON(configPaths, format, configRecursive)
} }
configContent, err := os.ReadFile(configPath)
if err != nil { if err != nil {
return E.Cause(err, "read config") return E.Cause(err, "read config")
} }
var options option.Options var options option.Options
err = options.UnmarshalJSON(configContent) err = options.UnmarshalJSON(configContent)
if err != nil { if err != nil {
return E.Cause(err, "decode config") return E.Cause(err, "decode config")
} }
content, err := conf.Sprint(options, encode) buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(options)
if err != nil { if err != nil {
return E.Cause(err, "encode config") return E.Cause(err, "encode config")
} }
if !commandFormatFlagWrite {
if commandFormatWrite == "" { os.Stdout.WriteString(buffer.String() + "\n")
os.Stdout.WriteString(content + "\n")
return nil return nil
} }
if bytes.Equal(configContent, buffer.Bytes()) {
output, err := os.Create(commandFormatWrite) return nil
}
output, err := os.Create(configPath)
if err != nil { if err != nil {
return E.Cause(err, "open output") return E.Cause(err, "open output")
} }
_, err = output.WriteString(content) _, err = output.Write(buffer.Bytes())
output.Close() output.Close()
if err != nil { if err != nil {
return E.Cause(err, "write output") return E.Cause(err, "write output")
} }
outputPath, _ := filepath.Abs(commandFormatWrite) outputPath, _ := filepath.Abs(configPath)
os.Stderr.WriteString(outputPath + "\n") os.Stderr.WriteString(outputPath + "\n")
return nil return nil
} }