stroy_windows.go raw
1 package main
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "syscall"
12 "time"
13
14 "github.com/davecgh/go-spew/spew"
15 "gopkg.in/src-d/go-git.v4"
16 "gopkg.in/src-d/go-git.v4/plumbing"
17 "gopkg.in/src-d/go-git.v4/plumbing/storer"
18 )
19
20 var WindowsExec = func(split []string) (out *exec.Cmd) {
21 out = exec.Command(split[0])
22 out.SysProcAttr = &syscall.SysProcAttr{}
23 out.SysProcAttr.CmdLine = strings.Join(split, " ")
24 return
25 }
26
27 func populateVersionFlags() bool {
28 // `-X 'package_path.variable_name=new_value'`
29 BuildTime = time.Now().Format(time.RFC3339)
30 var cwd string
31 var e error
32 if cwd, e = os.Getwd(); e != nil {
33 return false
34 }
35 var repo *git.Repository
36 if repo, e = git.PlainOpen(cwd); e != nil {
37 return false
38 }
39 var rr []*git.Remote
40 if rr, e = repo.Remotes(); e != nil {
41 return false
42 }
43 // spew.Dump(rr)
44 for i := range rr {
45 rs := rr[i].String()
46 if strings.HasPrefix(rs, "origin") {
47 rss := strings.Split(rs, "git@")
48 if len(rss) > 1 {
49 rsss := strings.Split(rss[1], ".git")
50 URL = strings.ReplaceAll(rsss[0], ":", "/")
51 break
52 }
53 rss = strings.Split(rs, "https://")
54 if len(rss) > 1 {
55 rsss := strings.Split(rss[1], ".git")
56 URL = rsss[0]
57 break
58 }
59
60 }
61 }
62 // var rl object.CommitIter
63 // var rbr *config.Branch
64 // if rbr, e = repo.Branch("l0k1"); e != nil {
65 // }
66 // var rbr storer.ReferenceIter
67 // if rbr, e = repo.Branches(); e != nil{
68 // return false
69 // }
70 // spew.Dump(rbr)
71 // if rl, e = repo.Log(&git.LogOptions{
72 // From: plumbing.Hash{},
73 // Order: 0,
74 // FileName: nil,
75 // All: false,
76 // }); e != nil {
77 // return false
78 // }
79 // if e = rl.ForEach(func(cmt *object.Commit) (e error) {
80 // spew.Dump(cmt)
81 // return nil
82 // }); e != nil {
83 // }
84 var rh *plumbing.Reference
85 if rh, e = repo.Head(); e != nil {
86 return false
87 }
88 rhs := rh.Strings()
89 GitRef = rhs[0]
90 GitCommit = rhs[1]
91 // fmt.Println(rhs)
92 // var rhco *object.Commit
93 // if rhco, e = repo.CommitObject(rh.Hash()); e != nil {
94 // }
95 // // var dateS string
96 // rhcoS := rhco.String()
97 // sS := strings.Split(rhcoS, "Date:")
98 // sSs := strings.TrimSpace(strings.Split(sS[1], "\n")[0])
99 // fmt.Println(sSs)
100 // var ti time.Time
101 // if ti, e = time.Parse("Mon Jan 02 15:04:05 2006 -0700", sSs); e != nil {
102 // }
103 // fmt.Printf("time %v\n", ti)
104 // fmt.Println(sSs)
105 // fmt.Println(dateS)
106 // fmt.Fprintln(os.Stderr,rh.Type(), rh.Target(), rh.Strings(), rh.String(), rh.Name())
107 // var rb storer.ReferenceIter
108 // if rb, e = repo.Branches(); e != nil {
109 // return false
110 // }
111 // if e = rb.ForEach(func(pr *plumbing.Reference) (e error) {
112 // fmt.Fprintln(os.Stderr,pr.String(), pr.Hash(), pr.Name(), pr.Strings(), pr.Target(), pr.Type())
113 // return nil
114 // }); e != nil {
115 // return false
116 // }
117 var rt storer.ReferenceIter
118 if rt, e = repo.Tags(); e != nil {
119 return false
120 }
121 // latest := time.Time{}
122 // biggest := ""
123 // allTags := []string{}
124 var maxVersion int
125 var maxString string
126 var maxIs bool
127 if e = rt.ForEach(
128 func(pr *plumbing.Reference) (e error) {
129 // var rcoh *object.Commit
130 // if rcoh, e = repo.CommitObject(pr.Hash()); e != nil {
131 // }
132 prs := strings.Split(pr.String(), "/")[2]
133 if strings.HasPrefix(prs, "v") {
134 var va [3]int
135 _, _ = fmt.Sscanf(prs, "v%d.%d.%d", &va[0], &va[1], &va[2])
136 vn := va[0]*1000000 + va[1]*1000 + va[2]
137 if maxVersion < vn {
138 maxVersion = vn
139 maxString = prs
140 }
141 if pr.Hash() == rh.Hash() {
142 maxIs = true
143 }
144 // allTags = append(allTags, prs)
145 }
146 // fmt.Println(pr.String(), pr.Hash(), pr.Name(), pr.Strings(),
147 // pr.Target(), pr.Type())
148 return nil
149 },
150 ); e != nil {
151 return false
152 }
153 if !maxIs {
154 maxString += "+"
155 }
156 // fmt.Println(maxVersion, maxString)
157 Tag = maxString
158 // txsort.Ints(versionsI)
159 // if runtime.GOOS == "windows" {
160 _, file, _, _ := runtime.Caller(0)
161 fmt.Fprintln(os.Stderr, "file", file)
162 urlSplit := strings.Split(URL, "/")
163 fmt.Fprintln(os.Stderr, "urlSplit", urlSplit)
164 baseFolder := urlSplit[len(urlSplit)-1]
165 fmt.Fprintln(os.Stderr, "baseFolder", baseFolder)
166 splitPath := strings.Split(file, baseFolder)
167 fmt.Fprintln(os.Stderr, "splitPath", splitPath)
168 PathBase := filepath.Join(splitPath[0], baseFolder) + string(filepath.Separator)
169 PathBase = strings.ReplaceAll(PathBase, "\\", "/")
170 fmt.Fprintln(os.Stderr, "PathBase", PathBase)
171 versionFile := `package version
172
173 import "fmt"
174
175 var (
176
177 // URL is the git URL for the repository
178 URL = "%s"
179 // GitRef is the gitref, as in refs/heads/branchname
180 GitRef = "%s"
181 // GitCommit is the commit hash of the current HEAD
182 GitCommit = "%s"
183 // BuildTime stores the time when the current binary was built
184 BuildTime = "%s"
185 // Tag lists the Tag on the podbuild, adding a + to the newest Tag if the commit is
186 // not that commit
187 Tag = "%s"
188 // PathBase is the path base returned from runtime caller
189 PathBase = "%s"
190 )
191
192 // Get returns a pretty printed version information string
193 func Get() string {
194 return fmt.Sprint(
195 "ParallelCoin Pod\n"+
196 " URL: "+URL+"\n",
197 " branch: "+GitRef+"\n"+
198 " commit: "+GitCommit+"\n"+
199 " built: "+BuildTime+"\n"+
200 " Tag: "+Tag+"\n",
201 )
202 }
203 `
204 versionFileOut := fmt.Sprintf(
205 versionFile,
206 URL,
207 GitRef,
208 GitCommit,
209 BuildTime,
210 Tag,
211 PathBase,
212 )
213 if e = ioutil.WriteFile("version/version.go", []byte(versionFileOut), 0666); e != nil {
214 fmt.Fprintln(os.Stderr, e)
215 }
216 // } else {
217 // ldFlags = []string{
218 // `"-X 'main.URL=` + URL + ``,
219 // `-X 'main.GitCommit=` + GitCommit + `'`,
220 // `-X 'main.BuildTime=` + BuildTime + `'`,
221 // `-X 'main.GitRef=` + GitRef + `'`,
222 // `-X 'main.Tag=` + Tag + `'"`,
223 // }
224 // }
225
226 // Infos(ldFlags)
227 return true
228 }
229
230 func main() {
231 fmt.Println(GetVersion())
232 var e error
233 var ok bool
234 var home string
235 if runtime.GOOS == "windows" {
236 var homedrive string
237 if homedrive, ok = os.LookupEnv("HOMEDRIVE"); !ok {
238 panic(e)
239 }
240 var homepath string
241 if homepath, ok = os.LookupEnv("HOMEPATH"); !ok {
242 panic(e)
243 }
244 home = homedrive + homepath
245 } else {
246 if home, ok = os.LookupEnv("HOME"); !ok {
247 panic(e)
248 }
249 }
250 if len(os.Args) > 1 {
251 folderName := "test0"
252 var datadir string
253 if len(os.Args) > 2 {
254 datadir = os.Args[2]
255 } else {
256 datadir = filepath.Join(home, folderName)
257 }
258 if list, ok := commands[os.Args[1]]; ok {
259 populateVersionFlags()
260 // Infos(list)
261 for i := range list {
262 // fmt.Fprintln(os.Stderr,list[i])
263 // inject the data directory
264 var split []string
265 out := strings.ReplaceAll(list[i], "%datadir", datadir)
266 split = strings.Split(out, " ")
267 for i := range split {
268 split[i] = strings.ReplaceAll(
269 split[i], "%ldflags",
270 fmt.Sprintf(
271 `-ldflags=%s`, strings.Join(
272 ldFlags,
273 " ",
274 ),
275 ),
276 )
277 }
278 // Infos(split)
279 // add ldflags to commands that have this
280 // for i := range split {
281 // split[i] =
282 // fmt.Fprintf(os.Stderr,"'%s'", split[i])
283 // }
284 fmt.Printf(
285 `executing item %d of list '%v' '%v' '%v'
286
287 `, i, os.Args[1],
288 split[0], split[1:],
289 )
290 cmd := WindowsExec(split)
291 cmd.Stdout = os.Stdout
292 cmd.Stdin = os.Stdin
293 cmd.Stderr = os.Stderr
294 if e := cmd.Start(); e != nil {
295 spew.Fdump(os.Stderr, e)
296 os.Exit(1)
297 }
298 if e := cmd.Wait(); e != nil {
299 os.Exit(1)
300 }
301 }
302 } else {
303 fmt.Println("command", os.Args[1], "not found")
304 }
305 } else {
306 fmt.Println("no command requested, available:")
307 for i := range commands {
308 fmt.Println(i)
309 for j := range commands[i] {
310 fmt.Println("\t" + commands[i][j])
311 }
312 }
313 fmt.Println()
314 fmt.Println(
315 "adding a second string to the commandline changes the name" +
316 " of the home folder selected in the scripts",
317 )
318 }
319 }
320
321 var (
322 URL string
323 GitRef string
324 GitCommit string
325 BuildTime string
326 Tag string
327 )
328
329 func GetVersion() string {
330 return fmt.Sprintf(
331 "app information: repo: %s branch: %s commit: %s built"+
332 ": %s tag: %s...\n", URL, GitRef, GitCommit, BuildTime, Tag,
333 )
334 }
335
336 type command struct {
337 name string
338 args []string
339 }
340
341 var ldFlags []string
342