summaryrefslogtreecommitdiff
path: root/Load.go
blob: 8a5118f21a64147197c86ea119dc82fb34b927b3 (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
// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
//     go install go.wit.com/apps/autogenpb@latest
//
// This file was autogenerated with autogenpb:
//  autogenpb v0.5.25-2-g30e8de2    Built on  2025/10/16 09:16:09  (  3 m)
// Theese sort.pb.go and marshal.pb.go files are autogenerated
// The autogenpb sources have example .proto files with instructions
//

package forgepb

import (
	"errors"

	"go.wit.com/lib/ENV"
	"go.wit.com/lib/config"
	"go.wit.com/lib/protobuf/argvpb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func (pb *ForgeConfigs) loadConfig() error {
	filename, err := config.LoadByAppName(pb, "forge", "forge")
	log.Printf("%s loadConfig() about to load Config from %s\n", argvpb.GetAPPNAME(), filename)
	if err != nil {
		log.Info("couldn't load filename:", filename)
		log.Info("forge has not been configured")
		panic("config failed to load. make a new/blank forge config here?")
	}
	ENV.SetGlobal("lib/forgepb", "ForgeCfg", pb.Filename)
	var changed bool
	// migrate from the old gopath to "namespace"
	for fc := range pb.IterAll() {
		if fc.Namespace != "" {
			continue
		}
		if fc.Namespace != fc.GoPath {
			fc.Namespace = fc.GoPath
			changed = true
		}
		// todo: deprecate this
		fc.GoPath = "" // I want to do this but it might be a bad idea at this point
	}
	log.Printf("%s loadConfig() loaded Config from %s\n", argvpb.GetAPPNAME(), pb.Filename)
	if changed {
		config.SetChanged("forge", true)
	}

	// init new config here
	return err
}

func (f *Forge) reposCacheLoad() error {
	if f.Repos != nil {
		return errors.New("already loaded")
	}
	f.Repos = gitpb.NewRepos()
	err := config.LoadAppnameCache(f.Repos, "forge")
	ENV.SetGlobal("lib/forgepb", "ReposPB", f.Repos.Filename)
	return err
}

/*
func (f *Forge) loadPatchsets() error {
	f.Patchsets = NewSets()

	data, err := os.ReadFile(config.Get("PatchPB"))
	if err != nil {
		return err
	}

	err = f.Patchsets.Unmarshal(data)
	if err != nil {
		log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
		return err
	}
	// log.Infof("LoadPatchsets() found %d patches.\n", f.Patchsets.Len())
	return nil
}
*/

func makeDefaultConfig() (*ForgeConfigs, error) {
	cfg := NewForgeConfigs()
	// Get fullpath to ~/.config/forge/forge.text
	cfg.loadConfig()

	cfg.addSampleConfigs()
	ENV.PrintTable()
	var err error
	if err = cfg.saveVerbose(); 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)
}