-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathpaths.go
More file actions
106 lines (86 loc) · 2.56 KB
/
paths.go
File metadata and controls
106 lines (86 loc) · 2.56 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
package setup
import (
"os"
"runtime"
"strings"
"time"
"github.com/hashload/boss/pkg/consts"
"github.com/hashload/boss/pkg/env"
"github.com/hashload/boss/pkg/msg"
"github.com/hashload/boss/utils"
"github.com/mattn/go-isatty"
"github.com/pterm/pterm"
)
// BuildMessage creates a message with instructions to add paths to the shell.
func BuildMessage(path []string) string {
if runtime.GOOS == "windows" {
advice := "\nTo add the path permanently, run the following command in the terminal:\n\n" +
"Press Win + R, type 'sysdm.cpl' and press Enter\n" +
"Click on the 'Advanced' tab and then on 'Environment Variables'\n" +
"In the 'System Variables' section, click on 'Path' and then on 'Edit'\n" +
"Click on 'New' and add the following path:\n" +
"\n"
for _, p := range path {
advice += p + "\n"
}
}
shellFile := ".bashrc"
shell := os.Getenv("SHELL")
if strings.HasSuffix(shell, "fish") {
shellFile = ".config/fish/config.fish"
}
if strings.HasSuffix(shell, "zsh") {
shellFile = ".zshrc"
}
pathStr := strings.Join(path, ":")
return "\nTo add the path permanently, run the following command in the terminal:\n\n" +
"echo 'export PATH=$PATH:" + pathStr + "' >> ~/" + shellFile + "\n" +
"source ~/" + shellFile + "\n"
}
// InitializePath initializes the path.
func InitializePath() {
if env.GlobalConfiguration().Advices.SetupPath {
return
}
paths := []string{
consts.EnvBossBin,
env.GetGlobalBinPath(),
env.GetGlobalEnvBpl(),
env.GetGlobalEnvDcu(),
env.GetGlobalEnvDcp(),
}
var needAdd = false
currentPath, err := os.Getwd()
if err != nil {
msg.Die("❌ Failed to load current working directory \n %s", err.Error())
return
}
splitPath := strings.Split(currentPath, ";")
for _, path := range paths {
if !utils.Contains(splitPath, path) {
splitPath = append(splitPath, path)
needAdd = true
msg.Info("📄 Adding path %s", path)
}
}
if needAdd {
newPath := strings.Join(splitPath, ";")
currentPathEnv := os.Getenv(PATH)
err := os.Setenv(PATH, currentPathEnv+";"+newPath)
if err != nil {
msg.Die("❌ Failed to update PATH \n %s", err.Error())
return
}
msg.Warn("⚠️ Please restart your console after complete.")
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
msg.Info(BuildMessage(paths))
spinner, _ := pterm.DefaultSpinner.Start("Sleeping for 5 seconds")
if spinner != nil {
time.Sleep(5 * time.Second)
_ = spinner.Stop()
}
env.GlobalConfiguration().Advices.SetupPath = true
env.GlobalConfiguration().SaveConfiguration()
}
}
}