Added flags to ls

This commit is contained in:
Laukka 2025-01-11 14:59:27 +01:00
parent 7dc7def780
commit d9c5458988
5 changed files with 52 additions and 30 deletions

View File

@ -12,11 +12,9 @@ func Cat(in input_parser.Input, env *environment.Env) {
file := filepath.Join(env.Path, in.Args) file := filepath.Join(env.Path, in.Args)
bytea_str, err := os.ReadFile(file) bytea_str, err := os.ReadFile(file)
if err != nil { 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 return
} }
fmt.Print(string(bytea_str)) fmt.Print(string(bytea_str))
} }

View File

@ -21,6 +21,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
) )
var path_command map[string]string var path_command map[string]string
func Init(env *environment.Env) { func Init(env *environment.Env) {
@ -31,24 +32,37 @@ func Init(env *environment.Env) {
func Run_command(in input_parser.Input, env *environment.Env) { func Run_command(in input_parser.Input, env *environment.Env) {
switch in.Instruction { switch in.Instruction {
case "pwd": pwd.Pwd(in, env) case "pwd":
case "echo": echo.Echo(in, env) pwd.Pwd(in, env)
case "ls": ls.Ls(in, env) case "echo":
case "cd": cd.Cd(in, env) echo.Echo(in, env)
case "man": man.Man(in, env) case "ls":
case "cat": cat.Cat(in, env) ls.Ls(in, env, "flags")
case "head": head.Head(in, env) case "cd":
case "touch": touch.Touch(in, env) cd.Cd(in, env)
case "rm": rm.Rm(in, env) case "man":
case "cp": cp.Cp(in, env) man.Man(in, env)
case "mv": mv.Mv(in,env) case "cat":
case "help": help.Help(in,env) cat.Cat(in, env)
default: case "head":
if !run_by_path(in, env) { head.Head(in, env)
fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction)) 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 // Returns if anything was run or not
func run_by_path(in input_parser.Input, env *environment.Env) bool { func run_by_path(in input_parser.Input, env *environment.Env) bool {
instr, errB := path_command[in.Instruction] 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)) print(string(output))
return true return true
} }
func init_path(env *environment.Env) { func init_path(env *environment.Env) {
path := strings.Split(env.Env["PATH"], ":") path := strings.Split(env.Env["PATH"], ":")
for _, a_path := range 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()) path_command[f.Name()] = filepath.Join(path, f.Name())
} }
} }
func is_executable( name string) bool { func is_executable(name string) bool {
return true return true
stat, _ := os.Stat(name) stat, _ := os.Stat(name)
mode := stat.Mode() mode := stat.Mode()

View File

@ -1,12 +1,12 @@
package echo package echo
import ( import (
"bbash/environment"
"bbash/input_parser" "bbash/input_parser"
"fmt" "fmt"
"bbash/environment"
) )
func Echo(in input_parser.Input, _ *environment.Env) { func Echo(in input_parser.Input, _ *environment.Env) {
fmt.Print(in.Args) fmt.Print(in.Args)
// fungerar inte om råttan åt din kod
} }

View File

@ -1,9 +1,9 @@
package help package help
import ( import (
"bbash/environment"
"bbash/input_parser" "bbash/input_parser"
"fmt" "fmt"
"bbash/environment"
) )
func Help(in input_parser.Input, _ *environment.Env) { func Help(in input_parser.Input, _ *environment.Env) {
@ -12,7 +12,7 @@ func Help(in input_parser.Input, _ *environment.Env) {
The options are: The options are:
pwd: Output current working directory pwd: Output current working directory
echo: Echoes the input argument 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 cd: Change to directory given in the argument
man: Manual for the terminal options man: Manual for the terminal options
cat: Outputs the contents of the given file cat: Outputs the contents of the given file
@ -24,5 +24,3 @@ mv: Moves a given file. Argument: <source> <destination>
help: Prints this message help: Prints this message
`) `)
} }

View File

@ -5,17 +5,29 @@ import (
"bbash/input_parser" "bbash/input_parser"
"fmt" "fmt"
"os" "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) fs, err := os.ReadDir(env.Path)
if err != nil { if err != nil {
fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path)) fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path))
} }
for _, f := range fs { for _, f := range fs {
if (f.Name()[0] == '.') && (strings.Contains(flags, "a")) {
continue
} // allows for hidden directories
fmt.Print(f.Name()) fmt.Print(f.Name())
fmt.Println() fmt.Println()
} }
} }