summaryrefslogtreecommitdiff
path: root/cmds/env.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmds/env.go')
-rw-r--r--cmds/env.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/cmds/env.go b/cmds/env.go
new file mode 100644
index 0000000..5f09df6
--- /dev/null
+++ b/cmds/env.go
@@ -0,0 +1,58 @@
+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) error {
+ if len(args) == 1 || len(args) >= 3 {
+ return errors.New("usage: unset [name]")
+ }
+ os.Unsetenv(args[1])
+ return nil
+}
+
+// export: exports a key-value pair to the environment
+// in the form of `name=value`
+func export(args []string) error {
+ if len(args) == 1 || len(args) >= 3 {
+ return errors.New("usage: export [name=value]")
+ }
+ tmp := strings.Split(args[1], "=")
+ if len(tmp) != 2 {
+ return errors.New("usage: export [name=value]")
+ }
+ return os.Setenv(tmp[0], tmp[1])
+}