Files
sing-box-extended-mirror/cmd/sing-box/cmd_format.go
T

110 lines
2.5 KiB
Go
Raw Normal View History

2022-07-04 16:59:27 +08:00
package main
import (
"bytes"
"os"
"path/filepath"
2022-07-24 17:58:52 +08:00
"github.com/sagernet/sing-box/common/json"
2022-07-12 15:17:29 +08:00
"github.com/sagernet/sing-box/log"
2022-07-04 16:59:27 +08:00
"github.com/sagernet/sing-box/option"
2022-08-13 18:36:49 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-06 15:01:09 +08:00
2022-07-04 16:59:27 +08:00
"github.com/spf13/cobra"
)
var commandFormatFlagWrite bool
var commandFormat = &cobra.Command{
Use: "format",
2022-07-04 17:08:28 +08:00
Short: "Format configuration",
2022-08-13 18:36:49 +08:00
Run: func(cmd *cobra.Command, args []string) {
err := format()
if err != nil {
log.Fatal(err)
}
},
Args: cobra.NoArgs,
2022-07-04 16:59:27 +08:00
}
func init() {
commandFormat.Flags().BoolVarP(&commandFormatFlagWrite, "write", "w", false, "write result to (source) file instead of stdout")
2022-08-13 18:36:49 +08:00
mainCommand.AddCommand(commandFormat)
2022-07-04 16:59:27 +08:00
}
2022-08-13 18:36:49 +08:00
func format() error {
2023-03-18 19:15:28 +08:00
optionsList, err := readConfig()
if err != nil {
return err
}
for _, optionsEntry := range optionsList {
buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(optionsEntry.options)
if err != nil {
return E.Cause(err, "encode config")
}
outputPath, _ := filepath.Abs(optionsEntry.path)
if !commandFormatFlagWrite {
if len(optionsList) > 1 {
os.Stdout.WriteString(outputPath + "\n")
}
os.Stdout.WriteString(buffer.String() + "\n")
continue
}
if bytes.Equal(optionsEntry.content, buffer.Bytes()) {
continue
}
output, err := os.Create(optionsEntry.path)
if err != nil {
return E.Cause(err, "open output")
}
_, err = output.Write(buffer.Bytes())
output.Close()
if err != nil {
return E.Cause(err, "write output")
}
os.Stderr.WriteString(outputPath + "\n")
}
return nil
}
func formatOne(configPath string) error {
2022-07-04 16:59:27 +08:00
configContent, err := os.ReadFile(configPath)
if err != nil {
2022-08-13 18:36:49 +08:00
return E.Cause(err, "read config")
2022-07-04 16:59:27 +08:00
}
var options option.Options
2022-09-07 13:19:57 +08:00
err = options.UnmarshalJSON(configContent)
2022-07-04 16:59:27 +08:00
if err != nil {
2022-08-13 18:36:49 +08:00
return E.Cause(err, "decode config")
2022-07-04 16:59:27 +08:00
}
buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(options)
if err != nil {
2022-08-13 18:36:49 +08:00
return E.Cause(err, "encode config")
2022-07-04 16:59:27 +08:00
}
if !commandFormatFlagWrite {
2022-07-04 17:08:28 +08:00
os.Stdout.WriteString(buffer.String() + "\n")
2022-08-13 18:36:49 +08:00
return nil
2022-07-04 16:59:27 +08:00
}
2022-07-25 16:03:21 +08:00
if bytes.Equal(configContent, buffer.Bytes()) {
2022-08-13 18:36:49 +08:00
return nil
2022-07-04 16:59:27 +08:00
}
output, err := os.Create(configPath)
if err != nil {
2022-08-13 18:36:49 +08:00
return E.Cause(err, "open output")
2022-07-04 16:59:27 +08:00
}
_, err = output.Write(buffer.Bytes())
output.Close()
if err != nil {
2022-08-13 18:36:49 +08:00
return E.Cause(err, "write output")
2022-07-04 16:59:27 +08:00
}
outputPath, _ := filepath.Abs(configPath)
2022-07-04 17:08:28 +08:00
os.Stderr.WriteString(outputPath + "\n")
2022-08-13 18:36:49 +08:00
return nil
2022-07-04 16:59:27 +08:00
}