diff --git a/cmd/root.go b/cmd/root.go index 96f9815d..9a15a450 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -78,6 +78,10 @@ yq -P sample.json rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. )") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. )") + rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredLuaPreferences.DocPrefix, "lua-prefix", yqlib.ConfiguredLuaPreferences.DocPrefix, "prefix") + rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredLuaPreferences.DocSuffix, "lua-suffix", yqlib.ConfiguredLuaPreferences.DocSuffix, "suffix") + rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredLuaPreferences.UnquotedKeys, "lua-unquoted-keys", yqlib.ConfiguredLuaPreferences.UnquotedKeys, "output unquoted string keys (e.g. {foo=\"bar\"})") + rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.") rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)") diff --git a/cmd/utils.go b/cmd/utils.go index b23135e8..a50f7048 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -198,7 +198,7 @@ func createEncoder(format yqlib.PrinterOutputFormat) (yqlib.Encoder, error) { case yqlib.ShellVariablesOutputFormat: return yqlib.NewShellVariablesEncoder(), nil case yqlib.LuaOutputFormat: - return yqlib.NewLuaEncoder(), nil + return yqlib.NewLuaEncoder(yqlib.ConfiguredLuaPreferences), nil } return nil, fmt.Errorf("invalid encoder: %v", format) } diff --git a/pkg/yqlib/encoder_lua.go b/pkg/yqlib/encoder_lua.go index ed0f6d48..aa44ddcf 100644 --- a/pkg/yqlib/encoder_lua.go +++ b/pkg/yqlib/encoder_lua.go @@ -18,7 +18,7 @@ func (le *luaEncoder) CanHandleAliases() bool { return false } -func NewLuaEncoder() Encoder { +func NewLuaEncoder(prefs LuaPreferences) Encoder { escape := strings.NewReplacer( "\000", "\\000", "\001", "\\001", @@ -57,7 +57,7 @@ func NewLuaEncoder() Encoder { "\\", "\\\\", "\177", "\\127", ) - return &luaEncoder{"return ", ";\n", false, escape} + return &luaEncoder{prefs.DocPrefix, prefs.DocSuffix, prefs.UnquotedKeys, escape} } func (le *luaEncoder) PrintDocumentSeparator(writer io.Writer) error { diff --git a/pkg/yqlib/lua.go b/pkg/yqlib/lua.go new file mode 100644 index 00000000..cbd8707c --- /dev/null +++ b/pkg/yqlib/lua.go @@ -0,0 +1,17 @@ +package yqlib + +type LuaPreferences struct { + DocPrefix string + DocSuffix string + UnquotedKeys bool +} + +func NewDefaultLuaPreferences() LuaPreferences { + return LuaPreferences{ + DocPrefix: "return ", + DocSuffix: ";\n", + UnquotedKeys: false, + } +} + +var ConfiguredLuaPreferences = NewDefaultLuaPreferences()