gshell, or GoShell is a small shell project that I am building as a side project. The plan is to implement some commands whenever I have the time and slowly increase the complexity of this project. The goal is to try and make this shell as full-featured as possible and exec
ing all other unsupported commands.
Contributions are also welcome!
This shell is built using 3 goroutines, or threads:
- Output Thread: A worker thread that receives results represented in string form that only handles outputting these results to STDOUT
- Worker Thread: A worker thread that takes user input and depending on the user input, will try to generate the correct answer to the user's request and sends it along to the
Output Thread
- Main Thread: The main thread that handles STDIN (User input), and passes the user input to
Worker Thread
The way these threads talk to each other is a combination of the built-in golang
mechanism, channels, and Mutexes.
I lock
the mutex right before I print the prompt in the Main Thread so that the Output Thread can print all that it needs to print, and then when the previous command finishes, I unlock
the mutex so that the prompt in the Main Thread can print, and then we can receive new user input.
The 3 threads use channels to communicate amongst each other in the following manner:
Main thread
sends user input to Worker thread via channelWorker Thread
receives command and processes it. Once theWorker Thread
is done doing work, or whenever it has something to output, it sends the output to theOutput Thread
via channel- The
Output Thread
receives what it needs to print, and when theOutput Thread
receives a stop token, it Unlocks the mutex so that theMain Thread
can again receive user input
- Colorized output
- Tab completion
- Command history
- Ability to run shell scripts
- ls: List files in current directory
- ls <path>: List files at directory
- cd: Change directory to home directory
- cd <path>: Change directory
- pwd: Returns current working directory
- cat <file>: Print contents of file to stdout
- tail <file>: Print last 10 lines of a file to terminal
- tail -f <file>: Print last 10 lines of a file, but still listen until user kills process
- wd <name>: Has a database of name:filepath key-value pairs, so that it remembers custom locations in file system and can
cd
to that location - wd add <name>: Add current working directory to the
wd
database and associate this location with a name - wd list: Lists all of the currently associated names within the database
- wd rm <name>: Removes the entry from the
wd
database with a key of the specified name
- Clone this repo
- Make sure to have a basic golang environment set up.
- Run
go get
to install 3rd party dependencies - Run
go run main.go
to run the program
If you want to implement a command, please create an issue, and I'll try to reply ASAP and give the okay.
Tests would be nice.