forked from harness/drone-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
47 lines (40 loc) · 787 Bytes
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package format
import (
"bytes"
"io"
"io/ioutil"
"os"
"github.com/drone/drone-yaml/yaml"
"github.com/drone/drone-yaml/yaml/pretty"
"github.com/urfave/cli"
)
// Command exports the fmt command.
var Command = cli.Command{
Name: "fmt",
Usage: "format the yaml file",
ArgsUsage: "<source>",
Action: format,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "save",
Usage: "save result to source",
},
},
}
func format(c *cli.Context) error {
path := c.Args().First()
if path == "" {
path = ".drone.yml"
}
manifest, err := yaml.ParseFile(path)
if err != nil {
return err
}
buf := new(bytes.Buffer)
pretty.Print(buf, manifest)
if c.Bool("save") {
return ioutil.WriteFile(path, buf.Bytes(), 0644)
}
_, err = io.Copy(os.Stderr, buf)
return err
}