@@ -17,137 +17,14 @@ limitations under the License.
17
17
package util
18
18
19
19
import (
20
- "bytes"
21
- "fmt"
22
- "go/format"
23
- "io/ioutil"
24
- "log"
25
20
"os"
26
- "os/exec"
27
- "path/filepath"
28
- "strings"
29
- "text/template"
30
-
31
- "github.com/gobuffalo/flect"
32
21
)
33
22
34
- // writeIfNotFound returns true if the file was created and false if it already exists
35
- func WriteIfNotFound (path , templateName , templateValue string , data interface {}) bool {
36
- // Make sure the directory exists
37
- os .MkdirAll (filepath .Dir (path ), 0700 )
38
-
39
- // Don't create the doc.go if it exists
40
- if _ , err := os .Stat (path ); err == nil {
41
- return false
42
- } else if ! os .IsNotExist (err ) {
43
- log .Fatalf ("Could not stat %s: %v" , path , err )
44
- }
45
-
46
- return Write (path , templateName , templateValue , data )
47
- }
48
-
49
- func Write (path , templateName , templateValue string , data interface {}) bool {
50
- t := template .Must (template .New (templateName ).Funcs (
51
- template.FuncMap {
52
- "title" : strings .Title ,
53
- "lower" : strings .ToLower ,
54
- "plural" : flect .Pluralize ,
55
- },
56
- ).Parse (templateValue ))
57
-
58
- var tmp bytes.Buffer
59
- err := t .Execute (& tmp , data )
60
- if err != nil {
61
- log .Fatalf ("Failed to render template %s: %v" , templateName , err )
62
- }
63
-
64
- content := tmp .Bytes ()
65
- if filepath .Ext (path ) == ".go" {
66
- content , err = format .Source (content )
67
- if err != nil {
68
- log .Fatalf ("Failed to format template %s: %v" , templateName , err )
69
- }
70
- }
71
-
72
- WriteString (path , string (content ))
73
-
74
- return true
75
- }
76
-
77
- func WriteString (path , value string ) {
78
- if _ , err := os .Stat (path ); os .IsNotExist (err ) {
79
- create (path )
80
- }
81
-
82
- f , err := os .OpenFile (path , os .O_WRONLY | os .O_TRUNC , 0 )
83
- if err != nil {
84
- log .Fatalf ("Failed to create %s: %v" , path , err )
85
- }
86
- defer f .Close ()
87
-
88
- _ , err = f .WriteString (value )
89
- if err != nil {
90
- log .Fatalf ("Failed to write %s: %v" , path , err )
91
- }
92
- }
93
-
94
- // GetCopyright will return the contents of the copyright file if it exists.
95
- // if the file cannot be read, will return the empty string.
96
- func GetCopyright (file string ) string {
97
- if len (file ) == 0 {
98
- file = filepath .Join ("hack" , "boilerplate.go.txt" )
99
- }
100
- cr , err := ioutil .ReadFile (file )
101
- if err != nil {
102
- return ""
103
- }
104
- return string (cr )
105
- }
106
-
107
- func create (path string ) {
108
- f , err := os .Create (path )
109
- if err != nil {
110
- fmt .Println (err )
111
- return
112
- }
113
- defer f .Close ()
114
- }
115
-
116
- func DoCmd (cmd string , args ... string ) {
117
- c := exec .Command (cmd , args ... )
118
- c .Stderr = os .Stderr
119
- c .Stdout = os .Stdout
120
- log .Printf ("%s\n " , strings .Join (c .Args , " " ))
121
- err := c .Run ()
122
- if err != nil {
123
- log .Fatalf ("command failed %v" , err )
124
- }
125
- }
126
-
127
- func IsNewVersion () bool {
23
+ func ProjectExist () bool {
128
24
_ , err := os .Stat ("PROJECT" )
129
25
if err != nil {
130
26
return false
131
27
}
132
28
133
29
return true
134
30
}
135
-
136
- func ProjectExist () bool {
137
- return IsNewVersion ()
138
- }
139
-
140
- func IsProjectNotInitialized () bool {
141
- dirs := []string {
142
- "cmd" ,
143
- "hack" ,
144
- "pkg" ,
145
- "vendor" ,
146
- }
147
- for _ , dir := range dirs {
148
- if _ , err := os .Stat (dir ); err == nil {
149
- return false
150
- }
151
- }
152
- return true
153
- }
0 commit comments