summaryrefslogtreecommitdiff
path: root/new.go
blob: 337329e57a729f99d52a3a9139e3e8e6adc1fd37 (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
package repostatus

import (
	"os"
	"path/filepath"
	"strings"

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

var windowMap map[string]*RepoStatus

func ListAll() {
	for path, rs := range windowMap {
		log.Warn(rs.GetMasterVersion(), path)
	}
}

func NewRepoStatusWindow(path string) *RepoStatus {
	if windowMap[path] == nil {
		log.Log(INFO, "NewRepoStatusWindow() adding new", path)
	} else {
		log.Warn("This already exists yet for path", path)
		log.Warn("should return windowMap[path] here")
		return windowMap[path]
	}

	homeDir, err := os.UserHomeDir()
	if err != nil {
		log.Log(WARN, "Error getting home directory:", err)
		return nil
	}

	goSrcDir := filepath.Join(homeDir, "go/src")
	realpath := filepath.Join(goSrcDir, path)

	filename := filepath.Join(goSrcDir, path, ".git/config")

	_, err = os.Open(filename)
	if err != nil {
		log.Log(WARN, "Error reading .git/config:", filename, err)
		log.Log(WARN, "TODO: find .git/config in parent directory")
		// return nil
	}

	rs := &RepoStatus{
		ready:  false,
	}
	rs.tags = make(map[string]string)
	rs.window = gadgets.RawBasicWindow("GO Repo Details " + path)
	rs.window.Horizontal()
	rs.window.Make()
	rs.ready = true
	rs.window.Custom = func() {
		rs.Hide()
		log.Warn("repostatus user closed the window()")
	}
	rs.draw()

	// save ~/go/src & the whole path strings
	rs.path.SetValue(path)
	rs.goSrcPath.SetValue(goSrcDir)
	rs.realPath.SetValue(realpath)

	rs.readGitConfig()

	rs.readOnly.SetValue("true")
	// ignore everything else for now
	if strings.HasPrefix(path, "go.wit.com") {
		rs.readOnly.SetValue("false")
	}
	if strings.HasPrefix(path, "git.wit.org") {
		rs.readOnly.SetValue("false")
	}

	windowMap[path] = rs
	return rs
}

func init() {
	windowMap = make(map[string]*RepoStatus)
}