mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-11-10 09:12:47 +08:00
cd61c2c766
* Remove deprecated io/ioutil * fixup! * fixup!
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/ditashi/jsbeautifier-go/jsbeautifier"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var _ = cmd(catUtils, func() *cli.Command {
|
|
var args FmtArgs
|
|
return &cli.Command{
|
|
Name: "fmt",
|
|
Usage: "[BETA] Format and prettify a given file",
|
|
Action: func(c *cli.Context) error {
|
|
return exit(FmtFile(args))
|
|
},
|
|
Flags: args.flags(),
|
|
}
|
|
}())
|
|
|
|
// FmtArgs stores arguments related to the fmt subcommand.
|
|
type FmtArgs struct {
|
|
InputFile string
|
|
OutputFile string
|
|
}
|
|
|
|
func (args *FmtArgs) flags() []cli.Flag {
|
|
var flags []cli.Flag
|
|
flags = append(flags, &cli.StringFlag{
|
|
Name: "input",
|
|
Aliases: []string{"i"},
|
|
Value: "dnsconfig.js",
|
|
Usage: "Input file",
|
|
Destination: &args.InputFile,
|
|
})
|
|
flags = append(flags, &cli.StringFlag{
|
|
Name: "output",
|
|
Aliases: []string{"o"},
|
|
Usage: "Output file",
|
|
Destination: &args.OutputFile,
|
|
})
|
|
return flags
|
|
}
|
|
|
|
// FmtFile reads and formats a file.
|
|
func FmtFile(args FmtArgs) error {
|
|
fileBytes, readErr := os.ReadFile(args.InputFile)
|
|
if readErr != nil {
|
|
return readErr
|
|
}
|
|
|
|
opts := jsbeautifier.DefaultOptions()
|
|
|
|
str := string(fileBytes)
|
|
beautified, beautifyErr := jsbeautifier.Beautify(&str, opts)
|
|
if beautifyErr != nil {
|
|
return beautifyErr
|
|
}
|
|
|
|
if len(beautified) != 0 && beautified[len(beautified)-1] != '\n' {
|
|
beautified = beautified + "\n"
|
|
}
|
|
|
|
if args.OutputFile == "" {
|
|
fmt.Print(beautified)
|
|
} else {
|
|
if err := os.WriteFile(args.OutputFile, []byte(beautified), 0744); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "File %s successfully written\n", args.OutputFile)
|
|
}
|
|
return nil
|
|
}
|