3030// Run is the primary entrypoint to matrs cli tool.
3131// This is where the matrfile path is resolved, compiled and executed
3232func Run () {
33- // TODO: clean up this shit show
34- // create a new flagset
3533 fs := flag .NewFlagSet ("matr" , flag .ExitOnError )
3634 fs .StringVar (& matrFilePath , "matrfile" , "./Matrfile.go" , "path to Matrfile" )
3735 fs .BoolVar (& cleanFlag , "clean" , false , "clean the matr cache" )
@@ -53,6 +51,7 @@ func Run() {
5351
5452 if helpFlag {
5553 fs .Usage ()
54+ return
5655 }
5756
5857 if versionFlag {
@@ -84,15 +83,14 @@ func clean(matrfilePath string) error {
8483}
8584
8685func parseMatrfile (path string ) ([]parser.Command , error ) {
87- var err error
8886 var cmds []parser.Command
8987
90- matrFilePath , err = filepath .Abs (matrFilePath )
88+ absPath , err : = filepath .Abs (path )
9189 if err != nil {
9290 return cmds , err
9391 }
9492
95- matrFilePath , err = getMatrfilePath (matrFilePath )
93+ matrFilePath , err : = getMatrfilePath (absPath )
9694 if err != nil {
9795 return cmds , err
9896 }
@@ -106,66 +104,61 @@ func parseMatrfile(path string) ([]parser.Command, error) {
106104}
107105
108106func 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+ }
109110 c := exec .Command (filepath .Join (matrCachePath , "matr" ), args ... )
110111 c .Stderr = os .Stderr
111112 c .Stdout = os .Stdout
112113 return c .Run ()
113114}
114115
115116func 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 )
118118 if err != nil {
119119 return "" , err
120120 }
121121
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 )
126123 if err != nil {
127124 return "" , err
128125 }
129126
130- // read the hash from the matrfileSha256 file
127+ matrCachePath := filepath .Join (filepath .Dir (absPath ), defaultCacheFolder )
128+
131129 oldHash , err := os .ReadFile (filepath .Join (matrCachePath , "matrfile.sha256" ))
132130 if err == nil && ! noCache {
133- // if the hash is the same, we can skip the build
134131 if ok := bytes .Equal (oldHash , newHash ); ok {
135132 return matrCachePath , nil
136133 }
137134 }
138135
139- // check if the cache folder exists
140136 if dir , err := os .Stat (matrCachePath ); err != nil || ! dir .IsDir () {
141137 if err := os .Mkdir (matrCachePath , 0777 ); err != nil {
142138 return "" , err
143139 }
144140 }
145141
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 {
148143 return "" , err
149144 }
150145
151146 if ! symlinkValid (matrCachePath ) {
152147 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 {
154149 if os .IsExist (err ) {
155150 return "" , err
156151 }
157152 }
158153 }
159154
160- // create the main.go file in the matr cache folder
161- // for the generated code to write to
162155 f , err := os .OpenFile (filepath .Join (matrCachePath , "main.go" ), os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0755 )
163156 if err != nil {
164157 return "" , err
165158 }
166159 defer f .Close ()
167160
168- cmds , err := parseMatrfile (matrFilePath )
161+ cmds , err := parseMatrfile (absPath )
169162 if err != nil {
170163 return "" , err
171164 }
@@ -174,7 +167,6 @@ func build(matrFilePath string, noCache bool) (string, error) {
174167 return "" , err
175168 }
176169
177- // TODO: check if we need to rebuild
178170 cmd := exec .Command ("go" , "build" , "-tags" , "matr" , "-o" , filepath .Join (matrCachePath , "matr" ),
179171 filepath .Join (matrCachePath , "Matrfile.go" ),
180172 filepath .Join (matrCachePath , "main.go" ),
@@ -189,7 +181,7 @@ func getSha256(path string) ([]byte, error) {
189181 if err != nil {
190182 return nil , err
191183 }
192-
184+ defer f . Close ()
193185 h := sha256 .New ()
194186 if _ , err := io .Copy (h , f ); err != nil {
195187 return nil , err
@@ -198,22 +190,21 @@ func getSha256(path string) ([]byte, error) {
198190 return h .Sum (nil ), nil
199191}
200192
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 )
203195 if err != nil {
204196 return "" , err
205197 }
206-
207- fp , err := os .Stat (matrFilePath )
198+ fp , err := os .Stat (absPath )
208199 if err != nil {
209- return "" , errors .New ("unable to find Matrfile: " + matrFilePath )
200+ return "" , errors .New ("unable to find Matrfile: " + absPath )
210201 }
211202
212203 if ! fp .IsDir () {
213- return matrFilePath , nil
204+ return absPath , nil
214205 }
215206
216- matrFilePath = filepath .Join (matrFilePath , "Matrfile" )
207+ matrFilePath : = filepath .Join (absPath , "Matrfile" )
217208
218209 if _ , err = os .Stat (matrFilePath + ".go" ); err == nil {
219210 return matrFilePath + ".go" , nil
@@ -231,7 +222,6 @@ func symlinkValid(path string) bool {
231222 if err != nil {
232223 return false
233224 }
234-
235225 if _ , err := os .Stat (pth ); err != nil {
236226 return false
237227 }
0 commit comments