mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Better documentation generation
This commit is contained in:
parent
af39fc737d
commit
019acfe456
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
var equalsOperatorScenarios = []expressionScenario{
|
var equalsOperatorScenarios = []expressionScenario{
|
||||||
{
|
{
|
||||||
|
description: "Match string",
|
||||||
document: `[cat,goat,dog]`,
|
document: `[cat,goat,dog]`,
|
||||||
expression: `.[] | (. == "*at")`,
|
expression: `.[] | (. == "*at")`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -14,6 +15,7 @@ var equalsOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[2], (!!bool)::false\n",
|
"D0, P[2], (!!bool)::false\n",
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
|
description: "Match number",
|
||||||
document: `[3, 4, 5]`,
|
document: `[3, 4, 5]`,
|
||||||
expression: `.[] | (. == 4)`,
|
expression: `.[] | (. == 4)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -22,6 +24,7 @@ var equalsOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[2], (!!bool)::false\n",
|
"D0, P[2], (!!bool)::false\n",
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
|
skipDoc: true,
|
||||||
document: `a: { cat: {b: apple, c: whatever}, pat: {b: banana} }`,
|
document: `a: { cat: {b: apple, c: whatever}, pat: {b: banana} }`,
|
||||||
expression: `.a | (.[].b == "apple")`,
|
expression: `.a | (.[].b == "apple")`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -30,6 +33,7 @@ var equalsOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: ``,
|
document: ``,
|
||||||
expression: `null == null`,
|
expression: `null == null`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -37,6 +41,7 @@ var equalsOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Match nulls",
|
||||||
document: ``,
|
document: ``,
|
||||||
expression: `null == ~`,
|
expression: `null == ~`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"container/list"
|
"container/list"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -55,35 +56,61 @@ func writeOrPanic(w *bufio.Writer, text string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyFromHeader(title string, out *os.File) (bool, error) {
|
||||||
|
source := fmt.Sprintf("doc/headers/%v.md", title)
|
||||||
|
_, err := os.Stat(source)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
in, err := os.Open(source) // nolint gosec
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer safelyCloseFile(in)
|
||||||
|
_, err = io.Copy(out, in)
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
|
||||||
func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) {
|
func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) {
|
||||||
f, err := os.Create(fmt.Sprintf("doc/%v.md", title))
|
f, err := os.Create(fmt.Sprintf("doc/%v.md", title))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
|
hasHeader, err := copyFromHeader(title, f)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w := bufio.NewWriter(f)
|
w := bufio.NewWriter(f)
|
||||||
writeOrPanic(w, fmt.Sprintf("# %v\n", title))
|
|
||||||
writeOrPanic(w, "## Examples\n")
|
if !hasHeader {
|
||||||
|
writeOrPanic(w, fmt.Sprintf("## %v\n", title))
|
||||||
|
}
|
||||||
|
|
||||||
for index, s := range scenarios {
|
for index, s := range scenarios {
|
||||||
if !s.skipDoc {
|
if !s.skipDoc {
|
||||||
|
|
||||||
if s.description != "" {
|
if s.description != "" {
|
||||||
writeOrPanic(w, fmt.Sprintf("### %v\n", s.description))
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||||
} else {
|
} else {
|
||||||
writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
|
writeOrPanic(w, fmt.Sprintf("## Example %v\n", index))
|
||||||
}
|
}
|
||||||
if s.document != "" {
|
if s.document != "" {
|
||||||
writeOrPanic(w, "sample.yml:\n")
|
//TODO: pretty here
|
||||||
|
writeOrPanic(w, "Given a sample.yml file of:\n")
|
||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.document))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.document))
|
||||||
}
|
}
|
||||||
if s.expression != "" {
|
if s.expression != "" {
|
||||||
writeOrPanic(w, "Expression\n")
|
writeOrPanic(w, "then\n")
|
||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression))
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression))
|
||||||
}
|
}
|
||||||
|
|
||||||
writeOrPanic(w, "Result\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
var err error
|
var err error
|
||||||
|
@ -151,23 +151,23 @@ func EvaluateFileStreamsSequence(expression string, filenames []string, printer
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// // thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
|
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
|
||||||
// func copyFileContents(src, dst string) (err error) {
|
func copyFileContents(src, dst string) (err error) {
|
||||||
// in, err := os.Open(src) // nolint gosec
|
in, err := os.Open(src) // nolint gosec
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
// defer safelyCloseFile(in)
|
defer safelyCloseFile(in)
|
||||||
// out, err := os.Create(dst)
|
out, err := os.Create(dst)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
// defer safelyCloseFile(out)
|
defer safelyCloseFile(out)
|
||||||
// if _, err = io.Copy(out, in); err != nil {
|
if _, err = io.Copy(out, in); err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
// return out.Sync()
|
return out.Sync()
|
||||||
// }
|
}
|
||||||
|
|
||||||
func safelyFlush(writer *bufio.Writer) {
|
func safelyFlush(writer *bufio.Writer) {
|
||||||
if err := writer.Flush(); err != nil {
|
if err := writer.Flush(); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user