-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
executable file
·405 lines (375 loc) · 11.9 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// usr/bin/env go run findURL.go fsearch.go info.go install.go listBinaries.go main.go remove.go run.go update.go utility.go fetch.go config.go "$@"; exit $?
// dbin - 📦 Poor man's package manager. The easy to use, easy to get, suckless software distribution system
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/xplshn/a-utils/pkg/ccmd"
)
// Verbosity is used along with >= and <= to determine which messages to hide when using `--silent` and which messages to display when using `--verbose`
type Verbosity int8
const (
unsupportedArchMsg = "Unsupported architecture: "
indicator = "...>"
Version = "0.7"
maxCacheSize = 10
binariesToDelete = 5
normalVerbosity Verbosity = 1
extraVerbose Verbosity = 2
silentVerbosityWithErrors Verbosity = -1
extraSilent Verbosity = -2
)
func main() {
cmdInfo := &ccmd.CmdInfo{
Authors: []string{"xplshn"},
Repository: "https://github.com/xplshn/dbin",
Name: "dbin",
Synopsis: "[-v|-h] [list|install|remove|update|run|info|search|tldr|eget2] <-args->",
Description: "The easy to use, easy to get, software distribution system",
CustomFields: map[string]interface{}{
"1_Options": `-h, --help Show this help message
-v, --version Show the version number`,
"2_Commands": `list List all available binaries
install, add Install a binary
remove, del Remove a binary
update Update binaries, by checking their SHA against the repo's SHA
run Run a specified binary from cache
info Show information about a specific binary OR display installed binaries
search Search for a binary - (not all binaries have metadata. Use list to see all binaries)
tldr Equivalent to "run --transparent --silent tlrc"
eget2 Equivalent to "run --transparent --silent eget2"`,
"3_Variables": `DBIN_CACHEDIR If present, it must contain a valid directory path
DBIN_INSTALL_DIR If present, it must contain a valid directory path
DBIN_NOTRUNCATION If present, and set to ONE (1), string truncation will be disabled
DBIN_REPO_URLS If present, it must contain one or more repository URLS ended in / separated by ;
DBIN_METADATA_URLS If present, it must contain one or more repository's metadata url separated by ;`,
"4_Examples": `dbin search editor
dbin install micro.upx
dbin install lux kakoune aretext shfmt
dbin --silent install bed && echo "[bed] was installed to $INSTALL_DIR/bed"
dbin del bed
dbin del orbiton tgpt lux
dbin info
dbin info | grep a-utils | xargs dbin add # install the entire a-utils suite
dbin info jq
dbin list --described
dbin tldr gum
dbin --verbose run curl -qsfSL "https://raw.githubusercontent.com/xplshn/dbin/master/stubdl" | sh -
dbin --silent run elinks -no-home "https://fatbuffalo.neocities.org/def"
dbin --silent run --transparent micro ~/.profile
dbin run btop`,
},
}
helpPage, err := cmdInfo.GenerateHelpPage()
if err != nil {
fmt.Fprintln(os.Stderr, "error generating help page:", err)
os.Exit(1)
}
flag.Usage = func() {
print(helpPage)
}
// Define and parse flags
verboseFlag := flag.Bool("verbose", false, "Run in extra verbose mode")
silentFlag := flag.Bool("silent", false, "Run in silent mode, only errors will be shown")
extraSilentFlag := flag.Bool("extra-silent", false, "Run in extra silent mode, suppressing almost all output")
versionFlag := flag.Bool("v", false, "Show the version number")
longVersionFlag := flag.Bool("version", false, "Show the version number")
flag.Parse()
if *versionFlag || *longVersionFlag {
fmt.Println(helpPage, "dbin version", Version)
os.Exit(1)
}
// Check for conflicting flags
if (*verboseFlag && *silentFlag) || (*verboseFlag && *extraSilentFlag) || (*silentFlag && *extraSilentFlag) {
fmt.Fprintln(os.Stderr, "error: Conflicting verbose flags provided.")
os.Exit(1)
}
// determineVerbosity determines the verbosity level based on the flags provided.
determineVerbosity := func(
silentFlag, verboseFlag, extraSilentFlag bool,
normalVerbosity, extraVerbose, silentVerbosityWithErrors, extraSilent Verbosity,
) Verbosity {
switch {
case extraSilentFlag:
return extraSilent
case silentFlag:
return silentVerbosityWithErrors
case verboseFlag:
return extraVerbose
default:
return normalVerbosity
}
}
// Determine verbosity level
verbosityLevel := determineVerbosity(
*silentFlag,
*verboseFlag,
*extraSilentFlag,
normalVerbosity,
extraVerbose,
silentVerbosityWithErrors,
extraSilent,
)
args := flag.Args()
if len(args) < 1 {
print(helpPage)
os.Exit(1)
}
command := args[0]
args = args[1:]
// Load configuration
config, err := LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "error loading config: %v\n", err)
os.Exit(1)
}
// Fetch the metadata ONLY once
var metadata map[string]interface{}
fetchMetadata := func() map[string]interface{} {
for _, url := range config.MetadataURLs {
err := fetchJSON(url, &metadata)
if err != nil {
fmt.Printf("failed to fetch and decode binary information from %s: %v\n", url, err)
continue
}
}
return metadata
}
switch command {
case "findurl":
if len(args) < 1 {
fmt.Println("No binary names provided for findurl command.")
os.Exit(1)
}
binaryNames := args
fetchMetadata()
urls, _, err := findURL(config, binaryNames, verbosityLevel, metadata)
if err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "%v", err)
}
os.Exit(1)
}
if verbosityLevel >= normalVerbosity {
for i, url := range urls {
fmt.Printf("URL for %s: %s\n", binaryNames[i], url)
}
}
case "fullname":
if len(args) < 1 {
fmt.Println("No binary name was provided for fullname")
os.Exit(1)
}
fullName, _ := getFullName(args[0])
fmt.Println("fullName of", args[0], "is", fullName)
case "install", "add":
if len(args) < 1 {
fmt.Println("No binary name provided for install command.")
os.Exit(1)
}
binaries := args
fetchMetadata()
err := installCommand(config, binaries, verbosityLevel, metadata)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
// fmt.Println("Installation completed successfully.")
case "remove", "del":
if len(args) < 1 {
fmt.Println("No binary name provided for remove command.")
os.Exit(1)
}
binaries := args
err := removeBinaries(config, binaries, verbosityLevel, metadata)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
// fmt.Println("Removal completed successfully.")
case "list":
if len(os.Args) == 3 {
if os.Args[2] == "--described" || os.Args[2] == "-d" {
// Call fSearch with an empty query and a large limit to list all described binaries
fetchMetadata()
fSearch(config, "", metadata)
} else {
errorOut("dbin: Unknown command.\n")
}
} else {
fetchMetadata()
binaries, err := listBinaries(metadata)
if err != nil {
fmt.Println("Error listing binaries:", err)
os.Exit(1)
}
for _, binary := range binaries {
fmt.Println(binary)
}
}
case "search":
queryIndex := 0
if len(args) < queryIndex+1 {
fmt.Println("Usage: dbin search <--limit||-l [int]> [query]")
os.Exit(1)
}
if len(args) > 0 && (args[queryIndex] == "--limit" || args[queryIndex] == "-l") {
if len(args) > queryIndex+1 {
var err error
config.Limit, err = strconv.Atoi(args[queryIndex+1])
if err != nil {
fmt.Printf("error: 'limit' value is not an int: %v\n", err)
os.Exit(1)
}
queryIndex += 2
} else {
fmt.Println("error: Missing 'limit' value.")
os.Exit(1)
}
}
if len(args) <= queryIndex {
fmt.Println("Usage: dbin search <--limit||-l [int]> [query]")
os.Exit(1)
}
query := args[queryIndex]
fetchMetadata()
err := fSearch(config, query, metadata)
if err != nil {
fmt.Printf("error searching binaries: %v\n", err)
os.Exit(1)
}
case "info":
var binaryName string
var remote bool
// Check for flags: --remote or -r
for _, arg := range args {
if arg == "--remote" || arg == "-r" {
remote = true
} else if binaryName == "" { // The first non-flag argument is treated as binaryName
binaryName = arg
}
}
if binaryName == "" {
if !remote {
// Get the list of files in the installation directory
files, err := listFilesInDir(config.InstallDir)
if err != nil {
fmt.Printf("error listing files in %s: %v\n", config.InstallDir, err)
os.Exit(1)
}
installedPrograms := make([]string, 0)
// Loop over the files and check if they are installed
for _, file := range files {
fullBinaryName := listInstalled(file)
if fullBinaryName != "" {
installedPrograms = append(installedPrograms, fullBinaryName)
}
}
// Print the installed programs
for _, program := range installedPrograms {
fmt.Println(program)
}
} else {
// Validate programs from the remote source
fetchMetadata()
installedPrograms, err := validateProgramsFrom(config, nil, metadata)
if err != nil {
fmt.Printf("error validating programs: %v\n", err)
os.Exit(1)
}
// Print the installed programs
for _, program := range installedPrograms {
fmt.Println(program)
}
}
} else {
fetchMetadata()
binaryInfo, err := getBinaryInfo(config, binaryName, metadata)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
// Print detailed binary information
fmt.Printf("Name: %s\n", binaryInfo.RealName)
if binaryInfo.Description != "" {
fmt.Printf("Description: %s\n", binaryInfo.Description)
}
if binaryInfo.Note != "" {
fmt.Printf("Note: %s\n", binaryInfo.Note)
}
if binaryInfo.Version != "" {
fmt.Printf("Version: %s\n", binaryInfo.Version)
}
if binaryInfo.DownloadURL != "" {
fmt.Printf("Download URL: %s\n", binaryInfo.DownloadURL)
}
if binaryInfo.Size != "" {
fmt.Printf("Size: %s\n", binaryInfo.Size)
}
if binaryInfo.Bsum != "" {
fmt.Printf("B3SUM: %s\n", binaryInfo.Bsum)
}
if binaryInfo.Shasum != "" {
fmt.Printf("SHA256: %s\n", binaryInfo.Shasum)
}
if binaryInfo.BuildDate != "" {
fmt.Printf("Build Date: %s\n", binaryInfo.BuildDate)
}
if binaryInfo.SrcURL != "" {
fmt.Printf("Source URL: %s\n", binaryInfo.SrcURL)
}
if binaryInfo.WebURL != "" {
fmt.Printf("Web URL: %s\n", binaryInfo.WebURL)
}
if binaryInfo.BuildScript != "" {
fmt.Printf("Build Script: %s\n", binaryInfo.BuildScript)
}
if binaryInfo.BuildLog != "" {
fmt.Printf("Build Log: %s\n", binaryInfo.BuildLog)
}
if binaryInfo.Category != "" {
fmt.Printf("Category: %s\n", binaryInfo.Category)
}
if binaryInfo.ExtraBins != "" {
truncatePrintf(config.DisableTruncation, false, "Extra Bins: %s\n", binaryInfo.ExtraBins)
}
}
case "run":
if len(args) < 1 {
fmt.Println("Usage: dbin run <--transparent> [binary] <args>")
os.Exit(1)
}
var transparentMode bool
transparent := flag.Bool("transparent", false, "Run the binary from PATH if found")
flag.CommandLine.Parse(args)
if *transparent {
transparentMode = true
}
// Ensure binary name is provided
if len(flag.Args()) < 1 {
fmt.Println("Usage: dbin run <--transparent> [binary] <args>")
os.Exit(1)
}
fetchMetadata()
RunFromCache(config, flag.Arg(0), flag.Args()[1:], transparentMode, verbosityLevel, metadata)
case "tldr":
RunFromCache(config, "tlrc", flag.Args()[1:], true, verbosityLevel, metadata)
case "eget2":
RunFromCache(config, "eget2", flag.Args()[1:], true, verbosityLevel, metadata)
case "update":
var programsToUpdate []string
if len(os.Args) > 2 {
programsToUpdate = os.Args[2:]
}
fetchMetadata()
if err := update(config, programsToUpdate, verbosityLevel, metadata); err != nil {
fmt.Println("Update failed:", err)
}
default:
print(helpPage)
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)
os.Exit(1)
}
}