-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
135 lines (108 loc) · 3.35 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/wassimbj/lnk/cli"
"github.com/wassimbj/lnk/utils"
)
var (
invalidNumOfParams = errors.New("Expected at least 2 params")
invalidSubCmds = errors.New("invalid subcommands")
invalidLink = errors.New("invalid link")
)
const (
dataFileName = "_lnk_data.txt" // where your saved links will live
limit = 4
separator = "~~" // seperated the link and its description inside the dataFileName
usage = `
lnk <new | list | rm> [url|ID]
* new <url>: creates a new link
* list: Lists the saved links
* rm <ID>: remove a saved link
`
)
func main() {
args := os.Args[1:]
dataFilePath, dpError := utils.GetDataFilePath(dataFileName)
if dpError != nil {
utils.PrintMsg(dpError.Error())
os.Exit(1)
}
switch args[0] {
case "new":
if len(args) <= 1 {
utils.PrintMsg("error", "\t %s", invalidNumOfParams.Error())
os.Exit(1)
}
link := args[1]
if link == "" {
color.Red("\n\n %s \n\n", invalidLink.Error())
os.Exit(1)
}
err := cli.NewLnk(link, dataFilePath)
if err != nil {
utils.PrintMsg("error", err.Error())
os.Exit(1)
}
utils.PrintMsg("success", "\t Success! Link is saved")
case "list":
f, _ := utils.OpenFile(dataFilePath, os.O_RDONLY)
defer f.Close()
scanner := bufio.NewScanner(f)
var i = 0
color.New(color.BlinkSlow, color.Bold, color.BgHiGreen).Print("\n Saved Links \n")
for i = 0; scanner.Scan(); i++ {
// formatting the display
if i == 0 {
fmt.Print("\n\n")
}
// separate the link and title (link description)
idxOfSep := strings.Index(scanner.Text(), separator)
link := scanner.Text()[0 : idxOfSep-1]
title := scanner.Text()[idxOfSep+3:]
linkId := i + 1
// it's like a card
color.New(color.CrossedOut, color.FgHiBlack).Println("-----------------------------------------------------------------")
color.New(color.Bold, color.FgHiBlue).Printf("\r %d - %s", linkId, strings.TrimSpace(link))
color.New(color.FgHiMagenta, color.Italic).Printf("\n\n * %s \n", title)
color.New(color.CrossedOut, color.FgHiBlack).Println("-----------------------------------------------------------------")
}
case "rm":
if len(args) <= 1 {
utils.PrintMsg("error", "\t %s", invalidNumOfParams.Error())
os.Exit(1)
}
linkId := args[1]
fileContent, _ := ioutil.ReadFile(dataFilePath)
deletedLink := ""
// find the link to delete, and remove it from the array
dataArr := strings.Split(string(fileContent), "\n")
for i := 0; i < len(dataArr); i++ {
linkIdInt, _ := strconv.Atoi(linkId)
if i+1 == linkIdInt {
deletedLink = strings.Split(dataArr[i], separator)[0]
dataArr = append(dataArr[:i], dataArr[i+1:]...)
} else if i == len(dataArr) {
utils.PrintMsg("error", "the id doesn't exist, try lnk list to see links with its id")
}
}
// re-write the file with the new data
newData := strings.Join(dataArr, "\n")
f, _ := utils.OpenFile(dataFilePath, os.O_WRONLY|os.O_TRUNC)
if err := utils.AppendToFile(f, newData); err != nil {
utils.PrintMsg("error", "Can't delete the link, Error: "+err.Error())
}
utils.PrintMsg("success", "\t Removed! ["+strings.TrimSpace(deletedLink)+"]")
case "usage":
fmt.Printf("\n %s \n", usage)
default:
utils.PrintMsg("error", "\t", invalidSubCmds.Error())
os.Exit(1)
}
}