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/cmd"
|
||||||
"gosh/types"
|
"gosh/types"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
@ -41,6 +43,46 @@ func parse_flags(parts []string) (map[string]string, []string) {
|
|||||||
return flags, args
|
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) {
|
func gosh_process_input(sh *types.Shell, input string) {
|
||||||
// Split by pipe and trim spaces
|
// Split by pipe and trim spaces
|
||||||
commands := strings.Split(input, "|")
|
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])
|
prevOutput = cmd.GoshGrep(sh, flags, prevOutput.Output, args[len(args)-1])
|
||||||
case "clear":
|
case "clear":
|
||||||
cmd.GoshClear()
|
cmd.GoshClear()
|
||||||
|
case "apath":
|
||||||
|
if len(parts) < 2 {
|
||||||
|
fmt.Println(("Usage: apath <path/dir>"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prevOutput = cmd.GolshPyapath(sh, parts[1])
|
||||||
default:
|
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)
|
c := color.New(color.FgCyan)
|
||||||
|
|
||||||
dir, _ := os.Getwd()
|
dir, _ := os.Getwd()
|
||||||
sh := types.Shell{Cd: dir}
|
|
||||||
|
// Init shell
|
||||||
|
sh := types.Shell{
|
||||||
|
Cd: dir,
|
||||||
|
PyPaths: []string{}}
|
||||||
|
|
||||||
var should_exit = false
|
var should_exit = false
|
||||||
for {
|
for {
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
type Shell struct {
|
type Shell struct {
|
||||||
Cd string
|
Cd string
|
||||||
|
PyPaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdOutput struct {
|
type CmdOutput struct {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user