Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added -is command #26

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion ft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)

Expand Down Expand Up @@ -44,7 +46,7 @@ func PassCmd(args []string) ([]string, error) {
case "-ls", "-]", "-[", "-..", "--":
return []string{cmd}, nil
// providing help for a specific command may be needed in the future
case "-help", "-h", "-version", "-v":
case "-help", "-h", "-version", "-v", "-is":
break
case "-rn":
if len(args) <= 3 {
Expand Down Expand Up @@ -274,6 +276,36 @@ func showVersion(data *CmdArgs) error {
return nil
}

func showDirectoryVar(data *CmdArgs) error {
osteensco marked this conversation as resolved.
Show resolved Hide resolved
rt := runtime.GOOS
var dir string
switch rt {
case "linux":
cmd := exec.Command("pwd")
d, err := cmd.Output()
dir = strings.Trim(string(d), "\n")
if err != nil {
return err
}
default:
fmt.Println("This command is not supported on your os")
}

pathMaps := data.allPaths

for k := range pathMaps {
v, _ := pathMaps[k]

if strings.Compare(v, dir) == 0 {
fmt.Println(strings.Trim(k, "\n"))
osteensco marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
}

fmt.Println("No key found for this path!")
osteensco marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// Used for commands that are simply handled by the shell function
func passToShell(data *CmdArgs) error {
c := data.cmd[0]
Expand Down
92 changes: 79 additions & 13 deletions ft/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ func TestPassCmd(t *testing.T) {
}

}

}

func TestChangeDirectory(t *testing.T) {

tmpdir, err := os.MkdirTemp("", "testing")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -150,7 +148,6 @@ func TestChangeDirectory(t *testing.T) {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()

}()

w.Close()
Expand All @@ -166,6 +163,85 @@ func TestChangeDirectory(t *testing.T) {
}
}

func TestShowDirectoryVar(t *testing.T) {
tmpfile, err := os.CreateTemp("", "testdata.bin")
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()

if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
command []string
expected string
wanterr bool
}{
{
name: "1. Check the directory var using -is",
command: []string{"-is"},
expected: "testKey",
wanterr: false,
},
}

for _, tt := range tests {
testData := NewCmdArgs(
[]string{"-set", "testKey"},
make(map[string]string),
tmpfile,
nil,
)

err := setDirectoryVar(testData)
if err != nil {
t.Errorf("Error setting directory var: %v", err)
}

data := NewCmdArgs(
tt.command,
testData.allPaths,
tmpfile,
nil,
)

old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

os.Stdout = w

err = showDirectoryVar(data)
if err != nil {
t.Error(err)
}

// Use go routine so printing doesn't block program
outChan := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()
}()

w.Close()
os.Stdout = old
actual := <-outChan

actual = strings.Trim(actual, "\n")

if strings.Compare(actual, tt.expected) != 0 {
t.Errorf("Did not get the expected key")
}
}
}

func TestSetDirectoryVar(t *testing.T) {
tmpfile, err := os.CreateTemp("", "testdata.bin")
if err != nil {
Expand Down Expand Up @@ -212,7 +288,6 @@ func TestSetDirectoryVar(t *testing.T) {

if data.allPaths["testKey"] != tt.expected {
t.Errorf("Expected key 'testKey' to have value %s, got %s", tt.expected, data.allPaths["testKey"])

}

file, err := os.Open(tmpfile.Name())
Expand All @@ -227,10 +302,8 @@ func TestSetDirectoryVar(t *testing.T) {
}
if result["testKey"] != tt.expected {
t.Errorf("Expected file to have key 'testKey' with value %s, got %s", tt.expected, result["testKey"])

}
}

}

func TestDisplayAllPaths(t *testing.T) {
Expand Down Expand Up @@ -259,7 +332,6 @@ func TestDisplayAllPaths(t *testing.T) {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()

}()

w.Close()
Expand Down Expand Up @@ -398,7 +470,6 @@ func TestRenameKey(t *testing.T) {
}
if data.allPaths["newKey"] != "value1" {
t.Fatalf("Expected key 'newKey' to have value 'value1', got %s", data.allPaths["newKey"])

}

file, err := os.Open(tmpfile.Name())
Expand Down Expand Up @@ -436,7 +507,6 @@ func TestVersionCmd(t *testing.T) {
os.Stdout = w

err = showVersion(data)

if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -479,7 +549,6 @@ func TestShowHelp(t *testing.T) {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()

}()

w.Close()
Expand All @@ -493,7 +562,6 @@ func TestShowHelp(t *testing.T) {
}

func TestNavStack(t *testing.T) {

tests := []struct {
name string
command []string
Expand Down Expand Up @@ -541,7 +609,6 @@ func TestNavStack(t *testing.T) {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()

}()

w.Close()
Expand All @@ -565,5 +632,4 @@ func TestNavStack(t *testing.T) {
t.Errorf("Expected %s, got %s", tt.expected, actual)
}
}

}
2 changes: 2 additions & 0 deletions ft/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var AvailCmds = map[string]func(data *CmdArgs) error{
"--": passToShell,
"-version": showVersion,
"-v": showVersion,
"-is": showDirectoryVar,
}

var CmdDesc = map[string]string{
Expand All @@ -42,6 +43,7 @@ var CmdDesc = map[string]string{
"-rn": "renames key to new key - Usage: ft -rn [key] [new key]",
"]": "navigate history forwards - Usage: ft ]",
"[": "navigate history backwards - Usage: ft [",
"-is": "know the directory variable if set for a directory",
"-help": "you are here :) - Usage: ft -help, -h",
"-version": "print current version of fastTravelCLI - Usage: ft -version, -v",
}
3 changes: 2 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func TestMainFunc(t *testing.T) {
name: "1. Check help command.",
args: []string{"ft", "-help"},
expected: fmt.Sprintf(
"\n-help: %s\n-ls: %s\n-rm: %s\n-rn: %s\n-set: %s\n-version: %s\n[: %s\n]: %s\nkey: %s\n\n",
"\n-help: %s\n-is: %s\n-ls: %s\n-rm: %s\n-rn: %s\n-set: %s\n-version: %s\n[: %s\n]: %s\nkey: %s\n\n",
ft.CmdDesc["-help"],
ft.CmdDesc["-is"],
ft.CmdDesc["-ls"],
ft.CmdDesc["-rm"],
ft.CmdDesc["-rn"],
Expand Down
Loading