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

package forgepb

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

	"go.wit.com/lib/cobol"
	"go.wit.com/lib/config"
	"go.wit.com/lib/env"
	"go.wit.com/lib/fhelp"
	"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, error) {
	f := new(Forge)
	f.Config = NewForgeConfigs()
	err := f.Config.loadConfig()
	err = errors.Join(err, f.postInit())
	return f, err
}

func InitByAppname(argname string) (*Forge, error) {
	log.Info("InitByAppname() IS DEPRECATED AND DOESNT DO ANYTHING")
	return Init()
}

func InitByFullpath(filename string) (*Forge, error) {
	f := new(Forge)
	cfg := NewForgeConfigs()
	cfg.Filename = filename
	err := config.ReLoad(cfg)
	f.Config = cfg

	err = errors.Join(err, f.postInit())
	if err != nil {
		log.Info("forge.InitByFullpath() error:", filename, err)
	}
	return f, err
}

// saves the config if there have been changes
func (f *Forge) Close() error {
	if err := f.Save(); err != nil {
		return err
	}
	return nil
}

func (f *Forge) postInit() error {
	cache := "auto"
	if env.Get("cache") == "" {
		// leave "auto" by default
	} else {
		// get the users default preference
		cache = env.Get("cache")
	}

	// if cache is set to "auto", this detects if you are in a
	// a golang directory (~/go/src) or in a dir that has a "go.work" file
	if cache == "auto" {
		if godir, ok := fhelp.FindGoWork(); ok {
			f.mode = ForgeMode_GOLANG
			cache = "gowork"
			env.SetGlobal("lib/forgepb", "gopath", godir)
			fullname := filepath.Join(godir, "gowork.pb")
			env.SetGlobal("lib/forgepb", "ReposPB", fullname)
		} else {
			gopath := filepath.Join(env.Get("homedir"), "go/src")

			pwd, err := os.Getwd()
			if err == nil {
				_, err := filepath.Rel(gopath, pwd)
				if err == nil {
					f.mode = ForgeMode_GOLANG
					// user is running forge from within ~/go/src
					env.SetGlobal("lib/forgepb", "gopath", gopath)
					cache = "gosrc"
				}
			}
			// always define
			if env.Get("gopath") == "" {
				env.SetGlobal("lib/forgepb", "gopath", gopath)
			}
		}
	}
	f.configureCache(cache)

	// always define
	if env.Get("username") == "" {
		usr, _ := user.Current()
		env.SetGlobal("lib/forgepb", "username", usr.Username)
		env.Save()
	}

	// always define
	if env.Get("homeDir") == "" {
		homeDir, _ := os.UserHomeDir()
		env.SetGlobal("lib/forgepb", "homeDir", homeDir)
		env.Save()
	}

	// always define
	if env.Get("curpatches") == "" {
		env.SetGlobal("lib/forgepb", "curpatches", "curpatches.pb")
	}

	// converts the lib/ENV "mode" string
	// todo: rethink all this
	f.mode = getModeENV()

	// todo: play with these / determine good values based on user's machine
	if cobol.Int(env.Get("RillX")) == 0 {
		env.SetGlobal("lib/forgepb", "RillX", "10")
	}
	if cobol.Int(env.Get("RillY")) == 0 {
		env.SetGlobal("lib/forgepb", "RillY", "20")
		env.Save()
	}

	// create an initial repos.pb file
	// panic() here? // warning? // (probably not. it's just the repos.pb cache file
	err := f.reposCacheLoad() // loads the file from ~/.cache/forge/repos.pb
	if err == nil {
		if env.Verbose() {
			log.Printf("forge loaded %s file with len(%d) repos\n", f.Repos.Filename, f.Repos.Len())
		}
	} else {
		log.Printf("forge failed to load %s file with len(%d) repos err=(%v)\n", f.Repos.Filename, f.Repos.Len(), err)
		panic("failed to load repos.pb")
	}
	err = f.patchesCacheLoad() // loads the file from ~/.cache/forge/patches.pb
	if err == nil {
		// log.Printf("forge loaded %s file with len(%d) repos\n", f.Repos.Filename, f.Repos.Len())
	} else {
		log.Printf("forge failed to load %s file with len(%d) patches err=(%v)\n", f.Patches.Filename, f.Patches.Len(), err)
		panic("failed to load repos.pb")
	}
	return err
}