github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/cmd/yamlfmt/flags.go (about) 1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "flag" 19 "fmt" 20 "runtime" 21 "strings" 22 23 "github.com/google/yamlfmt" 24 "github.com/google/yamlfmt/engine" 25 ) 26 27 var ( 28 flagLint *bool = flag.Bool("lint", false, `Check if there are any differences between 29 source yaml and formatted yaml.`) 30 flagDry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting 31 operation without performing it.`) 32 flagIn *bool = flag.Bool("in", false, "Format yaml read from stdin and output to stdout") 33 flagVersion *bool = flag.Bool("version", false, "Print yamlfmt version") 34 flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path") 35 flagGlobalConf *bool = flag.Bool("global_conf", false, fmt.Sprintf("Use global yamlfmt config from %s", globalConfFlagVar())) 36 flagDisableGlobalConf *bool = flag.Bool("no_global_conf", false, fmt.Sprintf("Disabled usage of global yamlfmt config from %s", globalConfFlagVar())) 37 flagPrintConf *bool = flag.Bool("print_conf", false, "Print config") 38 flagDoublestar *bool = flag.Bool("dstar", false, "Use doublestar globs for include and exclude") 39 flagQuiet *bool = flag.Bool("quiet", false, "Print minimal output to stdout") 40 flagContinueOnError *bool = flag.Bool("continue_on_error", false, "Continue to format files that didn't fail instead of exiting with code 1.") 41 flagGitignoreExcludes *bool = flag.Bool("gitignore_excludes", false, "Use a gitignore file for excludes") 42 flagGitignorePath *string = flag.String("gitignore_path", ".gitignore", "Path to gitignore file to use") 43 flagOutputFormat *string = flag.String("output_format", "default", "The engine output format") 44 flagExclude = arrayFlag{} 45 flagFormatter = arrayFlag{} 46 flagExtensions = arrayFlag{} 47 flagDebug = arrayFlag{} 48 ) 49 50 func bindArrayFlags() { 51 flag.Var(&flagExclude, "exclude", "Paths to exclude in the chosen format (standard or doublestar)") 52 flag.Var(&flagFormatter, "formatter", "Config value overrides to pass to the formatter") 53 flag.Var(&flagExtensions, "extensions", "File extensions to use for standard path collection") 54 flag.Var(&flagDebug, "debug", "Debug codes to activate for debug logging") 55 } 56 57 type arrayFlag []string 58 59 // Implements flag.Value 60 func (a *arrayFlag) String() string { 61 return strings.Join(*a, " ") 62 } 63 64 func (a *arrayFlag) Set(value string) error { 65 values := []string{value} 66 if strings.Contains(value, ",") { 67 values = strings.Split(value, ",") 68 } 69 *a = append(*a, values...) 70 return nil 71 } 72 73 func configureHelp() { 74 flag.Usage = func() { 75 fmt.Println(`yamlfmt is a simple command line tool for formatting yaml files. 76 77 Arguments: 78 79 Glob paths to yaml files 80 Send any number of paths to yaml files specified in doublestar glob format (see: https://github.com/bmatcuk/doublestar). 81 Any flags must be specified before the paths. 82 83 - or /dev/stdin 84 Passing in a single - or /dev/stdin will read the yaml from stdin and output the formatted result to stdout 85 86 Flags:`) 87 flag.PrintDefaults() 88 } 89 } 90 91 func getOperationFromFlag() yamlfmt.Operation { 92 if *flagIn || isStdinArg() { 93 return yamlfmt.OperationStdin 94 } 95 if *flagLint { 96 return yamlfmt.OperationLint 97 } 98 if *flagDry { 99 return yamlfmt.OperationDry 100 } 101 if *flagPrintConf { 102 return yamlfmt.OperationPrintConfig 103 } 104 return yamlfmt.OperationFormat 105 } 106 107 func getOutputFormatFromFlag() engine.EngineOutputFormat { 108 return engine.EngineOutputFormat(*flagOutputFormat) 109 } 110 111 func isStdinArg() bool { 112 if len(flag.Args()) != 1 { 113 return false 114 } 115 arg := flag.Args()[0] 116 return arg == "-" || arg == "/dev/stdin" 117 } 118 119 func globalConfFlagVar() string { 120 if runtime.GOOS == "windows" { 121 return "LOCALAPPDATA" 122 } 123 return "XDG_CONFIG_HOME" 124 }