Added flags to ls
This commit is contained in:
parent
7dc7def780
commit
d9c5458988
@ -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))
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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)
|
||||
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]
|
||||
@ -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()
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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: <source> <destination>
|
||||
help: Prints this message
|
||||
`)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user