mirror of
https://github.com/mikefarah/yq.git
synced 2026-08-30 20:35:13 +08:00
Compare commits
2
Commits
ndjson
...
prettyPrint11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c28e5c58ae | ||
|
|
09e5c5b5c5 |
Executable
+173
@@ -0,0 +1,173 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
testPrettyPrintWithBooleans() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
leaveUnquoted: [yes, no, on, off, y, n, true, false]
|
||||||
|
leaveQuoted: ["yes", "no", "on", "off", "y", "n", "true", "false"]
|
||||||
|
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
leaveUnquoted:
|
||||||
|
- yes
|
||||||
|
- no
|
||||||
|
- on
|
||||||
|
- off
|
||||||
|
- y
|
||||||
|
- n
|
||||||
|
- true
|
||||||
|
- false
|
||||||
|
leaveQuoted:
|
||||||
|
- "yes"
|
||||||
|
- "no"
|
||||||
|
- "on"
|
||||||
|
- "off"
|
||||||
|
- "y"
|
||||||
|
- "n"
|
||||||
|
- "true"
|
||||||
|
- "false"
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testPrettyPrintWithBooleansCapitals() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
leaveUnquoted: [YES, NO, ON, OFF, Y, N, TRUE, FALSE]
|
||||||
|
leaveQuoted: ["YES", "NO", "ON", "OFF", "Y", "N", "TRUE", "FALSE"]
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
leaveUnquoted:
|
||||||
|
- YES
|
||||||
|
- NO
|
||||||
|
- ON
|
||||||
|
- OFF
|
||||||
|
- Y
|
||||||
|
- N
|
||||||
|
- TRUE
|
||||||
|
- FALSE
|
||||||
|
leaveQuoted:
|
||||||
|
- "YES"
|
||||||
|
- "NO"
|
||||||
|
- "ON"
|
||||||
|
- "OFF"
|
||||||
|
- "Y"
|
||||||
|
- "N"
|
||||||
|
- "TRUE"
|
||||||
|
- "FALSE"
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testPrettyPrintOtherStringValues() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
leaveUnquoted: [yesSir, hellno, bonapite]
|
||||||
|
makeUnquoted: ["yesSir", "hellno", "bonapite"]
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
leaveUnquoted:
|
||||||
|
- yesSir
|
||||||
|
- hellno
|
||||||
|
- bonapite
|
||||||
|
makeUnquoted:
|
||||||
|
- yesSir
|
||||||
|
- hellno
|
||||||
|
- bonapite
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testPrettyPrintKeys() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
"removeQuotes": "please"
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
removeQuotes: please
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testPrettyPrintOtherStringValues() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
leaveUnquoted: [yesSir, hellno, bonapite]
|
||||||
|
makeUnquoted: ["yesSir", "hellno", "bonapite"]
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
leaveUnquoted:
|
||||||
|
- yesSir
|
||||||
|
- hellno
|
||||||
|
- bonapite
|
||||||
|
makeUnquoted:
|
||||||
|
- yesSir
|
||||||
|
- hellno
|
||||||
|
- bonapite
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testPrettyPrintStringBlocks() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
"removeQuotes": |
|
||||||
|
"please"
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
removeQuotes: |
|
||||||
|
"please"
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testPrettyPrintWithExpression() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
a: {b: {c: ["cat"]}}
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
b:
|
||||||
|
c:
|
||||||
|
- cat
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e '.a' --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea '.a' --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
source ./scripts/shunit2
|
||||||
@@ -44,10 +44,11 @@ expression and prints the result in sequence.`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processExpression(expression string) string {
|
func processExpression(expression string) string {
|
||||||
|
var prettyPrintExp = `(... | (select(tag != "!!str"), select(tag == "!!str") | select(test("(?i)^(y|yes|n|no|on|off)$") | not)) ) style=""`
|
||||||
if prettyPrint && expression == "" {
|
if prettyPrint && expression == "" {
|
||||||
return `... style=""`
|
return prettyPrintExp
|
||||||
} else if prettyPrint {
|
} else if prettyPrint {
|
||||||
return fmt.Sprintf("%v | ... style= \"\"", expression)
|
return fmt.Sprintf("%v | %v", expression, prettyPrintExp)
|
||||||
}
|
}
|
||||||
return expression
|
return expression
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -1,4 +1,2 @@
|
|||||||
---
|
- ["oy", "yo", "ohno", "nom", "y", "Y", "n", "yes", "no", "on", "off", "true", "false", "apples"]
|
||||||
# hi peeps
|
- [y, n, yes, no, on, off, true, false]
|
||||||
# cool
|
|
||||||
a: test
|
|
||||||
@@ -3,6 +3,45 @@
|
|||||||
## RegEx
|
## RegEx
|
||||||
This uses golangs native regex functions under the hood - See https://github.com/google/re2/wiki/Syntax for the supported syntax.
|
This uses golangs native regex functions under the hood - See https://github.com/google/re2/wiki/Syntax for the supported syntax.
|
||||||
|
|
||||||
|
|
||||||
|
# String blocks, bash and newlines
|
||||||
|
Bash is notorious for chomping on precious trailing newline characters, making it tricky to set strings with newlines properly. In particular, the `$( exp )` _will trim trailing newlines_.
|
||||||
|
|
||||||
|
For instance to get this yaml:
|
||||||
|
|
||||||
|
```
|
||||||
|
a: |
|
||||||
|
cat
|
||||||
|
```
|
||||||
|
|
||||||
|
Using `$( exp )` wont work, as it will trim the trailing new line.
|
||||||
|
|
||||||
|
```
|
||||||
|
m=$(echo "cat\n") yq e -n '.a = strenv(m)'
|
||||||
|
a: cat
|
||||||
|
```
|
||||||
|
|
||||||
|
However, using printf works:
|
||||||
|
```
|
||||||
|
printf -v m "cat\n" ; m="$m" yq e -n '.a = strenv(m)'
|
||||||
|
a: |
|
||||||
|
cat
|
||||||
|
```
|
||||||
|
|
||||||
|
As well as having multiline expressions:
|
||||||
|
```
|
||||||
|
m="cat
|
||||||
|
" yq e -n '.a = strenv(m)'
|
||||||
|
a: |
|
||||||
|
cat
|
||||||
|
```
|
||||||
|
|
||||||
|
Similarly, if you're trying to set the content from a file, and want a trailing new line:
|
||||||
|
|
||||||
|
```
|
||||||
|
IFS= read -rd '' output < <(cat my_file)
|
||||||
|
output=$output ./yq e '.data.values = strenv(output)' first.yml
|
||||||
|
```
|
||||||
## Join strings
|
## Join strings
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
Reference in New Issue
Block a user