2020-01-13 09:11:56 +00:00
package cmd
import (
2020-01-19 21:42:08 +00:00
"os"
2021-12-21 05:52:54 +00:00
"github.com/mikefarah/yq/v4/pkg/yqlib"
2020-01-13 09:11:56 +00:00
"github.com/spf13/cobra"
logging "gopkg.in/op/go-logging.v1"
)
func New ( ) * cobra . Command {
var rootCmd = & cobra . Command {
Use : "yq" ,
Short : "yq is a lightweight and portable command-line YAML processor." ,
2021-07-09 04:58:09 +00:00
Long : ` yq is a portable command - line YAML processor ( https : //github.com/mikefarah/yq/)
See https : //mikefarah.gitbook.io/yq/ for detailed documentation and examples.`,
2022-01-27 01:07:41 +00:00
Example : `
# yq defaults to ' eval ' command if no command is specified . See "yq eval --help" for more examples .
2022-01-27 06:21:10 +00:00
# read the "stuff" node from "myfile.yml"
2022-03-29 02:15:27 +00:00
yq ' . stuff ' < myfile . yml
2022-01-27 06:21:10 +00:00
# update myfile . yml in place
2022-03-19 23:54:24 +00:00
yq - i ' . stuff = "foo" ' myfile . yml
# print contents of sample . json as idiomatic YAML
2022-03-19 23:55:24 +00:00
yq - P sample . json
2022-01-27 01:07:41 +00:00
` ,
2022-01-26 23:18:46 +00:00
2020-01-13 09:11:56 +00:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
if version {
cmd . Print ( GetVersionDisplay ( ) )
return nil
}
2022-01-26 23:18:46 +00:00
return evaluateSequence ( cmd , args )
2020-10-27 05:45:16 +00:00
2020-01-13 09:11:56 +00:00
} ,
PersistentPreRun : func ( cmd * cobra . Command , args [ ] string ) {
2020-09-13 01:49:55 +00:00
cmd . SetOut ( cmd . OutOrStdout ( ) )
2022-02-01 03:47:51 +00:00
2020-01-13 09:11:56 +00:00
var format = logging . MustStringFormatter (
` % { color}% { time:15:04:05} % { shortfunc} [% { level:.4s}]% { color:reset} % { message} ` ,
)
var backend = logging . AddModuleLevel (
logging . NewBackendFormatter ( logging . NewLogBackend ( os . Stderr , "" , 0 ) , format ) )
if verbose {
backend . SetLevel ( logging . DEBUG , "" )
} else {
backend . SetLevel ( logging . ERROR , "" )
}
logging . SetBackend ( backend )
2022-02-01 03:47:51 +00:00
yqlib . InitExpressionParser ( )
2022-02-07 00:55:55 +00:00
yqlib . XMLPreferences . AttributePrefix = xmlAttributePrefix
yqlib . XMLPreferences . ContentName = xmlContentName
2022-03-28 03:05:10 +00:00
yqlib . XMLPreferences . StrictMode = xmlStrictMode
2020-01-13 09:11:56 +00:00
} ,
}
rootCmd . PersistentFlags ( ) . BoolVarP ( & verbose , "verbose" , "v" , false , "verbose mode" )
2021-07-25 08:08:33 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & outputToJSON , "tojson" , "j" , false , "(deprecated) output as json. Set indent to 0 to print json in one line." )
2021-08-20 03:35:57 +00:00
err := rootCmd . PersistentFlags ( ) . MarkDeprecated ( "tojson" , "please use -o=json instead" )
2021-07-25 08:08:33 +00:00
if err != nil {
panic ( err )
}
2021-12-21 06:05:33 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & outputFormat , "output-format" , "o" , "yaml" , "[yaml|y|json|j|props|p|xml|x] output format type." )
2022-02-10 01:02:53 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & inputFormat , "input-format" , "p" , "yaml" , "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml." )
2021-12-21 04:02:07 +00:00
rootCmd . PersistentFlags ( ) . StringVar ( & xmlAttributePrefix , "xml-attribute-prefix" , "+" , "prefix for xml attributes" )
rootCmd . PersistentFlags ( ) . StringVar ( & xmlContentName , "xml-content-name" , "+content" , "name for xml content (if no attribute name is present)." )
2022-03-28 03:05:10 +00:00
rootCmd . PersistentFlags ( ) . BoolVar ( & xmlStrictMode , "xml-strict-mode" , false , "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details." )
2021-12-21 04:02:07 +00:00
2021-12-21 06:07:32 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & nullInput , "null-input" , "n" , false , "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch." )
2020-11-13 02:35:59 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & noDocSeparators , "no-doc" , "N" , false , "Don't print document separators (---)" )
2020-11-06 03:37:01 +00:00
2020-02-03 05:52:12 +00:00
rootCmd . PersistentFlags ( ) . IntVarP ( & indent , "indent" , "I" , 2 , "sets indent level for output" )
2020-01-13 09:11:56 +00:00
rootCmd . Flags ( ) . BoolVarP ( & version , "version" , "V" , false , "Print version information and quit" )
2021-12-21 06:07:32 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & writeInplace , "inplace" , "i" , false , "update the file inplace of first file given." )
2020-12-21 23:40:20 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & unwrapScalar , "unwrapScalar" , "" , true , "unwrap scalar, print the value with no quotes, colors or comments" )
2020-12-28 00:40:41 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & prettyPrint , "prettyPrint" , "P" , false , "pretty print, shorthand for '... style = \"\"'" )
2020-11-30 05:35:21 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & exitStatus , "exit-status" , "e" , false , "set exit status if there are no matches or null or false is returned" )
2020-10-27 05:45:16 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & forceColor , "colors" , "C" , false , "force print with colors" )
rootCmd . PersistentFlags ( ) . BoolVarP ( & forceNoColor , "no-colors" , "M" , false , "force print with no colors" )
2021-07-18 03:44:56 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & frontMatter , "front-matter" , "f" , "" , "(extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact" )
2022-02-06 22:27:52 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & forceExpression , "expression" , "" , "" , "forcibly set the expression argument. Useful when yq argument detection thinks your expression is a file." )
2021-12-21 06:07:32 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & leadingContentPreProcessing , "header-preprocess" , "" , true , "Slurp any header comments and separators before processing expression." )
2021-10-29 03:14:39 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & splitFileExp , "split-exp" , "s" , "" , "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter." )
2022-04-29 01:08:41 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & splitFileExpFile , "split-exp-file" , "" , "" , "Use a file to specify the split-exp expression." )
2021-10-29 03:14:39 +00:00
2022-03-01 00:50:09 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & expressionFile , "from-file" , "" , "" , "Load expression from specified file." )
2020-11-16 01:09:57 +00:00
rootCmd . AddCommand (
createEvaluateSequenceCommand ( ) ,
createEvaluateAllCommand ( ) ,
completionCmd ,
)
2020-01-13 09:11:56 +00:00
return rootCmd
}