summaryrefslogtreecommitdiff
path: root/init.go
blob: c58629dface67bd2ecdd9f7173b3510a6830e96d (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
package forgepb

// go.wit.com/apps/autogenpb auto generates Sort() and Marshal() for protobuf files
//go:generate autogenpb --proto forgeConfig.proto
//go:generate autogenpb --proto uuid.proto
//go:generate autogenpb --proto patch.proto

import (
	"os"
	"os/user"
	"path/filepath"
	"time"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
)

// todo: use initOnce
// cache.go has Do()
// f.initOnce.Do(f.initWork)
func Init() *Forge {
	f := InitPB()

	f.Machine = new(zoopb.Machine)

	if err := f.Machine.ConfigLoad(); err != nil {
		log.Log(WARN, "zoopb.ConfigLoad() failed", err)
	}
	if f.Config.Username == "" {
		usr, _ := user.Current()
		f.Config.Username = usr.Username
		f.SetConfigSave(true)
	}
	f.Machine.InitWit()

	now := time.Now()
	start := f.Repos.Len()
	f.ScanGoSrc()
	end := f.Repos.Len()
	if (end - start) == 0 {
		log.Log(INFO, "forgepb.Scan()     Scan did not find new git repositories. Total =", end)
	} else {
		log.Log(INFO, "forgepb.Scan()     Scan found", end-start, "new git repositories. Total =", end)
	}

	f.rillUpdate(20, 10)

	if f.configSave {
		f.ConfigSave()
		f.configSave = false
	}
	log.Log(INFO, "update()           check took", shell.FormatDuration(time.Since(now)))
	return f
}

// only init's the protobuf. intended to not scan or change anything
func InitPB() *Forge {
	f := new(Forge)

	// TODO: rethink this but it works for now
	gosrc := os.Getenv("FORGE_GOSRC")
	if gosrc == "" {
		goSrcDir, err := f.findGoSrc()
		if err != nil {
			log.Log(WARN, "forge init() findGoSrc()", err)
		}
		os.Setenv("FORGE_GOSRC", goSrcDir)
	}
	f.goSrc = os.Getenv("FORGE_GOSRC")

	// also rethink this, but maybe this is the right thing to do
	if os.Getenv("FORGE_CONFIG") == "" {
		homeDir, _ := os.UserHomeDir()
		fullpath := filepath.Join(homeDir, ".config/forge")
		os.Setenv("FORGE_CONFIG", fullpath)
	}

	// check again for go.work // user could have a go.work file in ~/go/src
	if f.goWorkExists() {
		f.goWork = true
	}

	// print out the settings that will be used
	log.Log(INFO, "forgepb.Init()     FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))

	// load the ~/.config/forge/ config
	f.Config = new(ForgeConfigs)
	if err := f.Config.ConfigLoad(); err != nil {
		log.Log(WARN, "forgepb.ConfigLoad() failed", err)
	}

	if f.IsGoWork() {
		log.Log(INFO, "forgepb.Init()     FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = true)")
	} else {
		log.Log(INFO, "forgepb.Init()     FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = false)")
	}
	f.Repos = new(gitpb.Repos)
	f.Repos.ConfigLoad()
	return f
}

func (f *Forge) SetConfigSave(b bool) {
	f.configSave = b
}

// saves the config if there have been changes
func (f *Forge) Exit() {
	// log.Info("forge.configSave =", f.configSave)
	if f.configSave {
		f.ConfigSave()
	}
	// log.Info("forge.Exit() ok")
	os.Exit(0)
}