GoSH/main.go
Alexandre 3f18ca0cf1 Added the main Go file.
The file is broken ATM, to make it work delete the last function at the end.
2025-01-28 22:47:46 +01:00

69 lines
1.3 KiB
Go

package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
// Read the keyboard input.
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Handle the execution of the input.
if err = execInput(input); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
func execInput(input string) error {
// Remove the newline caracter.
input = strings.TrimSuffix(input, "\n")
// Split the input to separate the command and the arguments.
args := strings.Split(input, " ")
// Check for built-in commands.
switch args[0] {
case "cd":
// Change to ~ with an empty PATH is not supported
if len(args) < 2 || args[1] == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return errors.New("Unable to get home directory")
}
return os.Chdir(homeDir)
}
// Change the dir and return the error.
return os.Chdir(args[1])
case "exit":
os.Exit(0)
case "version":
println("GoShell Version 0.0.1")
}
// Pass the program and the arguments separately.
cmd := exec.Command(args[0], args[1:]...)
// Set the correct output device.
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
// Execute the command and return the error.
return cmd.Run()
}
func checkHistory() {
if _, err := os.Stat()
}