summaryrefslogtreecommitdiff
path: root/repolist.go
blob: 3ee6ebed85094e793220d5b099c735ff49a0c572 (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
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
// This is a simple example
package main

import (
	"io/ioutil"
	"os/user"
	"strings"

	"go.wit.com/gui"
	"go.wit.com/lib/gadgets"
	"go.wit.com/lib/gui/repostatus"
	"go.wit.com/log"
)

func (r *repo) String() string {
	return r.status.String()
}

func (r *repo) getPath() string {
	return r.path
}

func RemoveFirstElement(slice []string) (string, []string) {
	if len(slice) == 0 {
		return "", slice // Return the original slice if it's empty
	}
	return slice[0], slice[1:] // Return the slice without the first element
}

// returns path, master branch name, devel branch name, user branch name
func splitLine(line string) (string, string, string, string) {
	var path, master, devel, user string
	parts := strings.Split(line, " ")
	path, parts = RemoveFirstElement(parts)
	master, parts = RemoveFirstElement(parts)
	devel, parts = RemoveFirstElement(parts)
	user, parts = RemoveFirstElement(parts)
	// path, master, devel, user := strings.Split(line, " ")
	return path, master, devel, user
}

func myrepolist() []string {
	content, _ := ioutil.ReadFile("/home/jcarr/.config/myrepolist")
	out := string(content)
	out = strings.TrimSpace(out)
	lines := strings.Split(out, "\n")
	return lines
}

func (r *repo) Hide() {
	r.pLabel.Hide()
	r.lastTag.Hide()
	r.vLabel.Hide()

	r.dirtyLabel.Hide()
	r.goSumStatus.Hide()
	r.statusButton.Hide()
	r.hidden = true
}

func (r *repo) Show() {
	r.pLabel.Show()
	r.lastTag.Show()
	r.vLabel.Show()

	r.dirtyLabel.Show()
	r.goSumStatus.Show()
	r.statusButton.Show()
	r.hidden = false
}

// This creates a window
func repoworld() {
	reposwin = gadgets.NewBasicWindow(me.myGui, "All git repositories in ~/go/src/")
	reposwin.Make()

	reposbox = reposwin.Box().NewBox("bw vbox", false)
	reposwin.Draw()
	reposwin.Custom = func() {
		log.Warn("GOT HERE: main() gadgets.NewBasicWindow() close")
		log.Warn("Should I do something special here?")
	}

	reposgroup = reposbox.NewGroup("go repositories (read from ~/.config/myrepolist)")
	reposgrid = reposgroup.NewGrid("test", 6, 1)

	reposgrid.NewLabel("") // path goes here

	reposgrid.NewLabel("last tag").SetProgName("last tag")
	reposgrid.NewLabel("Current Version").SetProgName("Current Version")

	reposgrid.NewLabel("Status")
	reposgrid.NewLabel("go.sum")

	reposgrid.NewLabel("Show()")

	repos := myrepolist()
	for _, line := range repos {
		log.Verbose("repo =", line)
		path, mbranch, dbranch, ubranch := splitLine(line)
		if mbranch == "" {
			mbranch = "master"
		}
		if dbranch == "" {
			dbranch = "devel"
		}
		usr, _ := user.Current()
		if ubranch == "" {
			ubranch = usr.Username
		}
		addRepo(reposgrid, path, mbranch, dbranch, ubranch)
	}

	for i, path := range repostatus.ListGitDirectories() {
		// log.Info("addRepo()", i, path)
		tmp := strings.TrimPrefix(path, "/home/jcarr/go/src/")
		log.Info("addRepo()", i, tmp)
		addRepo(reposgrid, tmp, "master", "master", "master")
	}

	reposwin.Toggle()
}

func addRepo(grid *gui.Node, path string, master string, devel string, user string) {
	_, ok := me.allrepos[path]
	if ok {
		log.Info("addRepo() already had path", path)
		return
	}

	newRepo := new(repo)

	path = strings.Trim(path, "/") // trim any extranous '/' chars put in the config file by the user
	if path == "" {
		log.Warn("addRepo() got empty path", path, master, devel, user)
		return
	}

	if repostatus.VerifyLocalGoRepo(path) {
		log.Verbose("newRepo actually exists", newRepo.getPath())
	} else {
		log.Warn("repostatus.VerifyLocalGoRepo() failed for for", path, master, devel, user)
		return
	}

	newRepo.path = path
	newRepo.pLabel = grid.NewLabel(path).SetProgName("path")

	newRepo.lastTag = grid.NewLabel("").SetProgName("lastTag")
	newRepo.vLabel = grid.NewLabel("").SetProgName("current")

	newRepo.dirtyLabel = grid.NewLabel("")
	newRepo.goSumStatus = grid.NewLabel("?")


	newRepo.statusButton = grid.NewButton("Configure", func() {
		if newRepo.status == nil {
			log.Warn("status window doesn't exist")
			return
		}
		log.Warn("status window exists. trying TestDraw() here")
		newRepo.status.Toggle()
		setCurrentRepo(newRepo, "manually chosen", "notsure")
	})

	newRepo.status = repostatus.NewRepoStatusWindow(newRepo.path)
	newRepo.hidden = false
	newRepo.status.SetMainWorkingName(master)
	newRepo.status.SetDevelWorkingName(devel)
	newRepo.status.SetUserWorkingName(user)
	me.allrepos[path] = newRepo
}