added python path thing

This commit is contained in:
JISAUAY 2025-06-27 10:55:53 -05:00
parent f6140a1fc3
commit 7062e798a9
4 changed files with 84 additions and 4 deletions

1
.golshrc Normal file
View File

@ -0,0 +1 @@
apath ~/Documents/misc/ado-linker

22
cmd/pyapath.go Normal file
View File

@ -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: ""}
}

58
main.go
View File

@ -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,11 +124,21 @@ 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 <path/dir>"))
return
}
prevOutput = cmd.GolshPyapath(sh, parts[1])
default:
if !check_pathes(sh, parts[0], parts[1:]) {
fmt.Println("Unknown command:", parts[0])
return
}
}
}
gosh_print_output(prevOutput)
}
@ -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 {

View File

@ -2,6 +2,7 @@ package types
type Shell struct {
Cd string
PyPaths []string
}
type CmdOutput struct {