Skip to content

Commit

Permalink
feat: add ability to pass config path (#30)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
braydonk committed Aug 25, 2022
1 parent 79e66ea commit fc7dca4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -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)
56 changes: 36 additions & 20 deletions cmd/yamlfmt/main.go
Expand Up @@ -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"
Expand All @@ -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{}{}
Expand All @@ -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) {
Expand All @@ -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{})
}

0 comments on commit fc7dca4

Please sign in to comment.