-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmr
executable file
·261 lines (235 loc) · 9.41 KB
/
mr
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
261
#!/bin/bash
STGDIR="$HOME/.local/share/repo-maintain"
# Bold High Intensity
export BIBlack='\033[1;90m' # Black
export BIRed='\033[1;91m' # Red
export BIGreen='\033[1;92m' # Green
export BIYellow='\033[1;93m' # Yellow
export BIBlue='\033[1;94m' # Blue
export BIPurple='\033[1;95m' # Purple
export BICyan='\033[1;96m' # Cyan
export BIWhite='\033[1;97m' # White
# Colors
export BOLD=$(tput bold)
export NORMAL=$(tput sgr0)
export NC='\033[0m'
# Bold
export BBlack='\033[1;30m' # Black
export BRed='\033[1;31m' # Red
export BGreen='\033[1;32m' # Green
export BYellow='\033[1;33m' # Yellow
export BBlue='\033[1;34m' # Blue
export BPurple='\033[1;35m' # Purple
export BCyan='\033[1;36m' # Cyan
export BWhite='\033[1;37m' # White
function ask() {
local prompt default reply
if [[ ${2:-} = 'Y' ]]; then
prompt="${BIGreen}Y${NC}/${RED}n${NC}"
default='Y'
elif [[ ${2:-} = 'N' ]]; then
prompt="${GREEN}y${NC}/${BIRed}N${NC}"
default='N'
else
prompt="${GREEN}y${NC}/${RED}n${NC}"
fi
while true; do
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -ne "$1 [$prompt] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
if [[ -z "$DISABLE_PROMPTS" ]]; then
read -r reply < /dev/tty
else
echo "$default"
reply=$default
fi
# Default?
if [[ -z $reply ]]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
# fancy_message allows visually appealing output. Source the code block and run `fancy_message {info,warn,error} "What you want to say"`
function fancy_message() {
local MESSAGE_TYPE="${1}"
local MESSAGE="${2}"
case ${MESSAGE_TYPE} in
info) echo -e "[${BGreen}+${NC}] INFO: ${MESSAGE}";;
warn) echo -e "[${BRed}*${NC}] WARNING: ${MESSAGE}";;
error) echo -e "[${BRed}!${NC}] ERROR: ${MESSAGE}" >&2;;
*) echo -e "[${BOLD}?${NORMAL}] UNKNOWN: ${MESSAGE}";;
esac
}
function usage {
echo "$(basename $0) -h --> shows usage"
echo "$(basename $0) -I --> initializes a pacstall repo"
echo "$(basename $0) -A --> adds packages to the packagelist"
echo "$(basename $0) -T --> tests a package in a docker container"
}
case "$1" in
-I|--init)
# Checks if user gave -I input: `mr -I my_repo` would create my_repo and cd into it
if [[ -n "$2" ]]; then
# This will create a directory, but if mkdir fails (the directory exists already), then fancy_message error it and quit
mkdir -p "$2" 2>/dev/null || { fancy_message error "Directory already exists"; exit 1; }
cd "$2"
fi
# Else, check if pwd is empty
if [[ -z "$(\ls -A .)" ]]; then
fancy_message info "Creating repo in $(pwd)"
mkdir -p packages
touch packagelist
# Add neofetch to the repo, because why not (btw i use arch)
mkdir -p packages/neofetch
echo 'name="neofetch"
pkgname="neofetch"
version="7.1.0"
url="https://github.com/dylanaraps/neofetch/archive/7.1.0.zip"
optdepends=("w3m-img: image preview"
"libsixel-dev: image preview"
"catimg: image preview"
"jp2a: image preview"
"caca-utils: image preview"
"libcaca-dev: image preview")
description="Neofetch is a command-line system information tool written in bash 3.2+"
hash="e610da6561f17c8161337d0c4115da310be576e8699fa2add64e14eeb3f3dcf1"
prepare() {
true
}
build() {
true
}
install() {
sudo make install DESTDIR=/usr/src/pacstall/neofetch
}' > packages/neofetch/neofetch.pacscript
# Create a git repo
git init -q
# Make github detect anything ending with .pacscript as a shell script
echo '*.pacscript linguist-language=Shell' > .gitattributes
echo 'neofetch' > packagelist
# Add and commit it
git commit -am "add: neofetch" &>/dev/null
exit 0
# If the directory does contain something
else
fancy_message warn "Directory not empty"
exit 1
fi
;;
-U|--update)
DIR=$(pwd)
if ! cd packages; then
fancy_message error "Cannot enter packages directory"
exit 1
else
# This fancy line will output the contents of the current directory in a single line, alphabeticaly ordered
\ls -1aA > ../packagelist
git commit -am "upd(packagelist)"
fi
# Then cd back into where we were before
cd -
;;
-A|--add)
# Checks if user inputed anything
if [[ -z $2 ]]; then
fancy_message error "Specify a pacscript"
exit 1
fi
# Checks for broken repos
if ! [[ -f packagelist ]]; then
fancy_message error "This repo is broken (missing packagelist)"
exit 1
fi
if [[ -d packages ]]; then
fancy_message error "This repo is broken (missing packages dir)"
fi
INPUT="$2"
# Check if beginning of input contains http (I'll assume that means the input is a url) and if it is true, then trigger the download process of that file
if [[ $INPUT == http* ]]; then
# This will get the ending of the url, so `https://raw.githubusercontent.com/pacstall/pacstall-programs/master/packages/blang-git/blang-git.pacscript` -> `blang-git.pacscript` and then cutting of the `.pacscript` at the end so we can identify the package name
PACSCRIPT=$(echo "${NAME##*/}" | cut -d. -f1)
# Make the directory
mkdir -p packages/"$PACSCRIPT"
# Download with a spicy progress bar to `packages/$PACSCRIPT`
wget -q --show-progress --progress=bar:force "$INPUT" -P packages/"$PACSCRIPT" 2>&1
# If the beginning of the input does not contain `http`, then assume the input is a local file
else
NAME=$(echo $INPUT | cut -d. -f1 | sed 's|.*/||')
mkdir -p packages/$NAME
# This is not a typo. Putting \ in front of a command makes it bypass aliases, and a lot of distros alias `cp` -> `cp -i` but we don't want that
\cp -rf $INPUT packages/$NAME/
fi
;;
-T|--test)
if [[ -z $2 ]]; then
fancy_message error "Specify a pacscript"
exit 1
fi
# Get's date for tmp file
DATE=$(date +%s%N)
# Get's full input, could be ~/myscript.pacscript or just mypacscript.pacscript
INPUT="$2"
# Get's the basename, so `/home/henry/mypacscript.pacscript` -> `mypacscript.pacscript`
FILE=$(basename $INPUT)
# Get the name of a file, so `alacritty.pacscript` -> `alacritty`
NAME=$(echo $FILE | cut -d. -f1)
# Get's the name of $FILE without the .pacscript extension
BASENAME=$(echo $INPUT | cut -d. -f1)
# Pulls ubuntu image if not installed or if installed, checks for updates
docker pull ghcr.io/pacstall/pacstall:latest
# copy our default docker script to /tmp
mkdir -p "$STGDIR"
mkdir -p /tmp/mr-$DATE
cp "$STGDIR"/docker.sh /tmp/mr-$DATE/docker.sh
# This is complicated, so heres a simple example (the pacscript won't work, but just imagine it's a full script)
#
# $ cat my.pacscript
# name="test"
# url="https://github.com/test/test"
#
# Will turn into this:
#
# echo 'name="test"
# url="https://github.com/test/test"' > my.pacscript
#
# So basically we're taking the contents of the pacscript, and turning it into a mini script inside echo that could be run by bash
echo "echo '$(cat $INPUT)"\'" > $FILE" | tee -a /tmp/mr-$DATE/docker.sh >/dev/null
# Add the final bit that will append the command to install the pacscript in the docker container
echo "pacstall -Il $NAME" | tee -a /tmp/mr-$DATE/docker.sh >/dev/null
docker container run -i pacstall:latest /bin/bash -s < /tmp/mr-"$DATE"/docker.sh
;;
-B|--deb)
URL="$2"
wget -N "$URL" -q --show-progress
INPUT=$(basename "$URL")
_NAME=$(dpkg -f "$INPUT" Package)
_VERSION=$(dpkg -f "$INPUT" Version)
_DESCRIPTION=$(dpkg -f "$INPUT" Description | awk '{$1=$1};1' | sed '/^$/d')
_HASH=$(sha256sum "$INPUT" | cut -d' ' -f1)
if [[ -z "$2" ]]; then
echo -n "Enter maintainer info (myname <myemail.potato.com>): "
read -r _MAINTAINER
else
_MAINTAINER="$2"
fi
echo name=""${_NAME}-deb"" | tee "${_NAME}-deb.pacscript" > /dev/null
echo gives=""$_NAME"" | tee -a "${_NAME}-deb.pacscript" > /dev/null
echo version=""${_VERSION}"" | tee -a "${_NAME}-deb.pacscript" > /dev/null
echo url=""${URL}"" | tee -a "${_NAME}-deb.pacscript" > /dev/null
echo description=""${_DESCRIPTION}"" | tee -a "${_NAME}-deb.pacscript" > /dev/null
echo hash=""${_HASH}"" | tee -a "${_NAME}-deb.pacscript" > /dev/null
echo maintainer=""${_MAINTAINER}"" | tee -a "${_NAME}-deb.pacscript" > /dev/null
rm -f "${INPUT}"
;;
*)
usage
exit 1
;;
esac
# Other commands that are useful but are too small to put into a flag
# curl -s https://raw.githubusercontent.com/pacstall/pacstall-programs/master/packagelist | wc -l # gets total package count from pacstall/pacstall-programs repo