Skip to content

Commit

Permalink
public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Aug 17, 2024
1 parent d708f90 commit a894bdd
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 33 deletions.
18 changes: 9 additions & 9 deletions ft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (



func passCmd(args []string) ([]string, error) {
func PassCmd(args []string) ([]string, error) {

cmd := args[1]

_, ok := availCmds[cmd]
_, ok := AvailCmds[cmd]
if !ok {
return nil, errors.New(fmt.Sprintf("%v is not a valid command, use 'ft help' for valid commands", cmd))
}
Expand All @@ -35,20 +35,20 @@ func passCmd(args []string) ([]string, error) {

}

func changeDirectory(data cmdArgs) {
func changeDirectory(data CmdArgs) {

if len(data.allPaths) == 0 {
fmt.Printf("No fast travel locations set, set locations by navigating to desired destination directory and using 'ft set <key>' ")
os.Exit(1)
}

p := data.allPaths[data.cmd[1]]
path := sanitizeDir(p)
path := SanitizeDir(p)
fmt.Println(path)

}

func setDirectoryVar(data cmdArgs) {
func setDirectoryVar(data CmdArgs) {

key := data.cmd[1]
path, err := os.Getwd()
Expand All @@ -75,13 +75,13 @@ func setDirectoryVar(data cmdArgs) {

}

func displayAllPaths(data cmdArgs) {
func displayAllPaths(data CmdArgs) {

printMap(data.allPaths)

}

func removeKey(data cmdArgs) {
func removeKey(data CmdArgs) {

var res string
key := data.cmd[1]
Expand All @@ -107,7 +107,7 @@ func removeKey(data cmdArgs) {

}

func renameKey(data cmdArgs) {
func renameKey(data CmdArgs) {

originalKey := data.cmd[1]
newKey := data.cmd[2]
Expand Down Expand Up @@ -149,7 +149,7 @@ func renameKey(data cmdArgs) {

}

func showHelp(data cmdArgs) {
func showHelp(data CmdArgs) {

printMap(cmdDesc)

Expand Down
14 changes: 7 additions & 7 deletions ft/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPassCmd(t *testing.T) {
}

for _, tt := range tests {
got, err := passCmd(tt.args)
got, err := PassCmd(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("passCmd err %v, want err: %v", err, tt.wantErr)
return
Expand All @@ -45,7 +45,7 @@ func TestPassCmd(t *testing.T) {
}

func TestChangeDirectory(t *testing.T) {
data := cmdArgs{
data := CmdArgs{
cmd: []string{"to", "testKey"},
allPaths: map[string]string{
"testKey": "C:\\Users\\Test\\Documents",
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestSetDirectoryVar(t *testing.T) {
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()

data := cmdArgs{
data := CmdArgs{
cmd: []string{"set", "testKey"},
allPaths: make(map[string]string),
file: tmpfile,
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestSetDirectoryVar(t *testing.T) {
}

func TestDisplayAllPaths(t *testing.T) {
data := cmdArgs{
data := CmdArgs{
cmd: []string{"ls"},
allPaths: map[string]string{
"key1": "value1",
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestRemoveKey(t *testing.T) {
defer tmpfile.Close()

input := "y"
data := cmdArgs{
data := CmdArgs{
cmd: []string{"rm", "key1"},
allPaths: map[string]string{
"key1": "value1",
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestRenameKey(t *testing.T) {
defer tmpfile.Close()

input := "y"
data := cmdArgs{
data := CmdArgs{
cmd: []string{"rn", "key1", "newKey"},
allPaths: map[string]string{
"key1": "value1",
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestRenameKey(t *testing.T) {
}

func TestShowHelp(t *testing.T) {
data := cmdArgs{
data := CmdArgs{
cmd: []string{"help"},
allPaths: map[string]string{},
file: nil,
Expand Down
4 changes: 2 additions & 2 deletions ft/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (


// api
type cmdArgs struct {
type CmdArgs struct {
cmd []string
allPaths map[string]string
file *os.File
rdr io.Reader
}

// map of available commands
var availCmds = map[string]func(data cmdArgs){
var AvailCmds = map[string]func(data CmdArgs){
"to": changeDirectory,
"set": setDirectoryVar,
"ls": displayAllPaths,
Expand Down
2 changes: 1 addition & 1 deletion ft/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func printMap(hashmap map[string]string) {

}

func sanitizeDir(path string) string {
func SanitizeDir(path string) string {
distro := os.Getenv("WSL_DISTRO_NAME")
if len(distro) == 0 {

Expand Down
8 changes: 4 additions & 4 deletions ft/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func TestPrintMap(t *testing.T) {
func TestVerifyInput(t *testing.T) {
tests := []struct {
expected bool
data cmdArgs
data CmdArgs
wantErr bool
}{
{
true,
cmdArgs{
CmdArgs{
cmd: []string{},
allPaths: map[string]string{},
file: nil,
Expand All @@ -66,7 +66,7 @@ func TestVerifyInput(t *testing.T) {
},
{
false,
cmdArgs{
CmdArgs{
cmd: []string{},
allPaths: map[string]string{},
file: nil,
Expand All @@ -76,7 +76,7 @@ func TestVerifyInput(t *testing.T) {
},
{
false,
cmdArgs{
CmdArgs{
cmd: []string{},
allPaths: map[string]string{},
file: nil,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/osteensco/fastTravel
module github.com/osteensco/fastTravelCLI

go 1.20
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/osteensco/fastTravelCLI/ft"
)

Expand Down Expand Up @@ -48,30 +49,30 @@ func main() {
}
dataDirPath := filepath.Dir(exePath)
dataPath := dataDirPath + "\\fastTravel.bin"
file := ft.ensureData(dataPath)
file := ft.EnsureData(dataPath)
defer file.Close()
allPaths := ft.readMap(file)
allPaths := ft.ReadMap(file)

// sanitize input
inputCommand, err := ft.passCmd(os.Args)
inputCommand, err := ft.PassCmd(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
action := inputCommand[0]

// execute user provided action
exeCmd, ok := ft.availCmds[action]
exeCmd, ok := ft.AvailCmds[action]
if !ok {
fmt.Println("Invalid command, use 'help' for available commands.")
os.Exit(1)
}

data := ft.cmdArgs{
data := ft.CmdArgs{
cmd: inputCommand,
allPaths: allPaths,
file: file,
rdr: os.Stdin,
file: file,
rdr: os.Stdin,
}

exeCmd(data)
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"testing"

"github.com/osteensco/fastTravel/ft"
"github.com/osteensco/fastTravelCLI/ft"
)


Expand Down Expand Up @@ -70,7 +70,7 @@ func TestMainFunc(t *testing.T) {
},
{
[]string{"ft", "to", "key"},
fmt.Sprintf("%v\n",ft.sanitizeDir(tmpdir)),
fmt.Sprintf("%v\n",ft.SanitizeDir(tmpdir)),
},
{
[]string{"ft", "ls"},
Expand Down

0 comments on commit a894bdd

Please sign in to comment.