summaryrefslogtreecommitdiff
path: root/main.go
blob: 2bc6ad2a308d8b10568478de966ee66ef91e2df6 (plain)
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
package main

import (
	"os"

	"go.wit.com/dev/alexflint/arg"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// sent via -ldflags
var VERSION string
var BUILDTIME string

var pp *arg.Parser
var forge *forgepb.Forge

var workingRepo *gitpb.Repo

func main() {
	log.Info("go-clone version", VERSION, "built on", BUILDTIME)
	pp = arg.MustParse(&argv)

	// for very new users or users unfamilar with the command line, this may help them
	if argv.Repo == "help" || argv.Repo == "?" {
		pp.WriteHelp(os.Stdout)
		os.Exit(0)
	}
	if argv.Repo == "version" {
		log.Info(argv.Version())
		os.Exit(0)
	}

	// this package helps scan git repos
	forge = forgepb.Init()

	var err error
	// attempt to clone, returns *gitpb.Repo
	workingRepo, err = clone(argv.Repo)
	if err != nil {
		badExit(err)
	}
	if argv.Recursive {
		log.Info("STARTING RECURSIVE CLONE", workingRepo.GoPath)
		if err := recursiveClone(workingRepo); err != nil {
			badExit(err)
		}
	}
	autoWork()
	if argv.Build {
		if err := build(); err != nil {
			badExit(err)
		}
	}
	if argv.Install {
		// can only install binary or plugin go packages
		if workingRepo.RepoType() == "binary" || workingRepo.RepoType() == "plugin" {
			log.Info("install will probably fail", workingRepo.GoPath, "is", workingRepo.RepoType())
		}
		if err := forge.Install(workingRepo, nil); err != nil {
			log.Warn("INSTALL FAILED", workingRepo.GoPath, err)
			badExit(err)
		}
	}

	if argv.Pull {
		// run 'git pull' if argv --git-pull
		gitPull()
	}
	okExit("")
}

func okExit(thing string) {
	if thing != "" {
		log.Info(thing, "ok")
	}
	log.Info("Finished clone on", workingRepo.GetGoPath(), "ok")
	forge.ConfigSave()
	os.Exit(0)
}

func badExit(err error) {
	log.Info("Total repositories:", forge.Repos.Len())
	log.Info("Finished go-clone with error", err, forge.GetGoSrc())
	os.Exit(-1)
}

func gitPull() {
	log.Info("Total repositories:", forge.Repos.Len())
	log.Info("Going to run git pull in each one. TODO: use rill here")
	pull := []string{"git", "pull"}

	var trycount, errcount int
	repos := forge.Repos.SortByGoPath()
	for repos.Scan() {
		repo := repos.Next()
		if argv.DryRun {
			log.Info("git pull --dry-run", repo.GoPath)
			continue
		}
		log.Info("git pull:", repo.FullPath)
		trycount += 1
		log.Info("actually run: git pull:", repo.GoPath)
		if result := shell.PathRunRealtime(repo.FullPath, pull); result.Error != nil {
			log.Info("git pull error:", result.Error)
			errcount += 1
		}
	}
	log.Info("Total repositories:", forge.Repos.Len(), "Total attempted:", trycount, "Errors:", errcount)
}

func build() error {
	err := forge.Build(workingRepo, nil)
	pwd, _ := os.Getwd()
	if err == nil {
		log.Info("this totally worked", pwd)
		shell.RunEcho([]string{"ls", "-l"})
		log.Info("ran ls")
	} else {
		log.Info("this totally did not work", pwd)
		shell.RunEcho([]string{"ls", "-l"})
		log.Info("ran ls")
		badExit(err)
	}
	return err
}

func autoWork() {
	// remake the go.work file
	if argv.AutoWork {
		log.Info("About to re-create", forge.GetGoSrc()+"/go.work")
		shell.PathRun(forge.GetGoSrc(), []string{"mv", "go.work", "go.work.last"})
		forge.MakeGoWork()
		shell.PathRun(forge.GetGoSrc(), []string{"go", "work", "use"})
		log.Info("")
		log.Info("original go.work file saved as go.work.last")
		log.Info("")
		okExit("go.work create")
	}

}