Skip to content

Commit 338ba6d

Browse files
author
TP Honey
authored
Merge pull request #189 from tphoney/drone-113
(DRON-113) use ghodss/yaml for yaml printing
2 parents 8f2d782 + 01b1690 commit 338ba6d

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

drone/format/format.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"io/ioutil"
77
"os"
88

9-
"github.com/drone/drone-yaml/yaml"
10-
"github.com/drone/drone-yaml/yaml/pretty"
119
"github.com/urfave/cli"
1210
)
1311

1412
// Command exports the fmt command.
1513
var Command = cli.Command{
1614
Name: "fmt",
17-
Usage: "format the yaml file",
15+
Usage: "<deprecated. this operation is a no-op> format the yaml file",
1816
ArgsUsage: "<source>",
17+
Hidden: true,
1918
Action: format,
2019
Flags: []cli.Flag{
2120
cli.BoolFlag{
@@ -30,18 +29,11 @@ func format(c *cli.Context) error {
3029
if path == "" {
3130
path = ".drone.yml"
3231
}
33-
34-
manifest, err := yaml.ParseFile(path)
35-
if err != nil {
36-
return err
37-
}
38-
39-
buf := new(bytes.Buffer)
40-
pretty.Print(buf, manifest)
41-
32+
out, _ := ioutil.ReadFile(path)
33+
buf := bytes.NewBuffer(out)
4234
if c.Bool("save") {
4335
return ioutil.WriteFile(path, buf.Bytes(), 0644)
4436
}
45-
_, err = io.Copy(os.Stderr, buf)
37+
_, err := io.Copy(os.Stderr, buf)
4638
return err
4739
}

drone/jsonnet/jsonnet.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"os"
1010
"strings"
1111

12-
"github.com/drone/drone-yaml/yaml"
13-
"github.com/drone/drone-yaml/yaml/pretty"
1412
"github.com/fatih/color"
13+
"github.com/ghodss/yaml"
1514
"github.com/google/go-jsonnet"
1615
"github.com/urfave/cli"
1716
)
@@ -42,8 +41,9 @@ var Command = cli.Command{
4241
Usage: "Write output as a YAML stream.",
4342
},
4443
cli.BoolFlag{
45-
Name: "format",
46-
Usage: "Write output as formatted YAML",
44+
Name: "format",
45+
Hidden: true,
46+
Usage: "Write output as formatted YAML",
4747
},
4848
cli.BoolFlag{
4949
Name: "stdout",
@@ -97,31 +97,27 @@ func generate(c *cli.Context) error {
9797
return err
9898
}
9999
for _, doc := range docs {
100+
formatted, yErr := yaml.JSONToYAML([]byte(doc))
101+
if yErr != nil {
102+
return fmt.Errorf("failed to convert to YAML: %v", yErr)
103+
}
100104
buf.WriteString("---")
101105
buf.WriteString("\n")
102-
buf.WriteString(doc)
106+
buf.Write(formatted)
103107
}
104108
} else {
105109
result, err := vm.EvaluateSnippet(source, string(data))
106110
if err != nil {
107111
return err
108112
}
109-
buf.WriteString(result)
110-
}
111-
112-
// enable yaml formatting with --format
113-
if c.Bool("format") {
114-
manifest, err := yaml.Parse(buf)
115-
if err != nil {
116-
return err
113+
formatted, yErr := yaml.JSONToYAML([]byte(result))
114+
if yErr != nil {
115+
return fmt.Errorf("failed to convert to YAML: %v", yErr)
117116
}
118-
buf.Reset()
119-
pretty.Print(buf, manifest)
117+
buf.Write(formatted)
120118
}
121119

122-
// the user can optionally write the yaml to stdout. This
123-
// is useful for debugging purposes without mutating an
124-
// existing file.
120+
// the user can optionally write the yaml to stdout. This is useful for debugging purposes without mutating an existing file.
125121
if c.Bool("stdout") {
126122
io.Copy(os.Stdout, buf)
127123
return nil

drone/starlark/starlark.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"log"
1010
"os"
1111

12-
"github.com/drone/drone-yaml/yaml"
13-
"github.com/drone/drone-yaml/yaml/pretty"
14-
12+
"github.com/ghodss/yaml"
1513
"github.com/urfave/cli"
1614
"go.starlark.net/starlark"
1715
"go.starlark.net/starlarkstruct"
@@ -213,8 +211,7 @@ func generate(c *cli.Context) error {
213211
return fmt.Errorf("invalid return type (got a %s)", mainVal.Type())
214212
}
215213

216-
// if the user disables pretty printing, the yaml is printed
217-
// to the console or written to the file in json format.
214+
// if the user disables pretty printing, the yaml is printed to the console or written to the file in json format.
218215
if c.BoolT("format") == false {
219216
if c.Bool("stdout") {
220217
io.Copy(os.Stdout, buf)
@@ -223,16 +220,14 @@ func generate(c *cli.Context) error {
223220
return ioutil.WriteFile(target, buf.Bytes(), 0644)
224221
}
225222

226-
manifest, err := yaml.Parse(buf)
227-
if err != nil {
228-
return err
223+
yml, yErr := yaml.JSONToYAML(buf.Bytes())
224+
if yErr != nil {
225+
return fmt.Errorf("failed to convert to YAML: %v", yErr)
229226
}
230227
buf.Reset()
231-
pretty.Print(buf, manifest)
228+
buf.Write(yml)
232229

233-
// the user can optionally write the yaml to stdout. This
234-
// is useful for debugging purposes without mutating an
235-
// existing file.
230+
// the user can optionally write the yaml to stdout. This is useful for debugging purposes without mutating an existing file.
236231
if c.Bool("stdout") {
237232
io.Copy(os.Stdout, buf)
238233
return nil

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ require (
99
github.com/drone/drone-go v1.6.1
1010
github.com/drone/drone-runtime v1.1.1-0.20200623162453-61e33e2cab5d
1111
github.com/drone/drone-yaml v0.0.0-20190729072335-70fa398b3560
12-
github.com/drone/envsubst v1.0.1
12+
github.com/drone/envsubst v1.0.3
1313
github.com/drone/funcmap v0.0.0-20190918184546-d4ef6e88376d
1414
github.com/drone/signal v1.0.0
1515
github.com/fatih/color v1.9.0
16+
github.com/ghodss/yaml v1.0.0
1617
github.com/google/go-jsonnet v0.16.0
1718
github.com/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1
1819
github.com/joho/godotenv v1.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/drone/drone-runtime v1.1.1-0.20200623162453-61e33e2cab5d h1:P5HI/Y9hA
4040
github.com/drone/drone-runtime v1.1.1-0.20200623162453-61e33e2cab5d/go.mod h1:4/2QToW5+HGD0y1sTw7X35W1f7YINS14UfDY4isggT8=
4141
github.com/drone/drone-yaml v0.0.0-20190729072335-70fa398b3560 h1:3QL4NnDpGtaXpgI9eNd6N2k5WK8W388CzD67ZTuuZQg=
4242
github.com/drone/drone-yaml v0.0.0-20190729072335-70fa398b3560/go.mod h1:rCLISp/rqZ50s6G4nKsm971tRSzolxzqqXfgjDqPYoE=
43-
github.com/drone/envsubst v1.0.1 h1:NOOStingM2sbBwsIUeQkKUz8ShwCUzmqMxWrpXItfPE=
44-
github.com/drone/envsubst v1.0.1/go.mod h1:bkZbnc/2vh1M12Ecn7EYScpI4YGYU0etwLJICOWi8Z0=
43+
github.com/drone/envsubst v1.0.3 h1:PCIBwNDYjs50AsLZPYdfhSATKaRg/FJmDc2D6+C2x8g=
44+
github.com/drone/envsubst v1.0.3/go.mod h1:N2jZmlMufstn1KEqvbHjw40h1KyTmnVzHcSc9bFiJ2g=
4545
github.com/drone/funcmap v0.0.0-20190918184546-d4ef6e88376d h1:/IO7UVVu191Jc0DajV4cDVoO+91cuppvgxg2MZl+AXI=
4646
github.com/drone/funcmap v0.0.0-20190918184546-d4ef6e88376d/go.mod h1:Hph0/pT6ZxbujnE1Z6/08p5I0XXuOsppqF6NQlGOK0E=
4747
github.com/drone/signal v1.0.0 h1:NrnM2M/4yAuU/tXs6RP1a1ZfxnaHwYkd0kJurA1p6uI=

0 commit comments

Comments
 (0)