From fc7dca4dfe33e980dc6e62ad865615390aed345c Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Wed, 24 Aug 2022 23:04:01 -0400 Subject: [PATCH] feat: add ability to pass config path (#30) * feat: add ability to pass config path This PR adds a feature I originally intended for the first version but forgot about until the opened issue. The tool now accepts a new flag `-conf` and accepts a relative or absolute path to a config file. If the flag is not present, it will fall back to the default config. * docs: Add usage for conf flag * docs: fix indentation setting --- README.md | 13 +++++++---- cmd/yamlfmt/main.go | 56 +++++++++++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c905242..70b54f4 100644 --- a/README.md +++ b/README.md @@ -57,15 +57,18 @@ If the type is not specified, the default formatter will be used. In the tool in ## Flags -The CLI supports 3 operation modes: +The CLI supports the following flags/arguments: * Format (default, no flags) - - Format and write the matched files + - Format and write the matched files * Dry run (`-dry` flag) - - Format the matched files and output the diff to `stdout` + - Format the matched files and output the diff to `stdout` * Lint (`-lint` flag) - - Format the matched files and output the diff to `stdout`, exits with status 1 if there are any differences + - Format the matched files and output the diff to `stdout`, exits with status 1 if there are any differences * Stdin (just `-` or `/dev/stdin` argument) - - Format the yaml data from `stdin` and output the result to `stdout` + - Format the yaml data from `stdin` and output the result to `stdout` +* Custom config path (`-conf` flag) + - If you would like to use a config not stored at `.yamlfmt` in the working directory, you can pass a relative or absolute path to a separate configuration file + (NOTE: If providing paths as command line arguments, the flags must be specified before any paths) diff --git a/cmd/yamlfmt/main.go b/cmd/yamlfmt/main.go index 56197bf..51b21e1 100644 --- a/cmd/yamlfmt/main.go +++ b/cmd/yamlfmt/main.go @@ -28,10 +28,11 @@ import ( ) var ( - lint *bool = flag.Bool("lint", false, `Check if there are any differences between + flagLint *bool = flag.Bool("lint", false, `Check if there are any differences between source yaml and formatted yaml.`) - dry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting + flagDry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting operation without performing it.`) + flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path") ) const defaultConfigName = ".yamlfmt" @@ -45,20 +46,14 @@ func main() { func run() error { flag.Parse() - var op command.Operation - if *lint { - op = command.OperationLint - } else if *dry { - op = command.OperationDry - } else { - op = command.OperationFormat - } + operation := getOperation() - if len(flag.Args()) == 1 && isStdin(flag.Args()[0]) { - op = command.OperationStdin + configPath, err := getConfigPath() + if err != nil { + return err } - configData, err := readDefaultConfigFile() + configData, err := readConfig(configPath) if err != nil { if errors.Is(err, os.ErrNotExist) { configData = map[string]interface{}{} @@ -71,16 +66,24 @@ func run() error { configData["include"] = flag.Args() } - return command.RunCommand(op, getFullRegistry(), configData) + return command.RunCommand(operation, getFullRegistry(), configData) } -func readDefaultConfigFile() (map[string]interface{}, error) { +func getConfigPath() (string, error) { + configPath := *flagConf + if configPath == "" { + configPath = defaultConfigName + } + + if path.IsAbs(configPath) { + return configPath, nil + } + wd, err := os.Getwd() if err != nil { - return nil, err + return "", err } - path := path.Join(wd, defaultConfigName) - return readConfig(path) + return path.Join(wd, configPath), nil } func readConfig(path string) (map[string]interface{}, error) { @@ -99,10 +102,23 @@ func readConfig(path string) (map[string]interface{}, error) { return configData, nil } -func getFullRegistry() *yamlfmt.Registry { - return yamlfmt.NewFormatterRegistry(&basic.BasicFormatterFactory{}) +func getOperation() command.Operation { + if len(flag.Args()) == 1 && isStdin(flag.Args()[0]) { + return command.OperationStdin + } + if *flagLint { + return command.OperationLint + } + if *flagDry { + return command.OperationDry + } + return command.OperationFormat } func isStdin(arg string) bool { return arg == "-" || arg == "/dev/stdin" } + +func getFullRegistry() *yamlfmt.Registry { + return yamlfmt.NewFormatterRegistry(&basic.BasicFormatterFactory{}) +}