30
30
// Run is the primary entrypoint to matrs cli tool.
31
31
// This is where the matrfile path is resolved, compiled and executed
32
32
func Run () {
33
- // TODO: clean up this shit show
34
- // create a new flagset
35
33
fs := flag .NewFlagSet ("matr" , flag .ExitOnError )
36
34
fs .StringVar (& matrFilePath , "matrfile" , "./Matrfile.go" , "path to Matrfile" )
37
35
fs .BoolVar (& cleanFlag , "clean" , false , "clean the matr cache" )
@@ -53,6 +51,7 @@ func Run() {
53
51
54
52
if helpFlag {
55
53
fs .Usage ()
54
+ return
56
55
}
57
56
58
57
if versionFlag {
@@ -84,15 +83,14 @@ func clean(matrfilePath string) error {
84
83
}
85
84
86
85
func parseMatrfile (path string ) ([]parser.Command , error ) {
87
- var err error
88
86
var cmds []parser.Command
89
87
90
- matrFilePath , err = filepath .Abs (matrFilePath )
88
+ absPath , err : = filepath .Abs (path )
91
89
if err != nil {
92
90
return cmds , err
93
91
}
94
92
95
- matrFilePath , err = getMatrfilePath (matrFilePath )
93
+ matrFilePath , err : = getMatrfilePath (absPath )
96
94
if err != nil {
97
95
return cmds , err
98
96
}
@@ -106,66 +104,61 @@ func parseMatrfile(path string) ([]parser.Command, error) {
106
104
}
107
105
108
106
func run (matrCachePath string , args ... string ) error {
107
+ if _ , err := os .Stat (filepath .Join (matrCachePath , "matr" )); err != nil {
108
+ return errors .New ("matrfile has not been compiled" )
109
+ }
109
110
c := exec .Command (filepath .Join (matrCachePath , "matr" ), args ... )
110
111
c .Stderr = os .Stderr
111
112
c .Stdout = os .Stdout
112
113
return c .Run ()
113
114
}
114
115
115
116
func build (matrFilePath string , noCache bool ) (string , error ) {
116
- // get absolute path to matrfile
117
- matrFilePath , err := filepath .Abs (matrFilePath )
117
+ absPath , err := filepath .Abs (matrFilePath )
118
118
if err != nil {
119
119
return "" , err
120
120
}
121
121
122
- matrCachePath := filepath .Join (filepath .Dir (matrFilePath ), ".matr" )
123
-
124
- // check if the matrfile has changed
125
- newHash , err := getSha256 (matrFilePath )
122
+ newHash , err := getSha256 (absPath )
126
123
if err != nil {
127
124
return "" , err
128
125
}
129
126
130
- // read the hash from the matrfileSha256 file
127
+ matrCachePath := filepath .Join (filepath .Dir (absPath ), defaultCacheFolder )
128
+
131
129
oldHash , err := os .ReadFile (filepath .Join (matrCachePath , "matrfile.sha256" ))
132
130
if err == nil && ! noCache {
133
- // if the hash is the same, we can skip the build
134
131
if ok := bytes .Equal (oldHash , newHash ); ok {
135
132
return matrCachePath , nil
136
133
}
137
134
}
138
135
139
- // check if the cache folder exists
140
136
if dir , err := os .Stat (matrCachePath ); err != nil || ! dir .IsDir () {
141
137
if err := os .Mkdir (matrCachePath , 0777 ); err != nil {
142
138
return "" , err
143
139
}
144
140
}
145
141
146
- // if the file doesn't exist, create it
147
- if err := os .WriteFile (filepath .Join (matrCachePath , "matrfile.sha256" ), []byte (newHash ), 0644 ); err != nil {
142
+ if err := os .WriteFile (filepath .Join (matrCachePath , "matrfile.sha256" ), newHash , 0644 ); err != nil {
148
143
return "" , err
149
144
}
150
145
151
146
if ! symlinkValid (matrCachePath ) {
152
147
os .Remove (filepath .Join (matrCachePath , defaultMatrFile ))
153
- if err := os .Symlink (matrFilePath , filepath .Join (matrCachePath , defaultMatrFile )); err != nil {
148
+ if err := os .Symlink (absPath , filepath .Join (matrCachePath , defaultMatrFile )); err != nil {
154
149
if os .IsExist (err ) {
155
150
return "" , err
156
151
}
157
152
}
158
153
}
159
154
160
- // create the main.go file in the matr cache folder
161
- // for the generated code to write to
162
155
f , err := os .OpenFile (filepath .Join (matrCachePath , "main.go" ), os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0755 )
163
156
if err != nil {
164
157
return "" , err
165
158
}
166
159
defer f .Close ()
167
160
168
- cmds , err := parseMatrfile (matrFilePath )
161
+ cmds , err := parseMatrfile (absPath )
169
162
if err != nil {
170
163
return "" , err
171
164
}
@@ -174,7 +167,6 @@ func build(matrFilePath string, noCache bool) (string, error) {
174
167
return "" , err
175
168
}
176
169
177
- // TODO: check if we need to rebuild
178
170
cmd := exec .Command ("go" , "build" , "-tags" , "matr" , "-o" , filepath .Join (matrCachePath , "matr" ),
179
171
filepath .Join (matrCachePath , "Matrfile.go" ),
180
172
filepath .Join (matrCachePath , "main.go" ),
@@ -189,7 +181,7 @@ func getSha256(path string) ([]byte, error) {
189
181
if err != nil {
190
182
return nil , err
191
183
}
192
-
184
+ defer f . Close ()
193
185
h := sha256 .New ()
194
186
if _ , err := io .Copy (h , f ); err != nil {
195
187
return nil , err
@@ -198,22 +190,21 @@ func getSha256(path string) ([]byte, error) {
198
190
return h .Sum (nil ), nil
199
191
}
200
192
201
- func getMatrfilePath (matrFilePath string ) (string , error ) {
202
- matrFilePath , err := filepath .Abs (matrFilePath )
193
+ func getMatrfilePath (mfpath string ) (string , error ) {
194
+ absPath , err := filepath .Abs (mfpath )
203
195
if err != nil {
204
196
return "" , err
205
197
}
206
-
207
- fp , err := os .Stat (matrFilePath )
198
+ fp , err := os .Stat (absPath )
208
199
if err != nil {
209
- return "" , errors .New ("unable to find Matrfile: " + matrFilePath )
200
+ return "" , errors .New ("unable to find Matrfile: " + absPath )
210
201
}
211
202
212
203
if ! fp .IsDir () {
213
- return matrFilePath , nil
204
+ return absPath , nil
214
205
}
215
206
216
- matrFilePath = filepath .Join (matrFilePath , "Matrfile" )
207
+ matrFilePath : = filepath .Join (absPath , "Matrfile" )
217
208
218
209
if _ , err = os .Stat (matrFilePath + ".go" ); err == nil {
219
210
return matrFilePath + ".go" , nil
@@ -231,7 +222,6 @@ func symlinkValid(path string) bool {
231
222
if err != nil {
232
223
return false
233
224
}
234
-
235
225
if _ , err := os .Stat (pth ); err != nil {
236
226
return false
237
227
}
0 commit comments