From e239096ef714df163136bdd18ea49fcbcd1b8342 Mon Sep 17 00:00:00 2001 From: "Tobias P.L Wennberg" Date: Sun, 12 Jan 2025 09:35:41 +0100 Subject: [PATCH] Fix merge --- command/cat/cat.go | 2 +- command/cd/cd.go | 8 ++++---- command/command.go | 2 +- command/cp/cp.go | 2 +- command/echo/echo.go | 2 +- command/head/head.go | 2 +- command/ls/ls.go | 17 +++++++++++------ command/man/man.go | 2 +- command/mv/mv.go | 2 +- command/rm/rm.go | 3 +-- command/touch/touch.go | 2 +- input_parser/input_parser.go | 17 +++++++++-------- 12 files changed, 33 insertions(+), 28 deletions(-) diff --git a/command/cat/cat.go b/command/cat/cat.go index f6bb350..73cb413 100644 --- a/command/cat/cat.go +++ b/command/cat/cat.go @@ -9,7 +9,7 @@ import ( ) func Cat(in input_parser.Input, env *environment.Env) { - file := filepath.Join(env.Path, in.Args) + file := filepath.Join(env.Path, in.Args_raw) bytea_str, err := os.ReadFile(file) if err != nil { fmt.Print(fmt.Sprintf("Lilla råttan åt upp din kod", err.Error())) diff --git a/command/cd/cd.go b/command/cd/cd.go index 945af2d..47a5543 100644 --- a/command/cd/cd.go +++ b/command/cd/cd.go @@ -10,13 +10,13 @@ import ( func Cd(in input_parser.Input, env *environment.Env) { var new_path string - if in.Args == "" { + if in.Args_raw == "" { new_path = env.Env["HOME"] } else { - if in.Args[0] == '/' { - new_path = in.Args + if in.Args_raw[0] == '/' { + new_path = in.Args_raw } else { - new_path = filepath.Join(env.Path, in.Args) + new_path = filepath.Join(env.Path, in.Args_raw) } } file, err := os.Stat(new_path) diff --git a/command/command.go b/command/command.go index 3e817b6..e74ae7f 100644 --- a/command/command.go +++ b/command/command.go @@ -69,7 +69,7 @@ func run_by_path(in input_parser.Input, env *environment.Env) bool { if !errB { return false } - cmd := exec.Command(instr, strings.Fields(in.Args)...) + cmd := exec.Command(instr, strings.Fields(in.Args_raw)...) output, _ := cmd.CombinedOutput() print(string(output)) return true diff --git a/command/cp/cp.go b/command/cp/cp.go index 7df86d7..f47f875 100644 --- a/command/cp/cp.go +++ b/command/cp/cp.go @@ -10,7 +10,7 @@ import ( ) func Cp(in input_parser.Input, env *environment.Env) { - args := strings.SplitN(in.Args, " ", 2) + args := strings.SplitN(in.Args_raw, " ", 2) if len(args) != 2 { fmt.Print("No right amount of args") return diff --git a/command/echo/echo.go b/command/echo/echo.go index fe7368c..88bc8b7 100644 --- a/command/echo/echo.go +++ b/command/echo/echo.go @@ -7,6 +7,6 @@ import ( ) func Echo(in input_parser.Input, _ *environment.Env) { - fmt.Print(in.Args) + fmt.Print(in.Args_raw) // fungerar inte om råttan åt din kod } diff --git a/command/head/head.go b/command/head/head.go index 68b951b..8186006 100644 --- a/command/head/head.go +++ b/command/head/head.go @@ -11,7 +11,7 @@ import ( ) func Head(in input_parser.Input, env *environment.Env) { - args := strings.SplitN(in.Args, " ", 2) + args := strings.SplitN(in.Args_raw, " ", 2) var file_path string var lines int if len(args) >= 1 { diff --git a/command/ls/ls.go b/command/ls/ls.go index be43153..0626ae9 100644 --- a/command/ls/ls.go +++ b/command/ls/ls.go @@ -8,16 +8,21 @@ import ( "slices" ) -var flagsArray []string = []string{"a ", "help "} -var flagsDictionary map[string]string = map[string]string{"a": "-a shows hidden directories", "help": "-help shows this message"} - func Ls(in input_parser.Input, env *environment.Env) { + flagsArray := []string { + "a ", + "help ", + } + flagsDictionary := map[string]string { + "a": "-a shows hidden directories", + "help": "-help shows this message", + } if slices.Contains(in.Flags, "help") { fmt.Printf("Lists Sources in the current working directory") fmt.Printf("Supported flags are:") - for i := 0; i < len(flagsArray); i++ { - fmt.Println(flagsArray[i] + flagsDictionary[flagsArray[i]]) - } // this prints all flags and their description + for _, flag := range flagsArray { + fmt.Println(flag + flagsDictionary[flag]) + } // Print flags and there description } fs, err := os.ReadDir(env.Path) if err != nil { diff --git a/command/man/man.go b/command/man/man.go index a48aa8e..7cfed03 100644 --- a/command/man/man.go +++ b/command/man/man.go @@ -7,7 +7,7 @@ import ( ) func Man(in input_parser.Input, env *environment.Env) { - switch in.Args { + switch in.Args_raw { case "pwd": fmt.Print("Output current working directory") case "echo": diff --git a/command/mv/mv.go b/command/mv/mv.go index c0d64ef..4d32f31 100644 --- a/command/mv/mv.go +++ b/command/mv/mv.go @@ -10,7 +10,7 @@ import ( ) func Mv(in input_parser.Input, env *environment.Env) { - args := strings.SplitN(in.Args, " ", 2) + args := strings.SplitN(in.Args_raw, " ", 2) if len(args) != 2 { fmt.Print("No right amount of args") return diff --git a/command/rm/rm.go b/command/rm/rm.go index 334ac78..c24b500 100644 --- a/command/rm/rm.go +++ b/command/rm/rm.go @@ -9,11 +9,10 @@ import ( ) func Rm(in input_parser.Input, env *environment.Env) { - file_path := filepath.Join(env.Path, in.Args) + file_path := filepath.Join(env.Path, in.Args_raw) stat, err := os.Stat(file_path) if os.IsNotExist(err) { fmt.Println(fmt.Sprintf("File does not exist")) - fmt.Print(fmt.Sprintf("Try 'bash rm -rf /' instead")) return } if stat.IsDir() { diff --git a/command/touch/touch.go b/command/touch/touch.go index 2db49bc..efb6e57 100644 --- a/command/touch/touch.go +++ b/command/touch/touch.go @@ -9,7 +9,7 @@ import ( ) func Touch(in input_parser.Input, env *environment.Env) { - file_path := filepath.Join(env.Path, in.Args) + file_path := filepath.Join(env.Path, in.Args_raw) _, err := os.Stat(file_path) if !os.IsNotExist(err) { fmt.Print(fmt.Sprintf("File alredy exist!")) diff --git a/input_parser/input_parser.go b/input_parser/input_parser.go index 82b82bb..80069ce 100644 --- a/input_parser/input_parser.go +++ b/input_parser/input_parser.go @@ -26,22 +26,23 @@ func parse(input string) Input { instruction := strings.TrimSpace(split[0]) var flags []string var args []string - for i := 0; i < len(split); i++ { - if split[i][0:2] == "--" { - var result = strings.TrimSpace(split[i]) + + for _, arg := range split[1:] { + if arg[0:2] == "--" { + var result = strings.TrimSpace(arg) flags = append(flags, string(result[2:])) continue - } else if split[i][0] == '-' { - var result = strings.TrimSpace(split[i]) + } else if arg[0] == '-' { + var result = strings.TrimSpace(arg) for _, c := range result[1:] { flags = append(flags, string(c)) } continue - } else if i != 0 { - args = append(args, strings.TrimSpace(split[i])) + } else { + args = append(args, strings.TrimSpace(arg)) } - } + } return Input{ Instruction: instruction, Args: args,