-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmark
executable file
·156 lines (147 loc) · 4.46 KB
/
bookmark
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Presets
configFile="$HOME/.bookmark.conf.yaml"
storeDirectory=$(grep "storeDirectory" $configFile | awk -F": " '{ print $2 }')
open=0
view=0
list=0
pass=0
# Setup bookmark folder and config file
if [[ "$1" == "init" ]]
then
## Create a new bookmark store and config
if [ -d $HOME/.bookmark-store ]
then
echo -n "$HOME/.bookmark-store folder already exists, overwrite? (y/N): "
read overwriteStore
if [[ "$overwriteStore" == "y" ]]
then
echo "Removing old directory..."
rm -rf $HOME/.bookmark-store
echo -n "Enter location for bookmark store (Default: $HOME/.bookmark-store): "
read storeDir
if [[ "$newLocation" == "" ]]
then
storeDir=$HOME/.bookmark-store
fi
mkdir $storeDir
fi
else
echo -n "Enter location for bookmark store (Default: $HOME/.bookmark-store): "
read storeDir
if [[ "$newLocation" == "" ]]
then
storeDir=$HOME/.bookmark-store
fi
mkdir $storeDir
fi
if [ -f $HOME/.bookmark.conf.yaml ]
then
echo -n "$HOME/.bookmark.conf.yaml already exists, overwrite? (y/N): "
read overwriteConfig
if [[ "$overwriteConfig" == "y" ]]
then
rm $HOME/.bookmark.conf.yaml
echo "storeDirectory: $storeDir" >> $HOME/.bookmark.conf.yaml
fi
fi
exit 0
elif [[ "$1" == "" ]]
then
## Default action to show bookmark tree
tree -C $storeDirectory | sed "s/.yaml//g" | sed '$ d' | sed '$ d' | sed '1 d' | sed "s/^└──//" | sed "s/^├──//" | sed "s/^│ //" | sed "s/^ //"
fi
# Check if pass is available
if [[ "$(grep pass $configFile | awk -F": " '{ print $2 }')" == "installed" ]]
then
passInstalled=1
else
passTest=$(pass --help)
if [[ "$passTest" != "-bash: pass: command not found" ]]
then
passInstalled=1
echo "pass: installed" >> $configFile
fi
fi
while getopts b:c:ehopv o
do
case "$o" in
b) bookmark="$storeDirectory/${OPTARG}.yaml"
url=$(grep "url" $bookmark | awk -F": " '{ print $2 }')
;;
c) newBookmarkArg="${OPTARG}"
newBookmark="$(echo $newBookmarkArg | awk -F"/" '{ print $NF }').yaml"
newBookmarkPathArr=($(echo $newBookmarkArg | sed 's/\// /g'))
unset 'newBookmarkPathArr[${#newBookmarkPathArr[@]}-1]'
currentPath=$storeDirectory
for i in ${newBookmarkPathArr[@]}
do
if [ ! -d "$currentPath/$i" ]
then
mkdir $currentPath/$i
fi
currentPath="$currentPath/$i"
done
echo -n "url: "
read newURL
echo "url: $newURL" > $currentPath/$newBookmark
echo -n "browser: "
read newBrowser
if [[ "newBrowser" != "" ]]
then
echo -e "browser:\n $(hostname -f): $newBrowser" >> $currentPath/$newBookmark
fi
if [[ $passInstalled -eq 1 ]]
then
echo -n "pass path: "
read newPass
if [[ "$newPass" != "" ]]
then
echo "pass: $newPass" >> $currentPath/$newBookmark
fi
fi
echo "Added $currentPath/$newBookmark to the bookmark store"
exit 0
;;
e) vi $bookmark
exit 0
;;
h) echo -e "bookmark, version 0.2\n\nusage: bookmark [-b|c bookmark path] [-ehlopv]\n\n b : Path of bookmark for use.\n c : Creates a new bookmark.\n e : Opens specified bookmark in vi.\n h : Displays this help section.\n l : Shows a tree of all bookmarks in the store.\n o : Open the bookmark in the browser specified in the bookmakr YAML file or the default browser if that is not set.\n p : If pass is installed and the path to the pass file for this site is in the bookmark YAML file, this will copy the password.\n v : View the url.\n"
;;
o) open=1;;
p) pass=1;;
v) view=1;;
esac
done
# Opens URL in browser specified in the config file or in the bookmark file
if [[ $open -eq 1 ]]
then
browser=$(sed -n -e "/browser/,/ $(hostname -f)/p" $bookmark | grep "$(hostname -f)" | awk -F": " '{ print $2 }' | sed "s/'//g")
# Check to use default browser
if [ "$browser" == "" ]
then
browser=$(sed -n -e "/browser/,/ $(hostname -f)/p" $configFile | grep "$(hostname -f)" | awk -F": " '{ print $2 }' | sed "s/'//g")
fi
if [[ "$(uname)" == "Darwin" ]]
then
open -a "$browser" $url
elif [[ "$(uname)" == "Linux" ]]
then
exec "$browser" "$url"
fi
fi
# Send url to stdout
if [[ $view == 1 ]]
then
echo $url
fi
if [[ $pass -eq 1 ]]
then
password=$(grep "pass" $bookmark | awk -F": " '{ print $2 }' | sed "s/\'//g")
if [[ "$password" == "" ]]
then
echo "No password path has been assigned in the file."
else
pass -c "$password"
fi
fi