Skip to content

Commit

Permalink
Merge SID. 1.6.8 is out! (#74)
Browse files Browse the repository at this point in the history
* Prepare for 1.6.8!

* Refine error messages, replace unnecessary function, janky bits

* Add Extras to the Info struct

* Tweaks! Now FAST by default!

* Some more last minute changes

* This is 1.6.8!
  • Loading branch information
xplshn committed Jun 13, 2024
1 parent f0adc2e commit 797f578
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 180 deletions.
22 changes: 9 additions & 13 deletions fsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func fSearch(searchTerm string, limit int) {
if _, excludedName := excludedFileNames[base]; excludedName {
continue // Skip this binary if its name is excluded
}
entry := fmt.Sprintf("%s - %s", binary.Name, binary.Description)
searchResultsSet[entry] = struct{}{}
if binary.Description != "" {
entry := fmt.Sprintf("%s - %s", binary.Name, binary.Description)
searchResultsSet[entry] = struct{}{}
}
}
}

Expand Down Expand Up @@ -66,21 +68,15 @@ func fSearch(searchTerm string, limit int) {
description := parts[1]

installPath := filepath.Join(InstallDir, name)

cachedLocation, _ := ReturnCachedFile(name)

var prefix string
prefix := "[-]"
if fileExists(installPath) {
prefix = "[i]"
} else {
binaryPath, _ := exec.LookPath(name) // is it okay to ignore the err channel of LookPath?
if binaryPath != "" {
prefix = "[\033[4mi\033[0m]" // Print [i], I is underlined.
} else if cachedLocation != "" && isExecutable(cachedLocation) {
prefix = "[c]"
} else {
prefix = "[-]"
}
} else if path, err := exec.LookPath(name); err == nil && path != "" {
prefix = "[\033[4mi\033[0m]" // Print [i],'i' is underlined
} else if cachedLocation != "" && isExecutable(cachedLocation) {
prefix = "[c]"
}

truncatePrintf("%s %s - %s ", prefix, name, description)
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ module github.com/xplshn/bigdl

go 1.22.0

require github.com/schollz/progressbar/v3 v3.14.3
require (
github.com/goccy/go-json v0.10.3
github.com/schollz/progressbar/v3 v3.14.4
)

require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
)
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
Expand All @@ -9,13 +11,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/schollz/progressbar/v3 v3.14.3 h1:oOuWW19ka12wxYU1XblR4n16wF/2Y1dBLMarMo6p4xU=
github.com/schollz/progressbar/v3 v3.14.3/go.mod h1:aT3UQ7yGm+2ZjeXPqsjTenwL3ddUiuZ0kfQ/2tHlyNI=
github.com/schollz/progressbar/v3 v3.14.4 h1:W9ZrDSJk7eqmQhd3uxFNNcTr0QL+xuGNI9dEMrw0r74=
github.com/schollz/progressbar/v3 v3.14.4/go.mod h1:aT3UQ7yGm+2ZjeXPqsjTenwL3ddUiuZ0kfQ/2tHlyNI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
31 changes: 9 additions & 22 deletions helperFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -16,13 +15,11 @@ import (
"strings"
"syscall"

"github.com/goccy/go-json"
"github.com/schollz/progressbar/v3"
)

// TODO: Add *PROPER* error handling in the truncate functions. Ensure escape sequences are correctly handled?

// signalHandler sets up a channel to listen for interrupt signals and returns a function
// that can be called to check if an interrupt has been received.
// signalHandler sets up a channel to listen for interrupt signals and returns a function that can be called to check if an interrupt has been received.
func signalHandler(ctx context.Context, cancel context.CancelFunc) (func() bool, error) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
Expand Down Expand Up @@ -256,9 +253,10 @@ func spawnProgressBar(contentLength int64) *progressbar.ProgressBar {
}),
)
}
return progressbar.NewOptions(
-1,
return progressbar.NewOptions(-1,
progressbar.OptionSetWriter(io.Discard),
progressbar.OptionSetVisibility(false),
progressbar.OptionShowBytes(false),
)
}

