-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiprof.go
More file actions
339 lines (300 loc) · 9.05 KB
/
multiprof.go
File metadata and controls
339 lines (300 loc) · 9.05 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
// the initial _ needs to be there,
// otherwise we get "embed imported and not used"
_ "embed"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"text/template"
"github.com/BurntSushi/toml"
"github.com/gobwas/glob"
)
// --- Embedded Content ---
//
//go:embed help.txt
var helpText string
//go:embed default.toml
var defaultConfigToml string
//go:embed completion.template.bash
var completionTemplate string
//go:embed init.txt
var initHelpText string
// --- Constants ---
const (
configDirName = ".config/multiprof"
configFileName = "config.toml"
wrapperDirName = ".local/bin/multiprof"
completionDirName = ".local/share/bash-completion/completions"
debugEnvVar = "MULTIPROF_DEBUG"
)
// --- Global State ---
var debugMode bool
func init() {
debugMode = os.Getenv(debugEnvVar) == "1" || os.Getenv(debugEnvVar) == "true"
}
// --- Configuration Structs ---
type Config struct {
Settings Settings `toml:"settings"`
Rules []Rule `toml:"rules"`
}
type Settings struct {
Suffix string `toml:"suffix"`
}
type Rule struct {
Pattern string `toml:"pattern"`
Home string `toml:"home"`
}
// --- Main Logic ---
func main() {
log.SetFlags(0)
ownExecutable, err := os.Executable()
if err != nil {
logError("Critical error: Cannot determine own path: %v", err)
os.Exit(1)
}
calledAs := filepath.Base(os.Args[0])
ownName := filepath.Base(ownExecutable)
if calledAs == ownName || calledAs == "main" { // for go run
if len(os.Args) < 2 {
printUsage()
return
}
runManager(os.Args[1], os.Args[2:])
} else {
runWrapper()
}
}
func runManager(command string, args []string) {
switch command {
case "init":
runInit()
case "add-rule":
runAddRule(args)
case "add-wrapper":
runAddWrapper(args)
case "list":
runList()
case "help", "-h", "--help":
printUsage()
default:
logError("Unknown command '%s'. Run 'multiprof help' for a list of commands.", command)
os.Exit(1)
}
}
// --- Wrapper Execution ---
func runWrapper() {
config, _ := loadConfig()
cwd, _ := os.Getwd()
expandedCwd := expandPath(cwd)
expandedCwdWithSlash := expandedCwd + string(os.PathSeparator)
debugf("Checking match for '%s' and '%s'", expandedCwd, expandedCwdWithSlash)
var newHome string
profileMatched := false
for _, rule := range config.Rules {
expandedPattern := expandPath(rule.Pattern)
g, _ := glob.Compile(expandedPattern)
if g.Match(expandedCwd) || g.Match(expandedCwdWithSlash) {
debugf("Matched Rule with pattern: '%s'", rule.Pattern)
newHome = expandPath(rule.Home)
profileMatched = true
break
}
}
if !profileMatched {
logError("No multiprof Rule matched the current directory: %s", cwd)
logInfo("To add a Rule, run: multiprof add-rule --pattern \"%s/**\" --home \"/path/to/home\"", cwd)
os.Exit(1)
}
os.Setenv("HOME", newHome)
debugf("Set HOME to: '%s'", newHome)
wrapperName := filepath.Base(os.Args[0])
targetCmdName := strings.TrimSuffix(wrapperName, config.Settings.Suffix)
originalPath := os.Getenv("PATH")
wrapperDir, _ := getWrapperDir()
safePath := strings.ReplaceAll(originalPath, wrapperDir+":", "")
os.Setenv("PATH", safePath)
debugf("Temporarily searching for '%s' in safe PATH", targetCmdName)
targetCmdPath, err := exec.LookPath(targetCmdName)
os.Setenv("PATH", originalPath)
if err != nil {
logError("Could not find target command '%s' in the system PATH: %v", targetCmdName, err)
os.Exit(1)
}
debugf("Executing: %s", targetCmdPath)
syscall.Exec(targetCmdPath, os.Args, os.Environ())
}
// --- Management Commands ---
func runInit() {
logInfo("Running setup wizard...")
createDefaultConfig()
logSuccess("Ensured config file exists at ~/.config/multiprof/config.toml")
wrapperDir, _ := getWrapperDir()
os.MkdirAll(wrapperDir, 0755)
logSuccess("Ensured Wrapper Directory exists at ~/" + strings.TrimPrefix(wrapperDir, os.Getenv("HOME")+"/"))
tmpl, err := template.New("init").Parse(initHelpText)
if err != nil {
logError("Could not parse init template: %v", err)
return
}
data := struct{ WrapperDir string }{WrapperDir: wrapperDir}
tmpl.Execute(os.Stdout, data)
}
func runAddRule(args []string) {
addCmd := flag.NewFlagSet("add-rule", flag.ExitOnError)
patternFlag := addCmd.String("pattern", "", "Glob pattern to match a directory context.")
homeFlag := addCmd.String("home", "", "The directory to use as $HOME when the pattern matches.")
addCmd.Parse(args)
if *patternFlag == "" || *homeFlag == "" {
logError("--pattern and --home flags are required.")
addCmd.Usage()
os.Exit(1)
}
config, _ := loadConfig()
newPattern := expandPath(*patternFlag)
for _, rule := range config.Rules {
existingPattern := expandPath(rule.Pattern)
g, _ := glob.Compile(existingPattern)
if g.Match(newPattern) {
logWarn("New pattern '%s' may be shadowed by existing Rule '%s'.", *patternFlag, rule.Pattern)
logInfo("Rule priority is determined by their order in the config file.")
break
}
}
config.Rules = append(config.Rules, Rule{Pattern: *patternFlag, Home: *homeFlag})
saveConfig(config)
logSuccess("Added Rule: when in '%s', use '%s' as HOME.", *patternFlag, *homeFlag)
}
func runAddWrapper(args []string) {
if len(args) != 1 {
logError("Usage: multiprof add-wrapper <command_name>")
os.Exit(1)
}
cmdName := args[0]
config, _ := loadConfig()
wrapperName := cmdName + config.Settings.Suffix
wrapperDir, _ := getWrapperDir()
if !strings.Contains(os.Getenv("PATH"), wrapperDir) {
logWarn("Wrapper Directory '%s' not found in your $PATH.", wrapperDir)
logInfo("Please run `multiprof init` and follow the setup instructions.")
}
multiprofPath, _ := os.Executable()
symlinkPath := filepath.Join(wrapperDir, wrapperName)
if err := os.Symlink(multiprofPath, symlinkPath); err != nil {
if !os.IsExist(err) {
logError("Failed to create Wrapper: %v", err)
os.Exit(1)
}
}
logSuccess("Created Wrapper for '%s' at %s", cmdName, symlinkPath)
if config.Settings.Suffix != "" {
if err := createCompletionFile(wrapperName, cmdName); err != nil {
logWarn("Could not create completion file: %v", err)
} else {
logSuccess("Created completion file for '%s'.", wrapperName)
}
}
}
func createCompletionFile(wrapperName, originalCmd string) error {
completionDir, err := getCompletionDir()
if err != nil {
return err
}
if err := os.MkdirAll(completionDir, 0755); err != nil {
return fmt.Errorf("could not create completion directory: %w", err)
}
completionFilePath := filepath.Join(completionDir, wrapperName)
f, err := os.Create(completionFilePath)
if err != nil {
return err
}
defer f.Close()
tmpl, err := template.New("completion").Parse(completionTemplate)
if err != nil {
return err
}
data := struct {
WrapperName string
OriginalCmd string
HookName string
}{
WrapperName: wrapperName,
OriginalCmd: originalCmd,
HookName: "multiprof_hook_" + strings.ReplaceAll(wrapperName, "-", "_"),
}
return tmpl.Execute(f, data)
}
func printUsage() {
fmt.Print(helpText)
}
func runList() {
config, _ := loadConfig()
fmt.Printf("Wrapper Suffix: \"%s\"\n", config.Settings.Suffix)
fmt.Println("--- Rules (checked in order of priority) ---")
if len(config.Rules) == 0 {
fmt.Println("No Rules defined. Use 'multiprof add-rule' to create one.")
return
}
for i, rule := range config.Rules {
fmt.Printf("%d: When in '%s', use '%s' as HOME.\n", i+1, rule.Pattern, rule.Home)
}
}
// --- Helpers ---
func logInfo(format string, v ...interface{}) { fmt.Printf("[INFO] "+format+"\n", v...) }
func logSuccess(format string, v ...interface{}) { fmt.Printf("[OK] "+format+"\n", v...) }
func logWarn(format string, v ...interface{}) { fmt.Printf("[WARN] "+format+"\n", v...) }
func logError(format string, v ...interface{}) { fmt.Fprintf(os.Stderr, "[FAIL] "+format+"\n", v...) }
func debugf(format string, v ...interface{}) {
if debugMode {
log.Printf("[DEBUG] "+format, v...)
}
}
func expandPath(path string) string {
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err == nil {
path = filepath.Join(homeDir, path[1:])
}
}
return os.ExpandEnv(path)
}
func getWrapperDir() (string, error) { return expandPath(filepath.Join("~/", wrapperDirName)), nil }
func getCompletionDir() (string, error) {
return expandPath(filepath.Join("~/", completionDirName)), nil
}
func getConfigPath() (string, error) {
return expandPath(filepath.Join("~/", configDirName, configFileName)), nil
}
func createDefaultConfig() error {
configPath, _ := getConfigPath()
if _, err := os.Stat(configPath); err == nil {
return nil // File already exists
}
os.MkdirAll(filepath.Dir(configPath), 0755)
return os.WriteFile(configPath, []byte(defaultConfigToml), 0644)
}
func loadConfig() (Config, error) {
var config Config
configPath, _ := getConfigPath()
if _, err := os.Stat(configPath); os.IsNotExist(err) {
createDefaultConfig()
}
if _, err := toml.DecodeFile(configPath, &config); err != nil {
return config, err
}
return config, nil
}
func saveConfig(config Config) error {
configPath, _ := getConfigPath()
f, err := os.Create(configPath)
if err != nil {
return err
}
defer f.Close()
return toml.NewEncoder(f).Encode(config)
}