PATHgit add .

This commit is contained in:
Tobias P.L Wennberg 2025-01-10 19:23:14 +01:00
parent 15eeadf5d3
commit 7dc7def780
2 changed files with 14 additions and 10 deletions

View File

@ -17,11 +17,14 @@ import (
"bbash/input_parser"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
var path_command []string
var path_command map[string]string
func Init(env *environment.Env) {
path_command = make(map[string]string)
init_path(env)
}
@ -48,17 +51,21 @@ func Run_command(in input_parser.Input, env *environment.Env) {
}
// Returns if anything was run or not
func run_by_path(in input_parser.Input, env *environment.Env) bool {
fmt.Print(env.Env["PATH"])
return false
instr, errB := path_command[in.Instruction]
if !errB {
return false
}
cmd := exec.Command(instr, strings.Fields(in.Args)...)
output, _ := cmd.CombinedOutput()
print(string(output))
return true
}
func init_path(env *environment.Env) {
path := strings.Split(env.Env["PATH"], ":")
for _, a_path := range path {
recursive_executable_finder(a_path)
}
}
func recursive_executable_finder(path string) {
dir, err := os.ReadDir(path)
@ -71,12 +78,9 @@ func recursive_executable_finder(path string) {
continue
}
if !is_executable(f.Name()) {
println("not exec")
continue
}
println(f.Name())
path_command = append(path_command, f.Name())
path_command[f.Name()] = filepath.Join(path, f.Name())
}
}
func is_executable( name string) bool {

View File

@ -7,7 +7,7 @@ import (
)
func Man(in input_parser.Input, env *environment.Env) {
switch in.Instruction {
switch in.Args {
case "pwd":
fmt.Print("Output current working directory")
case "echo":