]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - xdg-ninja.sh
Update README
[xdg-ninja.git] / xdg-ninja.sh
1 #!/bin/bash
2
3 USE_GLOW=true
4 if ! command -v glow &> /dev/null
5 then
6 echo "Glow not found, markdown rendering not available."
7 USE_GLOW=false
8 fi
9
10 unalias -a
11
12 ERR=0
13 WARN=1
14 INFO=2
15 SUCS=3
16 HELP=4
17
18 # Function to expand enviornment variables in string
19 # https://stackoverflow.com/a/20316582/11110290
20 apply_shell_expansion() {
21 declare data="$1"
22 declare delimiter="__apply_shell_expansion_delimiter__"
23 declare command="cat <<$delimiter"$'\n'"$data"$'\n'"$delimiter"
24 eval "$command"
25 }
26
27 # Returns 0 if the path doesn't lead anywhere
28 # Return 1 if the path points to a file, 2 if it points to a directory
29 check_not_exists_file() {
30 FILE_PATH=$(apply_shell_expansion "$1")
31 if [ -f "$FILE_PATH" ]; then
32 return 1
33 elif [ -d "$FILE_PATH" ]; then
34 return 2
35 else
36 return 0
37 fi
38 }
39
40
41 # Function to handle the formatting of output
42 log() {
43 MODE="$1"
44 NAME="$2"
45 FILENAME="$3"
46 HELP="$4"
47
48 case "$MODE" in
49
50 ERR)
51 printf "[\e[1;31m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
52 ;;
53
54 WARN)
55 printf "[\e[1;33m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
56 ;;
57
58 INFO)
59 printf "[\e[1;36m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
60 ;;
61
62 SUCS)
63 printf "[\e[1;32m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
64 ;;
65
66 HELP)
67 if $USE_GLOW; then
68 echo "$HELP" | glow -
69 else
70 echo "$HELP"
71 fi
72 ;;
73
74 esac
75 }
76
77 # Checks that the given file does not exist, otherwise outputs help
78 check_file() {
79 INPUT="$1"
80 NAME="$2"
81
82 FILENAME=$(echo -E "$INPUT" | jq -r .path)
83 MOVABLE=$(echo -E "$INPUT" | jq -r .movable)
84 HELP=$(echo -E "$INPUT" | jq -r .help)
85
86 check_not_exists_file "$FILENAME"
87
88 case $? in
89
90 0)
91 log SUCS "$NAME" "$FILENAME" "$HELP"
92 ;;
93
94 1 | 2)
95 if "$MOVABLE"; then
96 log ERR "$NAME" "$FILENAME" "$HELP"
97 else
98 log WARN "$NAME" "$FILENAME" "$HELP"
99 fi
100 if ! [ -z "$HELP" ]; then
101 log HELP "$NAME" "$FILENAME" "$HELP"
102 else
103 log HELP "$NAME" "$FILENAME" "_No help available._"
104 fi
105 ;;
106
107 esac
108 }
109
110 # Reads a file from programs/, calls check_file on each file specified for the program
111 check_program() {
112 INPUT=$1
113
114 NAME=$(echo "$INPUT" | jq -r .name)
115
116
117 while IFS= read -r file; do
118 check_file "$file" "$NAME"
119 done <<< "$(echo "$INPUT" | jq -rc '.files[]')"
120 }
121
122 # Loops over all files in the programs/ directory and calls check_program
123 enumerate_programs() {
124 for prog_filename in ./programs/*; do
125 check_program "$(cat $prog_filename)"
126 done
127 }
128
129 enumerate_programs