forked from marcetin/parallelcoin
/
stroy.go
348 lines (331 loc) · 8.04 KB
/
stroy.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// +build !windows
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/p9c/pod/pkg/appdata"
"github.com/p9c/pod/pkg/apputil"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
)
var (
URL string
GitRef string
GitCommit string
BuildTime string
Tag string
)
type command struct {
name string
args []string
}
var ldFlags []string
func main() {
// fmt.Println(version.GetVersion())
var e error
var ok bool
var home string
if home, ok = os.LookupEnv("HOME"); !ok {
panic(e)
}
if len(os.Args) > 1 {
folderName := "test0"
var datadir string
if len(os.Args) > 2 {
datadir = os.Args[2]
} else {
datadir = filepath.Join(home, folderName)
}
if list, ok := commands[os.Args[1]]; ok {
writeVersionFile()
// Infos(list)
for i := range list {
// inf(list[i])
// inject the data directory
var split []string
out := strings.ReplaceAll(list[i], "%datadir", datadir)
split = strings.Split(out, " ")
for i := range split {
split[i] = strings.ReplaceAll(
split[i], "%ldflags",
fmt.Sprintf(
`-ldflags=%s`, strings.Join(
ldFlags,
" ",
),
),
)
}
// Infos(split)
// add ldflags to commands that have this
// for i := range split {
// split[i] =
// I.F("'%s'", split[i])
// }
fmt.Printf(
`executing item %d of list '%v' '%v' '%v'
`, i, os.Args[1],
split[0], split[1:],
)
// inf(split)
var cmd *exec.Cmd
scriptPath := filepath.Join(appdata.Dir("stroy", false), "stroy.sh")
apputil.EnsureDir(scriptPath)
if e = ioutil.WriteFile(
scriptPath,
[]byte(strings.Join(split, " ")),
0700,
); e != nil {
} else {
cmd = exec.Command("sh", scriptPath)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
}
if cmd == nil {
panic("cmd is nil")
}
var e error
if e = cmd.Start(); e != nil {
fmt.Fprintln(os.Stderr, e)
os.Exit(1)
}
if e := cmd.Wait(); e != nil {
os.Exit(1)
}
}
} else {
fmt.Println("command", os.Args[1], "not found")
}
} else {
fmt.Println("no command requested, available:")
for i := range commands {
fmt.Println(i)
for j := range commands[i] {
fmt.Println("\t" + commands[i][j])
}
}
fmt.Println()
fmt.Println(
"adding a second string to the commandline changes the name" +
" of the home folder selected in the scripts",
)
}
}
func writeVersionFile() bool {
// `-X 'package_path.variable_name=new_value'`
BuildTime = time.Now().Format(time.RFC3339)
var cwd string
var e error
if cwd, e = os.Getwd(); e != nil {
return false
}
var repo *git.Repository
if repo, e = git.PlainOpen(cwd); e != nil {
return false
}
var rr []*git.Remote
if rr, e = repo.Remotes(); e != nil {
return false
}
// spew.Dump(rr)
for i := range rr {
rs := rr[i].String()
if strings.HasPrefix(rs, "origin") {
rss := strings.Split(rs, "git@")
if len(rss) > 1 {
rsss := strings.Split(rss[1], ".git")
URL = strings.ReplaceAll(rsss[0], ":", "/")
break
}
rss = strings.Split(rs, "https://")
if len(rss) > 1 {
rsss := strings.Split(rss[1], ".git")
URL = rsss[0]
break
}
}
}
// var rl object.CommitIter
// var rbr *config.Branch
// if rbr, e = repo.Branch("l0k1"); e != nil {
// }
// var rbr storer.ReferenceIter
// if rbr, e = repo.Branches(); e != nil{
// return false
// }
// spew.Dump(rbr)
// if rl, e = repo.Log(&git.LogOptions{
// From: plumbing.Hash{},
// Order: 0,
// FileName: nil,
// All: false,
// }); e != nil {
// return false
// }
// if e = rl.ForEach(func(cmt *object.Commit) (e error) {
// spew.Dump(cmt)
// return nil
// }); e != nil {
// }
var rh *plumbing.Reference
if rh, e = repo.Head(); e != nil {
return false
}
rhs := rh.Strings()
GitRef = rhs[0]
GitCommit = rhs[1]
// fmt.Println(rhs)
// var rhco *object.Commit
// if rhco, e = repo.CommitObject(rh.Hash()); e != nil {
// }
// // var dateS string
// rhcoS := rhco.String()
// sS := strings.Split(rhcoS, "Date:")
// sSs := strings.TrimSpace(strings.Split(sS[1], "\n")[0])
// fmt.Println(sSs)
// var ti time.Time
// if ti, e = time.Parse("Mon Jan 02 15:04:05 2006 -0700", sSs); e != nil {
// }
// fmt.Printf("time %v\n", ti)
// fmt.Println(sSs)
// fmt.Println(dateS)
// inf(rh.Type(), rh.Target(), rh.Strings(), rh.String(), rh.Name())
// var rb storer.ReferenceIter
// if rb, e = repo.Branches(); e != nil {
// return false
// }
// if e = rb.ForEach(func(pr *plumbing.Reference) (e error) {
// inf(pr.String(), pr.Hash(), pr.Name(), pr.Strings(), pr.Target(), pr.Type())
// return nil
// }); e != nil {
// return false
// }
var rt storer.ReferenceIter
if rt, e = repo.Tags(); e != nil {
return false
}
// latest := time.Time{}
// biggest := ""
// allTags := []string{}
var maxVersion int
var maxString string
var maxIs bool
if e = rt.ForEach(
func(pr *plumbing.Reference) (e error) {
// var rcoh *object.Commit
// if rcoh, e = repo.CommitObject(pr.Hash()); e != nil {
// }
prs := strings.Split(pr.String(), "/")[2]
if strings.HasPrefix(prs, "v") {
var va [3]int
_, _ = fmt.Sscanf(prs, "v%d.%d.%d", &va[0], &va[1], &va[2])
vn := va[0]*1000000 + va[1]*1000 + va[2]
if maxVersion < vn {
maxVersion = vn
maxString = prs
}
if pr.Hash() == rh.Hash() {
maxIs = true
}
// allTags = append(allTags, prs)
}
// fmt.Println(pr.String(), pr.Hash(), pr.Name(), pr.Strings(),
// pr.Target(), pr.Type())
return nil
},
); e != nil {
return false
}
if !maxIs {
maxString += "+"
}
// fmt.Println(maxVersion, maxString)
Tag = maxString
// txsort.Ints(versionsI)
// if runtime.GOOS == "windows" {
_, file, _, _ := runtime.Caller(0)
fmt.Fprintln(os.Stderr, "file", file)
urlSplit := strings.Split(URL, "/")
fmt.Fprintln(os.Stderr, "urlSplit", urlSplit)
baseFolder := urlSplit[len(urlSplit)-1]
fmt.Fprintln(os.Stderr, "baseFolder", baseFolder)
splitPath := strings.Split(file, baseFolder)
fmt.Fprintln(os.Stderr, "splitPath", splitPath)
PathBase := filepath.Join(splitPath[0], baseFolder) + string(filepath.Separator)
PathBase = strings.ReplaceAll(PathBase, "\\", "\\\\")
fmt.Fprintln(os.Stderr, "PathBase", PathBase)
versionFile := `package version
import "fmt"
var (
// URL is the git URL for the repository
URL = "%s"
// GitRef is the gitref, as in refs/heads/branchname
GitRef = "%s"
// GitCommit is the commit hash of the current HEAD
GitCommit = "%s"
// BuildTime stores the time when the current binary was built
BuildTime = "%s"
// Tag lists the Tag on the build, adding a + to the newest Tag if the commit is
// not that commit
Tag = "%s"
// PathBase is the path base returned from runtime caller
PathBase = "%s"
)
// Get returns a pretty printed version information string
func Get() string {
return fmt.Sprint(
"ParallelCoin Pod\n"+
" git repository: "+URL+"\n",
" branch: "+GitRef+"\n"+
" commit: "+GitCommit+"\n"+
" built: "+BuildTime+"\n"+
" Tag: "+Tag+"\n",
)
}
`
versionFileOut := fmt.Sprintf(
versionFile,
URL,
GitRef,
GitCommit,
BuildTime,
Tag,
PathBase,
)
if e = ioutil.WriteFile("version/version.go", []byte(versionFileOut), 0666); e != nil {
fmt.Fprintln(os.Stderr, e)
}
// ldFlags = []string{
// `"-X main.URL=` + URL + ``,
// `-X main.GitCommit=` + GitCommit + ``,
// `-X main.BuildTime=` + BuildTime + ``,
// `-X main.GitRef=` + GitRef + ``,
// `-X main.Tag=` + Tag + ``,
// `-X main.PathBase=`+PathBase+`"`,
// }
// } else {
// ldFlags = []string{
// `"-X 'main.URL=` + URL + ``,
// `-X 'main.GitCommit=` + GitCommit + `'`,
// `-X 'main.BuildTime=` + BuildTime + `'`,
// `-X 'main.GitRef=` + GitRef + `'`,
// `-X 'main.Tag=` + Tag + `'"`,
// }
// }
// Infos(ldFlags)
return true
}
func GetVersion() string {
return fmt.Sprintf(
"app information: repo: %s branch: %s commit: %s built"+
": %s tag: %s...\n", URL, GitRef, GitCommit, BuildTime, Tag,
)
}