/
env.go
138 lines (124 loc) · 2.86 KB
/
env.go
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
// Copyright 2023 The Yock Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package yockc
import (
"fmt"
"os"
"strings"
"github.com/ansurfen/yock/util"
)
const (
exportOverrideWindows = `Set wshShell = CreateObject("WScript.Shell")
Set env = wshShell.Environment("User")
env("%s") = "%s"`
exportExpandWindows = `Set wshShell = CreateObject("WScript.Shell")
Set env = wshShell.Environment("User")
old = env("%s")
newVar = "%s"
if InStr(1, old, newVar, vbTextCompare) = 0 Then
old = newVar & ";" & old
End If
env("%s") = old`
exportOverridePosix = `#!/bin/bash
sed -i '/^export %s=%s/d' ~/.bashrc
echo 'export %s=%s' >> ~/.bashrc
. ~/.bashrc`
exportExpandPosix = `#!/bin/bash
sed -i '/^export %s=%s/d' ~/.bashrc
echo 'export %s=$%s:%s' >> ~/.bashrc
. ~/.bashrc`
unsetWindows = `
Set objShell = CreateObject("WScript.Shell")
objShell.Environment("User").Remove("%s")`
unsetExpandWindows = `
Set wshShell = CreateObject("WScript.Shell")
Set env = wshShell.Environment("{{ .Target }}")
old = env("{{ .Key }}")
newVar = "{{ .Value }}"
if InStr(1, old, newVar, vbTextCompare) = 1 Then
old = Replace(old, newVar+";", "")
End If
env("{{ .Key }}") = old`
unsetPosix = `#!/bin/bash
sed -i '/^export %s=/d' ~/.bashrc
. ~/.bashrc`
UnsetExpandPosix = `#!/bin/bash
sed -i '/^export %s=%s/d' ~/.bashrc
. ~/.bashrc`
)
type ExportOpt struct {
Expand bool
System bool
}
func Export(opt ExportOpt, k, v string) error {
script := ""
if strings.ToUpper(k) == "PATH" {
if util.CurPlatform.OS == "windows" {
k = "Path"
} else {
k = "PATH"
}
}
if util.CurPlatform.OS == "windows" {
if opt.Expand {
script = fmt.Sprintf(exportExpandWindows, k, v, k)
} else {
script = fmt.Sprintf(exportOverrideWindows, k, v)
}
} else {
if opt.Expand {
script = fmt.Sprintf(exportExpandPosix, k, v, k, k, v)
} else {
script = fmt.Sprintf(exportOverridePosix, k, v, k, v)
}
}
_, err := OnceScript(script)
return err
}
type UnsetOpt struct {
Expand bool
System bool
}
func Unset(opt UnsetOpt, k, v string) error {
script := ""
target := "User"
if opt.System {
target = "System"
}
if strings.ToUpper(k) == "PATH" {
if util.CurPlatform.OS == "windows" {
k = "Path"
} else {
k = "PATH"
}
}
if util.CurPlatform.OS == "windows" {
if opt.Expand {
script, _ = util.NewTemplate().OnceParse(unsetExpandWindows, map[string]string{
"Target": target,
"Key": k,
"Value": v,
})
} else {
script = fmt.Sprintf(unsetWindows, k)
}
} else {
if opt.Expand {
script = fmt.Sprintf(UnsetExpandPosix, k, v)
} else {
script = fmt.Sprintf(unsetPosix, k)
}
}
_, err := OnceScript(script)
return err
}
func ExportL(k, v string) error {
return os.Setenv(k, v)
}
func Environ(k string) string {
return os.Getenv(k)
}
func UnsetL(k string) error {
return os.Unsetenv(k)
}