Created
August 18, 2019 08:57
-
-
Save SimonAKing/eaa2debb3ef455054a9dbc99a6733cc1 to your computer and use it in GitHub Desktop.
Command tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "errors" | |
| "fmt" | |
| "os" | |
| "os/exec" | |
| "strings" | |
| ) | |
| const prompt = "$ " | |
| var ( | |
| noPathError = errors.New("path required.") | |
| ) | |
| func main() { | |
| scanner := bufio.NewScanner(os.Stdin) | |
| fmt.Print(prompt) | |
| for scanner.Scan() { | |
| fmt.Print(prompt) | |
| input := scanner.Text() | |
| if input == "" { | |
| continue | |
| } | |
| args := strings.Split(input, " ") | |
| command := args[0] | |
| args = args[1:] | |
| switch command { | |
| case "cd": | |
| if len(args) == 0 { | |
| panic(noPathError) | |
| } | |
| os.Chdir(args[0]) | |
| case "exit": | |
| os.Exit(0) | |
| default: | |
| cmd := exec.Command(command, args...) | |
| cmd.Stderr = os.Stderr | |
| cmd.Stdout = os.Stdout | |
| cmd.Run() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment