/
util.go
129 lines (109 loc) · 2.75 KB
/
util.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package provider
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func providerShortName(n string) string {
return strings.TrimPrefix(n, "terraform-provider-")
}
func resourceShortName(name, providerName string) string {
psn := providerShortName(providerName)
return strings.TrimPrefix(name, psn+"_")
}
func copyFile(srcPath, dstPath string, mode os.FileMode) error {
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()
// If the destination file already exists, we shouldn't blow it away
dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}
func removeAllExt(file string) string {
for {
ext := filepath.Ext(file)
if ext == "" || ext == file {
return file
}
file = strings.TrimSuffix(file, ext)
}
}
// resourceName determines whether the shortname and the relFile
// are identical after file extensions have been stripped from the
// latter. This allows single word resources (e.g., http) to use
// templates (e.g., http.md.tmpl).
func resourceName(shortName, relFile string) string {
if shortName == removeAllExt(relFile) {
return shortName
}
return shortName + "_" + removeAllExt(relFile)
}
func writeFile(path string, data string) error {
dir, _ := filepath.Split(path)
err := os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("unable to make dir %q: %w", dir, err)
}
err = ioutil.WriteFile(path, []byte(data), 0644)
if err != nil {
return fmt.Errorf("unable to write file %q: %w", path, err)
}
return nil
}
func runCmd(cmd *exec.Cmd) ([]byte, error) {
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error executing %q, %v", cmd.Path, cmd.Args)
log.Printf(string(output))
return nil, fmt.Errorf("error executing %q: %w", cmd.Path, err)
}
return output, nil
}
func cp(srcDir, dstDir string) error {
err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(srcDir, srcPath)
if err != nil {
return err
}
dstPath := filepath.Join(dstDir, relPath)
switch mode := f.Mode(); {
case mode.IsDir():
if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
return err
}
case mode.IsRegular():
if err := copyFile(srcPath, dstPath, mode); err != nil {
return err
}
default:
return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath)
}
return nil
})
return err
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}