-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
78 lines (64 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/osteensco/fastTravelCLI/ft"
)
// fastTravelCLI main process
func main() {
// identify exe path to establish a working directory and find dependency files
exePath, err := os.Executable()
if err != nil {
fmt.Println("Error:", err)
return
}
// handle piped args
err = ft.PipeArgs(&os.Args)
if err != nil {
fmt.Println("Error: ", err)
return
}
// sanitize user input
inputCommand, err := ft.PassCmd(os.Args)
if err != nil {
fmt.Println("Error: ", err)
return
}
action := inputCommand[0]
// grab command from registry
cmd, ok := ft.AvailCmds[action]
if !ok {
fmt.Printf("Invalid command '%s', use 'ft -h' for available commands. \n", action)
return
}
var dataDirPath string
var dataPath string
var file *os.File
var allPaths map[string]string
if cmd.LoadData {
// find persisted keys or create file to persist keys
dataDirPath = filepath.Dir(exePath)
dataPath = fmt.Sprintf("%s/fastTravel.bin", dataDirPath)
file, err = ft.EnsureData(dataPath)
if err != nil {
fmt.Println("Error: ", err)
return
}
defer file.Close()
// read keys into memory
allPaths, err = ft.ReadMap(file)
if err != nil {
fmt.Println("Error: ", err)
return
}
}
// manifest API
data := ft.NewCmdArgs(dataDirPath, inputCommand, allPaths, file, os.Stdin)
// execute user provided action
err = cmd.Callback(data)
if err != nil {
fmt.Println("fastTravelCLI returned an error: ", err)
return
}
}