diff options
-rw-r--r-- | cmds/cd.go | 2 | ||||
-rw-r--r-- | cmds/cmds.go | 17 | ||||
-rw-r--r-- | cmds/env.go | 58 | ||||
-rw-r--r-- | cmds/exit.go | 2 | ||||
-rw-r--r-- | cmds/export.go | 18 | ||||
-rw-r--r-- | cmds/set.go | 32 |
6 files changed, 70 insertions, 59 deletions
@@ -2,6 +2,8 @@ package cmds import "os" +// chDir: changes the current working directory +// if no directory is specifed sets it to home func chDir(args []string) error { var dir string if len(args) == 1 { diff --git a/cmds/cmds.go b/cmds/cmds.go index 6d4d895..0cfee5e 100644 --- a/cmds/cmds.go +++ b/cmds/cmds.go @@ -1,11 +1,11 @@ +// cmds: handles system commands package cmds -import ( - "errors" - "os" - "strings" -) +import "strings" +// Eval: evaluates a provided string into a command +// if it can't find the command or the arguments are incorrect +// returns an error func Eval(cmd string) error { args := strings.Split(cmd, " ") switch args[0] { @@ -20,16 +20,15 @@ func Eval(cmd string) error { } break case "unset": - if len(args) == 1 || len(args) >= 3 { - return errors.New("usage: unset {name}") + if err := unset(args); err != nil { + return err } - os.Unsetenv(args[1]) break case "set": if len(args) > 1 { set(args) } else { - printenv() + printEnv() } break case "exit": 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]) +} diff --git a/cmds/exit.go b/cmds/exit.go index a295ff4..5170b26 100644 --- a/cmds/exit.go +++ b/cmds/exit.go @@ -6,6 +6,8 @@ import ( "strconv" ) +// exit the shell and returns the provided status +// if no status is specified returns 0 func exit(args []string) error { status := 0 if len(args) > 1 { diff --git a/cmds/export.go b/cmds/export.go deleted file mode 100644 index 1c2632d..0000000 --- a/cmds/export.go +++ /dev/null @@ -1,18 +0,0 @@ -package cmds - -import ( - "errors" - "os" - "strings" -) - -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]) -} diff --git a/cmds/set.go b/cmds/set.go deleted file mode 100644 index a2bc3a6..0000000 --- a/cmds/set.go +++ /dev/null @@ -1,32 +0,0 @@ -package cmds - -import ( - "fmt" - "os" - - "gosh/global" -) - -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) - } -} - -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] - } -} |