summarylogtreecommitdiffstats
path: root/dossier
blob: 85d7533be374dc39008e30a8efa649507eca47e8 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash

ProgName="$(basename -- "$0")"
ProgName="${ProgName:-dossier}"
Version="0.0.5"

sub_help(){
    echo -e "\e[38;5;212;1mUsage:\e[0;38;5;114m $ProgName \e[0;38;5;230;1m<\e[0;38;5;230msubcommand\e[1m>\e[0m"
    echo -e "\e[38;5;212;1mSubcommands:\e[0m"
    echo -e "    \e[38;5;114minstall     \e[38;5;230;1mInstall dotfiles in the current directory\e[0m"
    echo -e "    \e[38;5;114muninstall   \e[38;5;230;1mUninstall dotfiles in the current directory\e[0m"
    echo -e "    \e[38;5;114mreinstall   \e[38;5;230;1mReinstall dotfiles in the current directory\e[0m"
    echo -e "    \e[38;5;114minit        \e[38;5;230;1mCreate a basic dossier.config.json\e[0m"
    echo -e "    \e[38;5;114mpreview     \e[38;5;230;1mShow where files will be symlinked\e[0m"
    echo -e "    \e[38;5;114madd         \e[38;5;230;1mAdd a file to your dossier config\e[0m"
    echo -e "    \e[38;5;114mremove      \e[38;5;230;1mRemove a file from your dossier config\e[0m"
    echo ""
    echo -e "    \e[38;5;114mhelp        \e[38;5;230;1mShow this text\e[0m"
    echo ""
}

sub_h(){
    sub_help
}

