summaryrefslogtreecommitdiff
path: root/cmds/cmds.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmds/cmds.go')
-rw-r--r--cmds/cmds.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/cmds/cmds.go b/cmds/cmds.go
new file mode 100644
index 0000000..f61f2b8
--- /dev/null
+++ b/cmds/cmds.go
@@ -0,0 +1,53 @@
+package cmds
+
+import (
+ "errors"
+ "os"
+ "strings"
+)
+
+func Eval(cmd string) error {
+ args := strings.Split(cmd, " ")
+ switch args[0] {
+ case "cd":
+ if err := chDir(args); err != nil {
+ return err
+ }
+ break
+ case "export":
+ if err := export(args); err != nil {
+ return err
+ }
+ break
+ case "unset":
+ if len(args) == 1 || len(args) >= 3{
+ return errors.New("unset: usage: name")
+ }
+ os.Unsetenv(args[1])
+ break
+ case ":":
+ return nil
+ }
+ return nil
+}
+
+func chDir(args []string) error {
+ var dir string
+ if len(args) == 1 {
+ dir, _ = os.UserHomeDir()
+ } else {
+ dir = args[1]
+ }
+ return os.Chdir(dir)
+}
+
+func export(args []string) error {
+ if len(args) == 1 || len(args) >= 3 {
+ return errors.New("export: usage: name=value")
+ }
+ tmp := strings.Split(args[1], "=")
+ if len(tmp) != 2 {
+ return errors.New("export: usage: name=value")
+ }
+ return os.Setenv(tmp[0], tmp[1])
+}