added python path thing
This commit is contained in:
parent
f6140a1fc3
commit
7062e798a9
22
cmd/pyapath.go
Normal file
22
cmd/pyapath.go
Normal 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: ""}
|
||||
}
|
||||
62
main.go
62
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,10 +124,20 @@ func gosh_process_input(sh *types.Shell, input string) {
|
||||
prevOutput = cmd.GoshGrep(sh, flags, prevOutput.Output, args[len(args)-1])
|
||||
case "clear":
|
||||
cmd.GoshClear()
|
||||
default:
|
||||
fmt.Println("Unknown command: ", parts[0])
|
||||
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 {
|
||||
|
||||
@ -2,6 +2,7 @@ package types
|
||||
|
||||
type Shell struct {
|
||||
Cd string
|
||||
PyPaths []string
|
||||
}
|
||||
|
||||
type CmdOutput struct {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user