Compare commits
No commits in common. "9e46cc910577da2dd85eabf9287afc662a126ef2" and "f6140a1fc394d938167d75b77630617ab2da03e8" have entirely different histories.
9e46cc9105
...
f6140a1fc3
@ -2,17 +2,13 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gosh/types"
|
"gosh/types"
|
||||||
"gosh/util"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GoshCat(sh *types.Shell, file_path string) types.CmdOutput {
|
func GoshCat(sh *types.Shell, filename string) types.CmdOutput {
|
||||||
|
data, err := os.ReadFile(filename)
|
||||||
file_path = util.ExpandHome(file_path)
|
|
||||||
|
|
||||||
data, err := os.ReadFile(file_path)
|
|
||||||
red := color.New(color.FgRed).SprintFunc()
|
red := color.New(color.FgRed).SprintFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.CmdOutput{Id: 1, Output: red(err)}
|
return types.CmdOutput{Id: 1, Output: red(err)}
|
||||||
|
|||||||
@ -4,15 +4,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"gosh/types"
|
"gosh/types"
|
||||||
"gosh/util"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GoshCd(sh *types.Shell, dir string) types.CmdOutput {
|
func GoshCd(sh *types.Shell, dir string) types.CmdOutput {
|
||||||
|
|
||||||
dir = util.ExpandHome(dir)
|
|
||||||
|
|
||||||
err := os.Chdir(dir)
|
err := os.Chdir(dir)
|
||||||
red := color.New(color.FgRed).SprintFunc()
|
red := color.New(color.FgRed).SprintFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
17
cmd/ls.go
17
cmd/ls.go
@ -2,27 +2,14 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gosh/types"
|
"gosh/types"
|
||||||
"gosh/util"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GoshLs(sh *types.Shell, paths ...string) types.CmdOutput {
|
func GoshLs(sh *types.Shell) types.CmdOutput {
|
||||||
|
children, err := os.ReadDir(sh.Cd)
|
||||||
var dir string
|
|
||||||
|
|
||||||
if len(paths) > 0 && paths[0] != "" {
|
|
||||||
dir = paths[0]
|
|
||||||
} else {
|
|
||||||
dir = sh.Cd
|
|
||||||
}
|
|
||||||
|
|
||||||
dir = util.ExpandHome(dir)
|
|
||||||
|
|
||||||
children, err := os.ReadDir(dir)
|
|
||||||
|
|
||||||
red := color.New(color.FgRed).SprintFunc()
|
red := color.New(color.FgRed).SprintFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.CmdOutput{Id: 1, Output: red(err)}
|
return types.CmdOutput{Id: 1, Output: red(err)}
|
||||||
|
|||||||
@ -1,26 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"gosh/types"
|
|
||||||
"gosh/util"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GolshPyapath(sh *types.Shell, path string) types.CmdOutput {
|
|
||||||
|
|
||||||
path = util.ExpandHome(path)
|
|
||||||
|
|
||||||
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: ""}
|
|
||||||
}
|
|
||||||
96
main.go
96
main.go
@ -6,8 +6,6 @@ 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"
|
||||||
@ -43,61 +41,6 @@ func parse_flags(parts []string) (map[string]string, []string) {
|
|||||||
return flags, args
|
return flags, args
|
||||||
}
|
}
|
||||||
|
|
||||||
func process_golshscript(sh *types.Shell, file_path string) {
|
|
||||||
data, err := os.ReadFile(file_path)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error: ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
lines := strings.Split(string(data), "\n")
|
|
||||||
|
|
||||||
for _, line := range lines {
|
|
||||||
gosh_process_input(sh, strings.TrimSpace(line))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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, "|")
|
||||||
@ -120,11 +63,7 @@ func gosh_process_input(sh *types.Shell, input string) {
|
|||||||
}
|
}
|
||||||
prevOutput = cmd.GoshCd(sh, parts[1])
|
prevOutput = cmd.GoshCd(sh, parts[1])
|
||||||
case "ls":
|
case "ls":
|
||||||
if len(parts) > 1 {
|
prevOutput = cmd.GoshLs(sh)
|
||||||
prevOutput = cmd.GoshLs(sh, parts[1:]...)
|
|
||||||
} else {
|
|
||||||
prevOutput = cmd.GoshLs(sh)
|
|
||||||
}
|
|
||||||
case "cat":
|
case "cat":
|
||||||
if len(parts) < 2 {
|
if len(parts) < 2 {
|
||||||
fmt.Println("Usage: cat <path to file>")
|
fmt.Println("Usage: cat <path to file>")
|
||||||
@ -143,19 +82,9 @@ 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])
|
||||||
if !check_pathes(sh, parts[0], parts[1:]) {
|
return
|
||||||
fmt.Println("Unknown command:", parts[0])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,24 +97,7 @@ 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{}}
|
|
||||||
|
|
||||||
// Check for and execute .golshrc
|
|
||||||
golshrc_path, _ := os.UserHomeDir()
|
|
||||||
golshrc_path = golshrc_path + "/.golshrc"
|
|
||||||
|
|
||||||
_, err := os.Stat(golshrc_path)
|
|
||||||
|
|
||||||
golshrc_exists := err == nil
|
|
||||||
fmt.Println(golshrc_exists)
|
|
||||||
if golshrc_exists {
|
|
||||||
fmt.Println("exists")
|
|
||||||
process_golshscript(&sh, golshrc_path)
|
|
||||||
}
|
|
||||||
|
|
||||||
var should_exit = false
|
var should_exit = false
|
||||||
for {
|
for {
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
type Shell struct {
|
type Shell struct {
|
||||||
Cd string
|
Cd string
|
||||||
PyPaths []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdOutput struct {
|
type CmdOutput struct {
|
||||||
|
|||||||
17
util/util.go
17
util/util.go
@ -1,17 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ExpandHome(path string) string {
|
|
||||||
if strings.HasPrefix(path, "~") {
|
|
||||||
home, err := os.UserHomeDir()
|
|
||||||
if err == nil {
|
|
||||||
return strings.Replace(path, "~", home, 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user