OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / hashicorp / hcl / hcl / fmtcmd / fmtcmd.go
1 // Derivative work from:
2 //      - https://golang.org/src/cmd/gofmt/gofmt.go
3 //      - https://github.com/fatih/hclfmt
4
5 package fmtcmd
6
7 import (
8         "bytes"
9         "errors"
10         "fmt"
11         "io"
12         "io/ioutil"
13         "os"
14         "os/exec"
15         "path/filepath"
16         "strings"
17
18         "github.com/hashicorp/hcl/hcl/printer"
19 )
20
21 var (
22         ErrWriteStdin = errors.New("cannot use write option with standard input")
23 )
24
25 type Options struct {
26         List  bool // list files whose formatting differs
27         Write bool // write result to (source) file instead of stdout
28         Diff  bool // display diffs of formatting changes
29 }
30
31 func isValidFile(f os.FileInfo, extensions []string) bool {
32         if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
33                 for _, ext := range extensions {
34                         if strings.HasSuffix(f.Name(), "."+ext) {
35                                 return true
36                         }
37                 }
38         }
39
40         return false
41 }
42
43 // If in == nil, the source is the contents of the file with the given filename.
44 func processFile(filename string, in io.Reader, out io.Writer, stdin bool, opts Options) error {
45         if in == nil {
46                 f, err := os.Open(filename)
47                 if err != nil {
48                         return err
49                 }
50                 defer f.Close()
51                 in = f
52         }
53
54         src, err := ioutil.ReadAll(in)
55         if err != nil {
56                 return err
57         }
58
59         res, err := printer.Format(src)
60         if err != nil {
61                 return fmt.Errorf("In %s: %s", filename, err)
62         }
63
64         if !bytes.Equal(src, res) {
65                 // formatting has changed
66                 if opts.List {
67                         fmt.Fprintln(out, filename)
68                 }
69                 if opts.Write {
70                         err = ioutil.WriteFile(filename, res, 0644)
71                         if err != nil {
72                                 return err
73                         }
74                 }
75                 if opts.Diff {
76                         data, err := diff(src, res)
77                         if err != nil {
78                                 return fmt.Errorf("computing diff: %s", err)
79                         }
80                         fmt.Fprintf(out, "diff a/%s b/%s\n", filename, filename)
81                         out.Write(data)
82                 }
83         }
84
85         if !opts.List && !opts.Write && !opts.Diff {
86                 _, err = out.Write(res)
87         }
88
89         return err
90 }
91
92 func walkDir(path string, extensions []string, stdout io.Writer, opts Options) error {
93         visitFile := func(path string, f os.FileInfo, err error) error {
94                 if err == nil && isValidFile(f, extensions) {
95                         err = processFile(path, nil, stdout, false, opts)
96                 }
97                 return err
98         }
99
100         return filepath.Walk(path, visitFile)
101 }
102
103 func Run(
104         paths, extensions []string,
105         stdin io.Reader,
106         stdout io.Writer,
107         opts Options,
108 ) error {
109         if len(paths) == 0 {
110                 if opts.Write {
111                         return ErrWriteStdin
112                 }
113                 if err := processFile("<standard input>", stdin, stdout, true, opts); err != nil {
114                         return err
115                 }
116                 return nil
117         }
118
119         for _, path := range paths {
120                 switch dir, err := os.Stat(path); {
121                 case err != nil:
122                         return err
123                 case dir.IsDir():
124                         if err := walkDir(path, extensions, stdout, opts); err != nil {
125                                 return err
126                         }
127                 default:
128                         if err := processFile(path, nil, stdout, false, opts); err != nil {
129                                 return err
130                         }
131                 }
132         }
133
134         return nil
135 }
136
137 func diff(b1, b2 []byte) (data []byte, err error) {
138         f1, err := ioutil.TempFile("", "")
139         if err != nil {
140                 return
141         }
142         defer os.Remove(f1.Name())
143         defer f1.Close()
144
145         f2, err := ioutil.TempFile("", "")
146         if err != nil {
147                 return
148         }
149         defer os.Remove(f2.Name())
150         defer f2.Close()
151
152         f1.Write(b1)
153         f2.Write(b2)
154
155         data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
156         if len(data) > 0 {
157                 // diff exits with a non-zero status when the files don't match.
158                 // Ignore that failure as long as we get output.
159                 err = nil
160         }
161         return
162 }