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
|
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0
package forgepb
import (
"errors"
"os"
"os/user"
"path/filepath"
"go.wit.com/lib/config"
"go.wit.com/lib/gui/prep"
"go.wit.com/log"
)
// returns err1 || err2
func (f *Forge) ConfigSave() error {
if f.Config == nil {
return log.Errorf("forge.Config == nil")
}
if !config.HasChanged("forge") {
return nil
}
// only let forge edit & save the config files
// inforce this in lib config?
if !(prep.AppName() == "forge" || prep.AppName() == "guireleaser") {
log.Info("This is not forge")
return log.Errorf("Only forge can save the forge config file")
}
// migrate from the old gopath to "namespace"
for fc := range f.Config.IterAll() {
if fc.Namespace != "" {
continue
}
if fc.Namespace != fc.GoPath {
fc.Namespace = fc.GoPath
}
// todo: deprecate this
// fc.GoPath = "" // I want to do this but it might be a bad idea at this point
}
log.Info("Okay, this is", prep.AppName())
if err := f.Config.ConfigSave(); err != nil {
log.Info("forge.Config.ConfigSave() error", err)
return err
}
return nil
}
func (f *Forge) SaveRepos() error {
return f.Repos.ConfigSave(f.Config.ReposPB)
}
func (f *Forge) SetMode(newmode ForgeMode) error {
if f.Config.Mode == newmode {
// nothing changed
return nil
}
f.Config.Mode = newmode
err := f.Config.SaveVerbose()
return err
}
// functions to import and export the protobuf
// data to and from config files
// write to ~/.config/forge/
func (cfg *ForgeConfigs) ConfigSave() error {
var header string
header += "\n"
header += "# the forge config file\n"
header += "# You can customize things like:\n"
header += "#\n"
header += "# * which repos you have write access to\n"
header += "# * custom branch names for 'master', 'devel' and 'user'\n"
header += "# * 'favorites' so you can remember which things you like\n"
header += "# * sometimes protobuf TEXT can fail so as a backup this also creates a .json file\n"
header += "#\n"
header += "\n"
return config.ConfigSaveWithHeader(cfg, header)
}
func (cfg *ForgeConfigs) DumpENV() {
log.Info("s/DumpENV/DumpPB/")
cfg.DumpPB()
}
func (cfg *ForgeConfigs) DumpPB() {
log.Infof("Config.Filename = %s\n", cfg.Filename)
log.Infof("Config.ReposPB = %s\n", cfg.ReposPB)
log.Infof("Config.ReposDir = %s\n", cfg.ReposDir)
log.Infof("Config.PatchPB = %s\n", cfg.PatchPB)
log.Infof("Config.ForgeURL = %s\n", cfg.ForgeURL)
if cfg.GoWork {
log.Infof("Config.GoWork = %v\n", cfg.GoWork)
}
// log.Infof("ConfigCfgPB.Hostname=%s\n", cfg.Hostname)
}
/*
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)
}
*/
func LoadStdConfig() (*ForgeConfigs, error) {
cfg := NewForgeConfigs()
err := config.ConfigLoad(cfg, "forge", "forge")
// todo: do something here with this error?
// right now, handled in forge
// todo: move the code from forge here
if errors.Is(err, os.ErrNotExist) {
log.Info("no files existed err=", err)
} else if errors.Is(err, config.ErrEmpty) {
log.Info("files were blank err=", err)
} else if errors.Is(err, config.ErrMarshal) {
log.Info("files existed and could not be Marshalled() err=", err)
} else if err == nil {
// do nothing. probably shoulda started with this
} else {
log.Info("some other bad problem err=", err)
}
if cfg.Filename == "" {
panic("lib/config broken. filename is blank")
}
return cfg, nil
}
func MakeDefaultConfig() (*ForgeConfigs, error) {
cfg := NewForgeConfigs()
// Get fullpath to ~/.config/forge/forge.text
cfg.Filename = config.GetConfigFilename("forge", "forge")
usr, _ := user.Current()
cfg.Username = usr.Username
homeDir, _ := os.UserHomeDir()
cfgdir := filepath.Join(homeDir, ".config/forge")
cfg.HomeDir = cfgdir
os.MkdirAll(cfgdir, 0755)
cfg.ReposPB = filepath.Join(cfgdir, "repos.pb")
cfg.PatchPB = filepath.Join(cfgdir, "patches.pb")
cfg.ForgeURL = "http://forge.wit.com/"
cfg.ReposDir = filepath.Join(homeDir, "go/src") // todo: check working directory
os.MkdirAll(cfg.ReposDir, 0755)
// try new idea using lib/config
config.Set("Username", usr.Username)
config.Set("HomeDir", cfgdir)
config.Set("ReposPB", filepath.Join(cfgdir, "repos.pb"))
config.Set("PatchPB", filepath.Join(cfgdir, "patches.pb"))
config.Set("ForgeURL", "https://forge.grid.wit.com/")
config.Set("ReposDir", filepath.Join(homeDir, "go/src"))
cfg.addSampleConfigs()
cfg.DumpENV()
config.SetChanged("forge", true)
var err error
if err = cfg.ConfigSave(); err != nil {
log.Info("config save error:", err)
}
return cfg, err
}
// first time user. add go.wit.com as an example
func (cfg *ForgeConfigs) addSampleConfigs() {
newc := new(ForgeConfig)
newc.GoPath = "go.wit.com"
newc.Writable = true
newc.Directory = true
cfg.Append(newc)
newc = new(ForgeConfig)
newc.GoPath = "priv.wit.com/corp"
newc.Writable = true
newc.Private = true
newc.Directory = true
cfg.Append(newc)
}
|