Skip to content

Commit f5666b5

Browse files
authored
cleanup (#2)
1 parent 4ad5b3c commit f5666b5

File tree

4 files changed

+27
-58
lines changed

4 files changed

+27
-58
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/euforic/matr
22

3-
go 1.17
3+
go 1.19
44

5-
require golang.org/x/text v0.4.0
5+
require golang.org/x/text v0.8.0

go.sum

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,2 @@
1-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
4-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
5-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
7-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
8-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
9-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
10-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
11-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
16-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
17-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
18-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
19-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
20-
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
21-
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
22-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
23-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
24-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
25-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1+
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
2+
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "github.com/euforic/matr/matr"
3+
import (
4+
"github.com/euforic/matr/matr"
5+
)
46

57
func main() {
68
matr.Run()

matr/discovery.go

+20-30
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ var (
3030
// Run is the primary entrypoint to matrs cli tool.
3131
// This is where the matrfile path is resolved, compiled and executed
3232
func 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

8685
func 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

108106
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+
}
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

115116
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)
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

Comments
 (0)