mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
strenv
This commit is contained in:
parent
4d8b64d05c
commit
34bc33d5c5
21
pkg/yqlib/doc/Env Variable Operators.md
Normal file
21
pkg/yqlib/doc/Env Variable Operators.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
## Read boolean environment variable as a string
|
||||||
|
Running
|
||||||
|
```bash
|
||||||
|
myenv="true" yq eval --null-input 'strenv(myenv)'
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
12
|
||||||
|
```
|
||||||
|
|
||||||
|
## Read numeric environment variable as a string
|
||||||
|
Running
|
||||||
|
```bash
|
||||||
|
myenv="12" yq eval --null-input 'strenv(myenv)'
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
12
|
||||||
|
```
|
||||||
|
|
@ -70,6 +70,7 @@ var TraverseArray = &OperationType{Type: "TRAVERSE_ARRAY", NumArgs: 1, Precedenc
|
|||||||
var DocumentFilter = &OperationType{Type: "DOCUMENT_FILTER", NumArgs: 0, Precedence: 50, Handler: TraversePathOperator}
|
var DocumentFilter = &OperationType{Type: "DOCUMENT_FILTER", NumArgs: 0, Precedence: 50, Handler: TraversePathOperator}
|
||||||
var SelfReference = &OperationType{Type: "SELF", NumArgs: 0, Precedence: 50, Handler: SelfOperator}
|
var SelfReference = &OperationType{Type: "SELF", NumArgs: 0, Precedence: 50, Handler: SelfOperator}
|
||||||
var ValueOp = &OperationType{Type: "VALUE", NumArgs: 0, Precedence: 50, Handler: ValueOperator}
|
var ValueOp = &OperationType{Type: "VALUE", NumArgs: 0, Precedence: 50, Handler: ValueOperator}
|
||||||
|
var EnvOp = &OperationType{Type: "ENV", NumArgs: 0, Precedence: 50, Handler: EnvOperator}
|
||||||
var Not = &OperationType{Type: "NOT", NumArgs: 0, Precedence: 50, Handler: NotOperator}
|
var Not = &OperationType{Type: "NOT", NumArgs: 0, Precedence: 50, Handler: NotOperator}
|
||||||
var Empty = &OperationType{Type: "EMPTY", NumArgs: 50, Handler: EmptyOperator}
|
var Empty = &OperationType{Type: "EMPTY", NumArgs: 50, Handler: EmptyOperator}
|
||||||
|
|
||||||
|
@ -27,5 +27,5 @@ func TestEnvOperatorScenarios(t *testing.T) {
|
|||||||
for _, tt := range envOperatorScenarios {
|
for _, tt := range envOperatorScenarios {
|
||||||
testScenario(t, &tt)
|
testScenario(t, &tt)
|
||||||
}
|
}
|
||||||
documentScenarios(t, "Env Variable Operators", addOperatorScenarios)
|
documentScenarios(t, "Env Variable Operators", envOperatorScenarios)
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
type expressionScenario struct {
|
type expressionScenario struct {
|
||||||
description string
|
description string
|
||||||
subdescription string
|
subdescription string
|
||||||
|
environmentVariable string
|
||||||
document string
|
document string
|
||||||
document2 string
|
document2 string
|
||||||
expression string
|
expression string
|
||||||
@ -61,6 +62,10 @@ func testScenario(t *testing.T, s *expressionScenario) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.environmentVariable != "" {
|
||||||
|
os.Setenv("myenv", s.environmentVariable)
|
||||||
|
}
|
||||||
|
|
||||||
results, err = treeNavigator.GetMatchingNodes(inputs, node)
|
results, err = treeNavigator.GetMatchingNodes(inputs, node)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -162,6 +167,13 @@ func documentInput(w *bufio.Writer, s expressionScenario) (string, string) {
|
|||||||
formattedDoc := ""
|
formattedDoc := ""
|
||||||
formattedDoc2 := ""
|
formattedDoc2 := ""
|
||||||
command := "eval"
|
command := "eval"
|
||||||
|
|
||||||
|
envCommand := ""
|
||||||
|
|
||||||
|
if(s.environmentVariable != "") {
|
||||||
|
envCommand = fmt.Sprintf("myenv=\"%v\" ", s.environmentVariable)
|
||||||
|
}
|
||||||
|
|
||||||
if s.document != "" {
|
if s.document != "" {
|
||||||
if s.dontFormatInputForDoc {
|
if s.dontFormatInputForDoc {
|
||||||
formattedDoc = s.document + "\n"
|
formattedDoc = s.document + "\n"
|
||||||
@ -188,14 +200,16 @@ func documentInput(w *bufio.Writer, s expressionScenario) (string, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeOrPanic(w, "then\n")
|
writeOrPanic(w, "then\n")
|
||||||
|
|
||||||
|
|
||||||
if s.expression != "" {
|
if s.expression != "" {
|
||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v '%v' %v\n```\n", command, s.expression, files))
|
writeOrPanic(w, fmt.Sprintf("```bash\n%vyq %v '%v' %v\n```\n", envCommand, command, s.expression, files))
|
||||||
} else {
|
} else {
|
||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v %v\n```\n", command, files))
|
writeOrPanic(w, fmt.Sprintf("```bash\n%vyq %v %v\n```\n", envCommand, command, files))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
writeOrPanic(w, "Running\n")
|
writeOrPanic(w, "Running\n")
|
||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v --null-input '%v'\n```\n", command, s.expression))
|
writeOrPanic(w, fmt.Sprintf("```bash\n%vyq %v --null-input '%v'\n```\n", envCommand, command, s.expression))
|
||||||
}
|
}
|
||||||
return formattedDoc, formattedDoc2
|
return formattedDoc, formattedDoc2
|
||||||
}
|
}
|
||||||
|
@ -178,6 +178,25 @@ func stringValue(wrapped bool) lex.Action {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func envOp(strenv bool) lex.Action {
|
||||||
|
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
|
||||||
|
value := string(m.Bytes)
|
||||||
|
|
||||||
|
if strenv {
|
||||||
|
// strenv( )
|
||||||
|
value = value[7:len(value)-1]
|
||||||
|
} else {
|
||||||
|
//env( )
|
||||||
|
value = value[4:len(value)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
envOperation := CreateValueOperation(value, value)
|
||||||
|
envOperation.OperationType = EnvOp
|
||||||
|
|
||||||
|
return &Token{TokenType: OperationToken, Operation: envOperation}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func nullValue() lex.Action {
|
func nullValue() lex.Action {
|
||||||
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
|
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
|
||||||
return &Token{TokenType: OperationToken, Operation: CreateValueOperation(nil, string(m.Bytes))}, nil
|
return &Token{TokenType: OperationToken, Operation: CreateValueOperation(nil, string(m.Bytes))}, nil
|
||||||
@ -266,6 +285,7 @@ func initLexer() (*lex.Lexer, error) {
|
|||||||
lexer.Add([]byte(`~`), nullValue())
|
lexer.Add([]byte(`~`), nullValue())
|
||||||
|
|
||||||
lexer.Add([]byte(`"[^"]*"`), stringValue(true))
|
lexer.Add([]byte(`"[^"]*"`), stringValue(true))
|
||||||
|
lexer.Add([]byte(`strenv\([^\)]+\)`), envOp(true))
|
||||||
|
|
||||||
lexer.Add([]byte(`\[`), literalToken(OpenCollect, false))
|
lexer.Add([]byte(`\[`), literalToken(OpenCollect, false))
|
||||||
lexer.Add([]byte(`\]`), literalToken(CloseCollect, true))
|
lexer.Add([]byte(`\]`), literalToken(CloseCollect, true))
|
||||||
|
Loading…
Reference in New Issue
Block a user