Expand Down Expand Up @@ -343,7 +341,7 @@ func truncateSprintf(format string, a ...interface{}) string {
// Determine the truncation length & truncate the formatted string if it exceeds the available space
availableSpace := getTerminalWidth() - len(indicator)
if len(formatted) > availableSpace {
formatted = fmt.Sprintf("%s", formatted[:availableSpace])
formatted = formatted[:availableSpace]
for strings.HasSuffix(formatted, ",") || strings.HasSuffix(formatted, ".") || strings.HasSuffix(formatted, " ") {
formatted = formatted[:len(formatted)-1]
}
Expand All @@ -353,16 +351,16 @@ func truncateSprintf(format string, a ...interface{}) string {
return formatted
}

// NOTE: \n will always get cut off when using a truncate function, this may also happen to other formatting options
// truncatePrintf is a drop-in replacement for fmt.Printf that truncates the input string if it exceeds a certain length.
func truncatePrintf(format string, a ...interface{}) (n int, err error) {
if DisableTruncation {
return fmt.Print(fmt.Sprintf(format, a...))
return fmt.Printf(format, a...)
}
return fmt.Print(truncateSprintf(format, a...))
} // NOTE: Both truncate functions will remove the escape sequences of truncated lines, and sometimes break them in half because of the truncation. Avoid using escape sequences with truncate functions, as it is UNSAFE.
}

// validateProgramsFrom validates programs against the files in the specified directory against the remote binaries.
// It returns the validated programs based on the last element of the received list of programs.
func validateProgramsFrom(InstallDir string, programsToValidate []string) ([]string, error) {
// Fetch the list of binaries from the remote source once
remotePrograms, err := listBinaries()
Expand Down Expand Up @@ -405,16 +403,5 @@ func validateProgramsFrom(InstallDir string, programsToValidate []string) ([]str

// Handle the list of programs received based on the last element
// If programsToValidate is not nil, handle based on the last element
if len(programsToValidate) != 0 {
lastElement := programsToValidate[len(programsToValidate)-1]
switch lastElement {
case "_2_":
return invalidPrograms, nil
case "_3_":
return append(validPrograms, invalidPrograms...), nil
default:
return validPrograms, nil
}
}
return validPrograms, nil
}
7 changes: 5 additions & 2 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type BinaryInfo struct {
Version string `json:"repo_version"`
Updated string `json:"repo_updated"`
Size string `json:"size"`
Extras string `json:"extra_bins"`
SHA256 string `json:"sha256"`
Source string `json:"download_url"`
}
Expand All @@ -23,21 +24,23 @@ func findBinaryInfo(metadata []map[string]interface{}, binaryName string) (Binar
if name, ok := binMap["name"].(string); ok && name == binaryName {
description, _ := binMap["description"].(string)
repo, _ := binMap["repo_url"].(string)
build_date, _ := binMap["build_date"].(string)
buildDate, _ := binMap["build_date"].(string)
version, _ := binMap["repo_version"].(string)
updated, _ := binMap["repo_updated"].(string)
size, _ := binMap["size"].(string)
extras, _ := binMap["extra_bins"].(string)
sha256, _ := binMap["sha256"].(string)
source, _ := binMap["download_url"].(string)

return BinaryInfo{
Name: name,
Description: description,
Repo: repo,
ModTime: build_date,
ModTime: buildDate,
Version: version,
Updated: updated,
Size: size,
Extras: extras,
SHA256: sha256,
Source: source,
}, true
Expand Down
8 changes: 6 additions & 2 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ func installCommand(silent bool, binaryNames string) error {
}

if err := fetchBinaryFromURL(url, installPath); err != nil {
return fmt.Errorf("error: Could not install binary: %v", err)
return fmt.Errorf("%v", err)
}

if !silent {
fmt.Printf("Installed: %s\n", installPath)
if InstallMessage != "disabled" {
fmt.Print(InstallMessage)
} else {
fmt.Printf("Succesfully created %s\n", installPath)
}
}
}
return nil
Expand Down
93 changes: 45 additions & 48 deletions listBinaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,72 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
"sort"
"strings"
)

// listBinariesCommand fetches and lists binary names from the given URL.
// listBinaries fetches and lists binary names from the given URL.
func listBinaries() ([]string, error) {
var allBinaries []string
var allBinaries []struct {
Name string `json:"Name"`
NameAlt string `json:"name"`
SHA256 string `json:"sha256"`
SHA string `json:"sha"`
}

// Fetch binaries from each metadata URL
for _, url := range MetadataURLs {
// Fetch metadata from the given URL
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("error fetching metadata from %s: %v", url, err)
var metadata []struct {
Name string `json:"Name"`
NameAlt string `json:"name"`
SHA256 string `json:"sha256"`
SHA string `json:"sha"`
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch metadata from %s. HTTP status code: %d", url, resp.StatusCode)
// Use fetchJSON to fetch and unmarshal the JSON data
if err := fetchJSON(url, &metadata); err != nil {
return nil, fmt.Errorf("failed to fetch metadata from %s: %v", url, err)
}

// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
// Extract binary names and SHA values
allBinaries = append(allBinaries, metadata...)
}

// Unmarshal JSON
var metadata []struct {
Name string `json:"Name"` // Consider both "name" and "Name" fields
NameAlt string `json:"name"`
}
if err := json.Unmarshal(body, &metadata); err != nil {
return nil, fmt.Errorf("failed to unmarshal metadata JSON from %s: %v", url, err)
// Filter out excluded file types and file names, and build a map of SHA256 and SHA values to binary names
filteredBinaries := make(map[string]string)
shaMap := make(map[string]bool)
for _, item := range allBinaries {
binary := item.Name
if binary == "" {
binary = item.NameAlt
}

// Extract binary names
for _, item := range metadata {
if item.Name != "" {
allBinaries = append(allBinaries, item.Name)
}
if item.NameAlt != "" {
allBinaries = append(allBinaries, item.NameAlt)
if binary != "" {
ext := strings.ToLower(filepath.Ext(binary))
base := filepath.Base(binary)
if _, excluded := excludedFileTypes[ext]; !excluded {
if _, excludedName := excludedFileNames[base]; !excludedName {
if _, exists := shaMap[item.SHA256]; !exists {
shaMap[item.SHA256] = true
filteredBinaries[binary] = item.SHA256
}
if _, exists := shaMap[item.SHA]; !exists {
shaMap[item.SHA] = true
filteredBinaries[binary] = item.SHA
}
}
}
}
}

// Filter out excluded file types and file names
var filteredBinaries []string
for _, binary := range allBinaries {
ext := strings.ToLower(filepath.Ext(binary))
base := filepath.Base(binary)
if _, excluded := excludedFileTypes[ext]; !excluded {
if _, excludedName := excludedFileNames[base]; !excludedName {
filteredBinaries = append(filteredBinaries, binary)
}
}
// Define and fill uniqueBinaries
var uniqueBinaries []string
for binary := range filteredBinaries {
uniqueBinaries = append(uniqueBinaries, binary)
}

// Remove duplicates
uniqueBinaries := removeDuplicates(filteredBinaries)

// Sort binaries alphabetically
sort.Strings(uniqueBinaries)
// Remove duplicates (entries with same name)
uniqueBinaries = removeDuplicates(uniqueBinaries)

// Return the list of binaries
return uniqueBinaries, nil
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var (
// InstallDir holds the directory that shall be used for installing, removing, updating, listing with `info`. It takes the value of $INSTALL_DIR if it is set in the user's env, otherwise it is set to have a default value
InstallDir = os.Getenv("INSTALL_DIR")
// TEMPDIR will be used as the dir to download files to before moving them to a final destination AND as the place that will hold cached binaries downloaded by `run`
TEMPDIR = os.Getenv("BIGDL_CACHEDIR") // Will default to "/tmp/bigdl_cached" if $TMPDIR is not set
TEMPDIR = os.Getenv("BIGDL_CACHEDIR")
// InstallMessage will be printed when installCommand() succeeds
InstallMessage = "disabled"
// InstallUseCache determines if cached files should be used when requesting an install
InstallUseCache = true
// UseProgressBar determines if the progressbar is shown or not
Expand All @@ -32,7 +34,7 @@ var (
)

const (
VERSION = "1.6.7" // VERSION to be displayed
VERSION = "1.6.8" // VERSION to be displayed
usagePage = " [-v|-h] [list|install|remove|update|run|info|search|tldr] <-args->" // usagePage to be shown
// Truncation indicator
indicator = "...>"
Expand Down Expand Up @@ -80,7 +82,6 @@ func init() {
}
InstallDir = filepath.Join(homeDir, ".local", "bin")
}

if TEMPDIR == "" {
cacheDir, err := os.UserCacheDir()
if err != nil {
Expand Down Expand Up @@ -181,7 +182,6 @@ func main() {
errorOut(" bigdl:%s\n", usagePage)
}

// From now on, things happen.
if err := os.MkdirAll(InstallDir, os.ModePerm); err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to get user's Home directory. %v\n", err)
os.Exit(1)
Expand All @@ -198,7 +198,8 @@ func main() {
case "list":
if len(os.Args) == 3 {
if os.Args[2] == "--described" || os.Args[2] == "-d" {
fSearch("", 99999) // Call fSearch with an empty query and a large limit to list all described binaries
// Call fSearch with an empty query and a large limit to list all described binaries
fSearch("", 99999)
} else {
errorOut("bigdl: Unknown command.\n")
}
Expand Down Expand Up @@ -279,7 +280,7 @@ func main() {
if binaryInfo.Size != "" {
fmt.Printf("Size: %s\n", binaryInfo.Size)
}
if binaryInfo.Source != "" {
if binaryInfo.Source != "" { // if binaryInfo.Extras != "" {
fmt.Printf("Source: %s\n", binaryInfo.Source)
}
if binaryInfo.SHA256 != "" {
Expand Down
Loading

0 comments on commit 797f578

Please sign in to comment.