mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-10 16:55:40 +00:00
Hook up settings for Lua output
This commit is contained in:
parent
cabb73b12c
commit
c4de13e874
@ -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. <?xml version=\"1\"?>)")
|
||||
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. <!DOCTYPE thing cat>)")
|
||||
|
||||
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 (---)")
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
17
pkg/yqlib/lua.go
Normal file
17
pkg/yqlib/lua.go
Normal file
@ -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()
|
||||
Loading…
Reference in New Issue
Block a user