sub_version() {
    echo -e "\e[38;5;114;1mVersion               \e[38;5;212m$Version\e[0m"
    echo -e "\e[38;5;114;1mCurrent Maintainer    \e[38;5;212mCallMeEcho\e[0m\n"
    echo -e "\e[38;5;114;1mContributors          \e[38;5;212mkawaegle
                      \e[38;5;212mmekb the turtle\e[0m"
}

sub_v() {
    sub_version
}

sub_install() {
    if [ -f "$HOME/.config/dossier.lock" ]; then
        echo "Dossier has already installed your dotfiles! Please use dossier uninstall"
        exit
    fi
    if [ -f "./dossier.config.json" ]; then
        IFS=$'\n'
        gum confirm "Are you sure you want to install these dotfiles?" && mkdir -p "$HOME/.config/dossier" && touch "$HOME/.config/dossier.lock" &&
        for row in $(jq -Mrc '.files[]' < dossier.config.json); do
            _jq() {
                jq -Mrc "${1}" <<< "${row}"
            }
            NAME="$(_jq '.name')"
            LOCATION="$(_jq '.location' | sed "s|^~|$HOME|")"
            if [ -h "${LOCATION}/${NAME}" ]; then
                echo -e "\e[38;5;212mLink Exists ${PWD}/${NAME} -> ${LOCATION}/${NAME}\e[0m"
            else
                if [ "$(_jq '.type')" == 'same' ]; then
                    echo -e "\e[38;5;212m${PWD}/${NAME} -> ${LOCATION}/${NAME}\e[0m"
                    rm -rf "${LOCATION:?}/${NAME}" &>/dev/null
                    ln -s "${PWD}/${NAME}" "${LOCATION}/${NAME}" &>/dev/null
                else
                    echo -e "\e[38;5;212m${PWD}/${NAME} -> ${LOCATION}\e[0m"
                    rm -rf "${LOCATION:?}" &>/dev/null
                    ln -s "${PWD}/${NAME}" "${LOCATION}" &>/dev/null
                fi
            fi
        done && echo -e "\e[38;5;114mAll links have been set up\e[0m" &&
        for row in $(jq -Mrc '.commands[]' < dossier.config.json); do
            _jq() {
                jq -Mrc "${1}" <<< "${row}"
            }
            COMMAND="$(_jq '.command')"
            MESSAGE="$(_jq '.message')"
            gum spin --title "$MESSAGE" -s line -- bash -c "$COMMAND" && echo -e "\e[38;5;114m$(_jq '.donemessage')\e[0m"
        done && mkdir -p "$HOME/.config/"
    else
        echo "This directory does not have a dossier config!"
    fi
}

sub_i() {
    sub_install
}

sub_preview() {
    if [ -f "./dossier.config.json" ]; then
        IFS=$'\n'
        for row in $(jq -Mrc '.files[]' < dossier.config.json); do
            _jq() {
                jq -Mrc "${1}" <<< "${row}"
            }
            NAME="$(_jq '.name')"
            LOCATION="$(_jq '.location' | sed "s|^~|$HOME|")"
            if [ -h "${LOCATION}/${NAME}" ]; then
                echo -e "\e[38;5;212mLink Exists ${PWD}/${NAME} -> ${LOCATION}/${NAME}\e[0m"
            else
                if [ "$(_jq '.type')" == 'same' ]; then
                    echo -e "\e[38;5;212m${PWD}/${NAME} -> ${LOCATION}/${NAME}\e[0m"
                else
                    echo -e "\e[38;5;212m${PWD}/${NAME} -> ${LOCATION}\e[0m"
                fi
            fi
        done &&
        mkdir -p "$HOME/.config/"
    else
        echo "This directory does not have a dossier config!"
    fi
}

sub_p() {
    sub_preview
}

sub_reinstall() {
    if [ -f "./dossier.config.json" ]; then
        IFS=$'\n'
        gum confirm "Are you sure you want to reinstall these dotfiles?" && mkdir -p "$HOME/.config/dossier" && touch "$HOME/.config/dossier.lock" &&
        for row in $(jq -Mrc '.files[]' < dossier.config.json); do
                _jq() {
                    jq -Mrc "${1}" <<< "${row}"
                }
                NAME="$(_jq '.name')"
                LOCATION="$(_jq '.location' | sed "s|^~|$HOME|")"
            if [ "$(_jq '.type')" == 'same' ]; then
                rm -rf "${LOCATION:?}/${NAME}"
            else
                rm -f "${LOCATION:?}"
            fi
        done &&
        for row in $(jq -Mrc '.files[]' < dossier.config.json); do
            _jq() {
                jq -Mrc "${1}" <<< "${row}"
            }
            NAME="$(_jq '.name')"
            LOCATION="$(_jq '.location' | sed "s|^~|$HOME|")"
            if [ ! -h "${LOCATION}/${NAME}" ]; then
                if [ "$(_jq '.type')" == 'same' ]; then
                    echo -e "\e[38;5;212m${PWD}/${NAME} -> ${LOCATION}/${NAME}\e[0m"
                    rm -rf "${LOCATION:?}/${NAME}" &>/dev/null
                    ln -s "${PWD}/${NAME}" "${LOCATION}/${NAME}" &>/dev/null
                else
                    echo -e "\e[38;5;212m${PWD}/${NAME} -> ${LOCATION}\e[0m"
                    rm -rf "${LOCATION:?}" &>/dev/null
                    ln -s "${PWD}/${NAME}" "${LOCATION}" &>/dev/null
                fi
            fi
        done && echo -e "\e[38;5;114mAll links have been set up\e[0m" &&
        for row in $(jq -Mrc '.commands[]' < dossier.config.json); do
            _jq() {
                jq -Mrc "${1}" <<< "${row}"
            }
            COMMAND="$(_jq '.command')"
            MESSAGE="$(_jq '.message')"
            gum spin --title "$MESSAGE" -s line -- bash -c "$COMMAND" && echo -e "\e[38;5;114m$(_jq '.donemessage')\e[0m"
        done && mkdir -p "$HOME/.config/"
    else
        echo "This directory does not have a dossier config!"
    fi
}

sub_uninstall() {
    if [ ! -f "$HOME/.config/dossier.lock" ]; then
        echo "Dossier hasn't installed your dotfiles yet!"
        exit
    fi
    IFS=$'\n'
    gum confirm "Are you sure you want to uninstall these dotfiles?
     This could lead to a broken desktop!" &&
    for row in $(jq -Mrc '.files[]' < dossier.config.json); do
        _jq() {
            jq -Mrc "${1}" <<< "${row}"
        }
        NAME="$(_jq '.name')"
        LOCATION="$(_jq '.location' | sed "s|^~|$HOME|")"
        if [ "$(_jq '.type')" == 'same' ]; then
            rm -rf "${LOCATION:?}/${NAME}"
            echo -e "\e[38;5;212mRemoved ${LOCATION:?}/${NAME}\e[0m"
        else
            rm -f "${LOCATION:?}"
            echo -e "\e[38;5;212mRemoved ${LOCATION:?}\e[0m"
        fi
    done && echo -e "\e[38;5;114mAll links have been removed\e[0m" && rm -f "$HOME/.config/dossier.lock"
}

sub_add() {
    if [ -f "./dossier.config.json" ]; then
        NAME=$(gum input --placeholder "What file should i track?")
        if [ -z "$NAME" ]; then
            echo "You need to specify what file to track!"
            exit
        fi
        EXISTS=$(jq ".files[] | select(.name == \"$NAME\")" < dossier.config.json)
        if [ -n "$EXISTS" ]; then
            echo "That file has already been added!"
            exit
        fi
        LOCATION=$(gum input --placeholder "Where should that file be symlinked? (eg ~/.config)")
        if [ -z "$LOCATION" ]; then
            echo "You need to specify where to symlink to!"
            exit
        fi
        echo "Is this file the same name as it will be when copied or different?"
        FILEFOLDER=$(gum choose same different)
        if [ -z "$FILEFOLDER" ]; then
            echo "You need to specify if this is a file or folder!"
            exit
        fi
        out="$(jq ".files += [{\"name\": \"$NAME\", \"location\": \"$LOCATION\", \"type\": \"$FILEFOLDER\"}]" < dossier.config.json)"
        echo $out > dossier.config.json
        if [ "$FILEFOLDER" == 'same' ]; then
            echo -e "\e[38;5;212m${PWD}/${NAME} will be symlinked to ${LOCATION}/${NAME} next time you install!\e[0m"
        else
            echo -e "\e[38;5;212m${PWD}/${NAME} will be symlinked to $LOCATION next time you install!\e[0m "
        fi
    else
        echo "This directory does not have a dossier config!"
    fi
}

sub_a() {
    sub_add
}

sub_remove() {
    if [ -f "./dossier.config.json" ]; then
        NAME=$(gum input --placeholder "What file should i stop tracking?")
        if [ -z "$NAME" ]; then
            echo "You need to specify what file to stop tracking!"
            exit
        fi
        out="$(jq "del(.files[] | select(.name == \"$NAME\"))" < dossier.config.json)"
        echo $out > dossier.config.json
    else
        echo "This directory does not have a dossier config!"
    fi
}

sub_r() {
    sub_remove
}

sub_init() {
    echo '{
    "files": [],
    "commands": []
}' > dossier.config.json
}

subcommand=$1
case $subcommand in
    "" | "-h" | "--help")
        sub_help
        ;;
    *)
        shift
        sub_"${subcommand}" "$@"
        if [ $? = 127 ]; then
            echo "Error: '$subcommand' is not a known subcommand." >&2
            echo "       Run '$ProgName --help' for a list of known subcommands." >&2
            exit 1
        fi
        ;;
esac