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

import (
	"os"
	"path/filepath"

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

func Init() *Forge {
	f := new(Forge)

	getwd, _ := os.Getwd()
	log.Info("forgepbb.Init() os.Getwd()", getwd)
	log.Info("forgepbb.Init() started with FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
	log.Info("forgepbb.Init() started with FORGE_GOSRC", os.Getenv("FORGE_GOSRC"))
	// TODO: rethink this but it works for now
	gosrc := os.Getenv("FORGE_GOSRC")
	if gosrc == "" {
		goSrcDir, err := f.findGoSrc()
		if err != nil {
			log.Warn("forge init() findGoSrc()", err)
		}
		os.Setenv("FORGE_GOSRC", goSrcDir)
	}
	f.goSrc = os.Getenv("FORGE_GOSRC")
	log.Info("forge.Init() using ~/go/src directory", f.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)
	}
	if f.goWorkExists() {
		f.goWork = true
	}
	log.Info("forgepbb.Init() ~/go/src ", f.goSrc)
	log.Info("forgepbb.Init() 2 FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
	log.Info("forgepbb.Init() 2 FORGE_GOSRC", os.Getenv("FORGE_GOSRC"), "f.goWork =", f.IsGoWork())

	// cache.go has Do()
	// f.initOnce.Do(f.initWork)

	f.Config = new(ForgeConfigs)

	// load the ~/.config/forge/ config
	if err := f.Config.ConfigLoad(); err != nil {
		log.Warn("forgepb.ConfigLoad() failed", err)
		os.Exit(-1)
	}

	f.Repos = new(gitpb.Repos)
	f.Repos.ConfigLoad()
	f.Machine = new(zoopb.Machine)

	if err := f.Machine.ConfigLoad(); err != nil {
		log.Warn("zoopb.ConfigLoad() failed", err)
		os.Exit(-1)
	}
	f.Machine.InitWit()

	start := f.Repos.Len()
	f.ScanGoSrc()
	end := f.Repos.Len()
	log.Info("forge.ScanGoSrc() Found", end-start, "new repos in", f.goSrc)
	testenv := os.Getenv("GO111MODULE")
	if testenv == "off" {
		log.Info("GO111MODULE=off", "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
	} else {
		log.Info("GO111MODULE=", testenv, "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
	}
	return f
}