blob: 63ee7f6e112b8b9c7935db47a8412671df0268a3 (
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
|
package cmds
import (
"errors"
"fmt"
"os"
"strings"
"gosh/global"
)
// printEnv: prints all environment variables with shell options.
func PrintEnv() {
vars := os.Environ()
for i := range vars {
fmt.Println(vars[i])
}
if len(global.Options) > 0 {
fmt.Printf("SH_OPTS=%v\n", global.Options)
}
}
// set: sets the shell options
func Set(args []string) {
for i := 1; i < len(args); i++ {
if i-1 >= len(global.Options) {
global.Options = append(global.Options, args[i])
continue
}
global.Options[i-1] = args[i]
}
if len(args)-1 < len(global.Options) {
global.Options = global.Options[:len(args)-1]
}
}
// unset: unsets an environment variable
func Unset(args []string) (int, error) {
if len(args) == 1 || len(args) >= 3 {
return 1, errors.New("usage: unset [name]")
}
os.Unsetenv(args[1])
return 0, nil
}
// export: exports a key-value pair to the environment
// in the form of `name=value`
func Export(args []string) (int, error) {
if len(args) == 1 || len(args) >= 3 {
return 1, errors.New("usage: export [name=value]")
}
tmp := strings.Split(args[1], "=")
if len(tmp) != 2 {
return 1, errors.New("usage: export [name=value]")
}
if err := os.Setenv(tmp[0], tmp[1]); err != nil {
return 2, err
}
return 0, nil
}
|