From d9c545898868b68421bb3e933c8e24ef5712d1a3 Mon Sep 17 00:00:00 2001 From: Laukka Date: Sat, 11 Jan 2025 14:59:27 +0100 Subject: [PATCH] Added flags to ls --- command/cat/cat.go | 4 +--- command/command.go | 50 ++++++++++++++++++++++++++++---------------- command/echo/echo.go | 4 ++-- command/help/help.go | 6 ++---- command/ls/ls.go | 18 +++++++++++++--- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/command/cat/cat.go b/command/cat/cat.go index 04bb674..f6bb350 100644 --- a/command/cat/cat.go +++ b/command/cat/cat.go @@ -12,11 +12,9 @@ func Cat(in input_parser.Input, env *environment.Env) { file := filepath.Join(env.Path, in.Args) bytea_str, err := os.ReadFile(file) if err != nil { - fmt.Print(fmt.Sprintf("Error opening file: %s", err.Error())) + fmt.Print(fmt.Sprintf("Lilla råttan åt upp din kod", err.Error())) return } fmt.Print(string(bytea_str)) } - - diff --git a/command/command.go b/command/command.go index 6daec8d..dcdd65f 100644 --- a/command/command.go +++ b/command/command.go @@ -21,6 +21,7 @@ import ( "path/filepath" "strings" ) + var path_command map[string]string func Init(env *environment.Env) { @@ -31,24 +32,37 @@ func Init(env *environment.Env) { func Run_command(in input_parser.Input, env *environment.Env) { switch in.Instruction { - case "pwd": pwd.Pwd(in, env) - case "echo": echo.Echo(in, env) - case "ls": ls.Ls(in, env) - case "cd": cd.Cd(in, env) - case "man": man.Man(in, env) - case "cat": cat.Cat(in, env) - case "head": head.Head(in, env) - case "touch": touch.Touch(in, env) - case "rm": rm.Rm(in, env) - case "cp": cp.Cp(in, env) - case "mv": mv.Mv(in,env) - case "help": help.Help(in,env) - default: - if !run_by_path(in, env) { - fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction)) - } + case "pwd": + pwd.Pwd(in, env) + case "echo": + echo.Echo(in, env) + case "ls": + ls.Ls(in, env, "flags") + case "cd": + cd.Cd(in, env) + case "man": + man.Man(in, env) + case "cat": + cat.Cat(in, env) + case "head": + head.Head(in, env) + case "touch": + touch.Touch(in, env) + case "rm": + rm.Rm(in, env) + case "cp": + cp.Cp(in, env) + case "mv": + mv.Mv(in, env) + case "help": + help.Help(in, env) + default: + if !run_by_path(in, env) { + fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction)) + } } } + // Returns if anything was run or not func run_by_path(in input_parser.Input, env *environment.Env) bool { instr, errB := path_command[in.Instruction] @@ -60,7 +74,7 @@ func run_by_path(in input_parser.Input, env *environment.Env) bool { print(string(output)) return true } - + func init_path(env *environment.Env) { path := strings.Split(env.Env["PATH"], ":") for _, a_path := range path { @@ -83,7 +97,7 @@ func recursive_executable_finder(path string) { path_command[f.Name()] = filepath.Join(path, f.Name()) } } -func is_executable( name string) bool { +func is_executable(name string) bool { return true stat, _ := os.Stat(name) mode := stat.Mode() diff --git a/command/echo/echo.go b/command/echo/echo.go index a13453f..fe7368c 100644 --- a/command/echo/echo.go +++ b/command/echo/echo.go @@ -1,12 +1,12 @@ package echo import ( + "bbash/environment" "bbash/input_parser" "fmt" - "bbash/environment" ) func Echo(in input_parser.Input, _ *environment.Env) { fmt.Print(in.Args) + // fungerar inte om råttan åt din kod } - diff --git a/command/help/help.go b/command/help/help.go index 9cc0d9d..affcdd2 100644 --- a/command/help/help.go +++ b/command/help/help.go @@ -1,9 +1,9 @@ package help import ( + "bbash/environment" "bbash/input_parser" "fmt" - "bbash/environment" ) func Help(in input_parser.Input, _ *environment.Env) { @@ -12,7 +12,7 @@ func Help(in input_parser.Input, _ *environment.Env) { The options are: pwd: Output current working directory echo: Echoes the input argument -ls: List Sources in the current working directory +ls: Lists Sources in the current working directory cd: Change to directory given in the argument man: Manual for the terminal options cat: Outputs the contents of the given file @@ -24,5 +24,3 @@ mv: Moves a given file. Argument: help: Prints this message `) } - - diff --git a/command/ls/ls.go b/command/ls/ls.go index 8a1a26d..cfc6226 100644 --- a/command/ls/ls.go +++ b/command/ls/ls.go @@ -5,17 +5,29 @@ import ( "bbash/input_parser" "fmt" "os" + "strings" ) -func Ls(in input_parser.Input, env *environment.Env) { +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, flags string) { + if flags == "help" || 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 + } fs, err := os.ReadDir(env.Path) if err != nil { fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path)) } for _, f := range fs { + if (f.Name()[0] == '.') && (strings.Contains(flags, "a")) { + continue + } // allows for hidden directories fmt.Print(f.Name()) fmt.Println() } } - -