summaryrefslogtreecommitdiff
path: root/init.go
blob: 9f72a92dcddd4de3f884a7330b1f8a172f333d43 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0

package forgepb

import (
	"os"
	"os/user"
	"time"

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

/* better syntax from gin
Default returns an Engine instance with the Logger and Recovery middleware already attached.
func Default(opts ...OptionFunc) *Engine {
	debugPrintWARNINGDefault()
	engine := New()
	engine.Use(Logger(), Recovery())
	return engine.With(opts...)
}
*/

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)
	}

	if f.Config.Xterm == "" {
		f.Config.Xterm = "xterm"
		f.Config.XtermArgv = append(f.Config.XtermArgv, "-bg")
		f.Config.XtermArgv = append(f.Config.XtermArgv, "black")
		f.Config.XtermArgv = append(f.Config.XtermArgv, "-fg")
		f.Config.XtermArgv = append(f.Config.XtermArgv, "white")
		f.SetConfigSave(true)
	}

	// f.Machine.InitWit()

	if f.hasFullScan {
		// duplicate time checking below. which one to keep?
		if f.FullScanAge() > time.Minute {
			log.Log(INFO, "forgepb.Scan()     skipping scan. been run a minute ago", f.FullScanAge())
			return f
		}
	}

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

	if f.configSave {
		// taking this out to debug Marshal() panic
		f.ConfigSave()
		f.configSave = false
	}
	log.Log(INFO, "update()           check took", shell.FormatDuration(time.Since(now)))
	return f
}

func FirstTimeUser() bool {
	if checkenv() {
		return true
	}

	// setup the env
	f := new(Forge)
	f.setenv()

	f.Config = new(ForgeConfigs)
	if err := f.Config.ConfigLoad(f.configDir); err != nil {
		// no config
		return true
	}
	return false
}

func (f *Forge) InitPB() {
	f.setenv()

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

	f.Repos = gitpb.NewRepos()
	f.Repos.ConfigLoad()
	if f.Repos.HasFullScan {
		f.hasFullScan = true
	}

	// init the Patchsets
	f.Patchsets = NewPatchsets()

	// todo: play with these / determine good values based on user's machine
	f.rillX = 10
	f.rillY = 20
}

func (f *Forge) InitMachine() {
	if f.Config.Username == "" {
		usr, _ := user.Current()
		f.Config.Username = usr.Username
	}
	f.hostname, _ = os.Hostname()
	// log.Info(hostname, err)
}

// only init's the protobuf. intended to not scan or change anything
func InitPB() *Forge {
	f := new(Forge)
	f.setenv()
	f.InitPB()
	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)
}

func RawInitPB() *Forge {
	f := new(Forge)
	f.RawInitPB()
	return f
}

func (f *Forge) RawInitPB() {
	f.InitPB()
}

// the first thing done is process any ENV settings
// try to NOT use the ENV settings anywhere but here
// all initial ENV settings should be stored in the forge struct
func (f *Forge) setenv() {
	f.once.Do(func() {
		if err := fhelp.ConfigureENV(); err != nil {
			log.Info("forge ConfigureENV() failed", err)
			os.Exit(-1)
		}
		f.configDir = os.Getenv("FORGE_CONFIG")
		f.goSrc = os.Getenv("FORGE_GOSRC")
		f.repoPB = os.Getenv("FORGE_REPOPB")
		f.forgeURL = os.Getenv("FORGE_URL")
		f.patchDir = os.Getenv("FORGE_PATCHDIR")
		if os.Getenv("FORGE_GOWORK") == "true" {
			f.goWork = true
		}
	})
}

// if the env vars are set, this is probably not a new user
func checkenv() bool {
	if os.Getenv("FORGE_CONFIG") != "" {
		return true
	}
	if os.Getenv("FORGE_GOSRC") != "" {
		return true
	}
	if os.Getenv("FORGE_REPOPB") != "" {
		return true
	}
	if os.Getenv("FORGE_URL") != "" {
		return true
	}
	if os.Getenv("FORGE_PATCHDIR") != "" {
		return true
	}
	if os.Getenv("FORGE_VERBOSE") != "" {
		return true
	}
	return false
}

func (f *Forge) GetForgeURL() string {
	return f.forgeURL
}

// set the URL for forge otherwise fallback to ENV or to forge.wit.com
func (f *Forge) SetForgeURL(url string) {
	if url == "" {
		url = os.Getenv("FORGE_URL")
	}
	if url == "" {
		url = "https://forge.wit.com/"
	}
	f.forgeURL = url
	os.Setenv("FORGE_URL", f.forgeURL)
	log.Info("Forge URL has been set to", f.forgeURL)
}