diff --git a/.golshrc b/.golshrc new file mode 100644 index 0000000..fdc83c2 --- /dev/null +++ b/.golshrc @@ -0,0 +1 @@ +apath ~/Documents/misc/ado-linker \ No newline at end of file diff --git a/cmd/pyapath.go b/cmd/pyapath.go new file mode 100644 index 0000000..7653b8e --- /dev/null +++ b/cmd/pyapath.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "gosh/types" + "path/filepath" + + "github.com/fatih/color" +) + +func GolshPyapath(sh *types.Shell, path string) types.CmdOutput { + abs, err := filepath.Abs(path) + + red := color.New(color.FgRed).SprintFunc() + + if err != nil { + return types.CmdOutput{Id: 1, Output: red(err)} + } + + sh.PyPaths = append(sh.PyPaths, abs) + + return types.CmdOutput{Id: 0, Output: ""} +} diff --git a/main.go b/main.go index 3a7ec1f..025bbc5 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "gosh/cmd" "gosh/types" "os" + "os/exec" + "path/filepath" "strings" "github.com/fatih/color" @@ -41,6 +43,46 @@ func parse_flags(parts []string) (map[string]string, []string) { return flags, args } +func check_pathes(sh *types.Shell, name string, args []string) bool { + var output types.CmdOutput + + program_found := false + + // Check Python Pathes + for _, path := range sh.PyPaths { + children, err := os.ReadDir(path) + + if err != nil { + color.Red("Error reading path.") + fmt.Println(err) + } + + for _, child := range children { + child_name := strings.TrimSpace(child.Name()) + if !child.IsDir() && child_name == name { + program_found = true + full_path := filepath.Join(path, child.Name()) + path_args := append([]string{full_path}, args...) + comd := exec.Command("python3", path_args...) + + out, err := comd.Output() + if err != nil { + fmt.Print("Error: ") + color.Red(err.Error()) + } + + output = types.CmdOutput{Id: 0, Output: string(out)} + } + } + } + + if program_found { + gosh_print_output(output) + } + + return program_found +} + func gosh_process_input(sh *types.Shell, input string) { // Split by pipe and trim spaces commands := strings.Split(input, "|") @@ -82,9 +124,19 @@ func gosh_process_input(sh *types.Shell, input string) { prevOutput = cmd.GoshGrep(sh, flags, prevOutput.Output, args[len(args)-1]) case "clear": cmd.GoshClear() + case "apath": + if len(parts) < 2 { + fmt.Println(("Usage: apath ")) + return + } + + prevOutput = cmd.GolshPyapath(sh, parts[1]) default: - fmt.Println("Unknown command: ", parts[0]) - return + + if !check_pathes(sh, parts[0], parts[1:]) { + fmt.Println("Unknown command:", parts[0]) + return + } } } @@ -97,7 +149,11 @@ func main() { c := color.New(color.FgCyan) dir, _ := os.Getwd() - sh := types.Shell{Cd: dir} + + // Init shell + sh := types.Shell{ + Cd: dir, + PyPaths: []string{}} var should_exit = false for { diff --git a/types/shell.go b/types/shell.go index 8ddba9e..94b595e 100644 --- a/types/shell.go +++ b/types/shell.go @@ -1,7 +1,8 @@ package types type Shell struct { - Cd string + Cd string + PyPaths []string } type CmdOutput struct {