summaryrefslogtreecommitdiff
path: root/init.go
blob: 0b9b2f0611219900daba211b791ac483fe920c38 (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
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0

package forgepb

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

	"go.wit.com/lib/config"
	"go.wit.com/lib/fhelp"
	"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 {
	engine := New()
	engine.Use(Logger(), Recovery())
	return engine.With(opts...)
}

*/

func Init() *Forge {
	f := new(Forge)
	cfg := new(ForgeConfigs)
	err := config.ConfigLoad(cfg, "forge", "forge")
	f.Config = cfg
	if err != nil {
		// fhelp.DumpENV("finit:")
		f.setenv()
		if !fhelp.QuestionUser("This is your first time using forge, use these default values?") {
			os.Exit(-1)
		}
		f.Config.InitDefaults()
		f.Config.ConfigSave()
		f.initFromConfig()
		f.Config.DumpENV()
		return f
	}
	if f.Config.Username == "" {
		usr, _ := user.Current()
		f.Config.Username = usr.Username
		f.Config.ConfigSave()
	}
	f.initFromConfig()
	if f.Config.Mode == ForgeMode_MASTER {
		log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len())
		// fhelp.DumpENV("finit:")
		f.Config.DumpENV()
	}
	return f
}

func InitByAppname(argname string) *Forge {
	cfg := new(ForgeConfigs)
	err := config.ConfigLoad(cfg, argname, "forge")
	if err != nil {
		log.Info("forge has not been configured yet", cfg.Filename)
		log.Info("go install go.wit.com/apps/forge@latest")
		os.Exit(-1)
	}
	f := new(Forge)
	f.Config = cfg
	f.initFromConfig()
	log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len())
	return f
}

func (f *Forge) initFromConfig() {
	if f.configENV() {
		log.Info("ENV changed config. todo: save config here")
		f.Config.ConfigSave()
	}
	if f.Config.ReposPB != os.Getenv("FORGE_REPOSPB") {
		// if different, use the ENV var
		// this probably means that it gets saved as the default in the config
		// we probably want that (?)
		f.Config.ReposPB = os.Getenv("FORGE_REPOSPB")
	}
	if _, s := filepath.Split(f.Config.ReposPB); s != "repos.pb" {
		fhelp.DumpENV("forge:")
		f.Config.DumpENV()
		log.Infof("ReposPB invalid filename '%s'\n", f.Config.ReposPB)
		os.Exit(-1)
	}

	f.Repos = gitpb.NewRepos()
	f.Repos.ConfigLoad(f.Config.ReposPB)

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

	// todo: play with these / determine good values based on user's machine
	if f.Config.RillX == 0 {
		f.Config.RillX = 10
	}
	if f.Config.RillY == 0 {
		f.Config.RillY = 20
	}
}

func (f *Forge) SetConfigSave(b bool) {
	config.SetChanged("forge", b)
}

// saves the config if there have been changes
func (f *Forge) Exit() {
	// log.Info("forge.configSave =", f.configSave)
	if f.Config.Mode == ForgeMode_MASTER {
		// fhelp.DumpENV("forge:")
		// f.Config.DumpENV()
		// todo: tell the user to switch to NORMAL mode
	}

	f.ConfigSave()
	if f.Repos != nil {
		if config.HasChanged("repos") {
			if err := f.Repos.ConfigSave(f.Config.ReposPB); err != nil {
				log.Info("forge.Repos.ConfigSave() error", err)
			}
		}
	}
	// log.Info("forge.Exit() ok")
	os.Exit(0)
}

// 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() {
		log.Info("doing setenv()")
		if err := fhelp.ConfigureENV(); err != nil {
			log.Info("forge ConfigureENV() failed", err)
			os.Exit(-1)
		}
		if f.Config == nil {
			log.Info("forge.Config() was nil")
			os.Exit(-1)
		}
		// f.forgeURL = os.Getenv("FORGE_URL")
		f.hostname = os.Getenv("HOSTNAME")
		if os.Getenv("FORGE_GOWORK") == "true" {
			f.goWork = true
		}

		f.Config.ReposPB = os.Getenv("FORGE_REPOSPB")
		f.Config.ReposDir = os.Getenv("FORGE_REPOSDIR")
		f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR")
		f.Config.ForgeURL = os.Getenv("FORGE_URL")
		fhelp.DumpENV("setenv end()")
	})
}

func (f *Forge) GetForgeURL() string {
	return f.Config.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.Config.ForgeURL = url
	os.Setenv("FORGE_URL", f.Config.ForgeURL)
	log.Info("Forge URL has been set to", f.Config.ForgeURL)
}

// 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) configENV() bool {
	var changed bool
	f.once.Do(func() {
		if err := fhelp.ConfigureENV(); err != nil {
			log.Info("forge ConfigureENV() failed", err)
			os.Exit(-1)
		}
		if os.Getenv("FORGE_REPOPB") != "" && f.Config.ReposPB != os.Getenv("FORGE_REPOPB") {
			log.Info("ENV: updating FORGE_REPOSPB from", f.Config.ReposPB, "to", os.Getenv("FORGE_REPOPB"))
			f.Config.ReposPB = os.Getenv("FORGE_REPOPB")
			changed = true
		}

		if os.Getenv("FORGE_GOSRC") != "" && f.Config.ReposDir != os.Getenv("FORGE_GOSRC") {
			log.Info("ENV: updating FORGE_GOSRC from", f.Config.ReposDir, "to", os.Getenv("FORGE_GOSRC"))
			f.Config.ReposDir = os.Getenv("FORGE_GOSRC")
			changed = true
		}

		if os.Getenv("FORGE_PATCHDIR") != "" && f.Config.PatchDir != os.Getenv("FORGE_PATCHDIR") {
			log.Info("ENV: updating FORGE_PATCHDIR from", f.Config.PatchDir, "to", os.Getenv("FORGE_PATCHDIRC"))
			f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR")
			changed = true
		}

		f.Config.ForgeURL = os.Getenv("FORGE_URL")
		f.hostname = os.Getenv("HOSTNAME")
		if os.Getenv("FORGE_GOWORK") == "true" {
			f.goWork = true
		}
	})
	if changed {
		// save config here
	}
	return changed
}