Skip to content

Commit

Permalink
Merge pull request #5 from xplshn/dev
Browse files Browse the repository at this point in the history
1.2! FIXXXES! +"update"
  • Loading branch information
xplshn committed Feb 20, 2024
2 parents e37f842 + b473cdc commit e83d145
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 115 deletions.
Binary file modified bigdl
Binary file not shown.
87 changes: 87 additions & 0 deletions fast_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// fast_info.go // this file implements the functionality of 'f_info' //>
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

// fast_BinaryInfo represents the structure of binary information.
type fast_BinaryInfo struct {
Description string `json:"description"`
Name string `json:"name"`
Architecture string `json:"architecture"`
Version string `json:"version"`
Updated string `json:"updated"`
Size string `json:"size"`
SHA string `json:"sha"`
Source string `json:"source"`
}

// fast_showBinaryInfo fetches a binary's information from RMetadataURL and prints it.
func fast_showBinaryInfo(binaryName string, validatedArch string) {
response, err := http.Get(RMetadataURL)
if err != nil {
fmt.Printf("Error fetching metadata: %v\n", err)
return
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("Error reading metadata: %v\n", err)
return
}

var metadata map[string][]fast_BinaryInfo
if err := json.Unmarshal(body, &metadata); err != nil {
fmt.Printf("Error decoding metadata: %v\n", err)
return
}

binaries, exists := metadata["packages"]
if !exists {
fmt.Println("Invalid metadata format. No fields found. Check fast_info.go:44:31")
return
}

var found bool
var nonMatchingArchCount int

for _, bin := range binaries {
if bin.Name == binaryName && bin.Architecture == validatedArch {
// Print the binary information
fmt.Printf("Name: %s\n", bin.Name)
fmt.Printf("Description: %s\n", bin.Description)
if bin.Version != "" {
fmt.Printf("Version: %s\n", bin.Version)
}
if bin.Updated != "" {
fmt.Printf("Updated: %s\n", bin.Updated)
}
if bin.Size != "" {
fmt.Printf("Size: %s\n", bin.Size)
}
if bin.SHA != "" {
fmt.Printf("SHA: %s\n", bin.SHA)
}
fmt.Printf("Source: %s\n", bin.Source)
found = true
break
} else if bin.Name == binaryName {
// Binary found but not for the system's architecture
nonMatchingArchCount++
}
}

// If binary not found for the system's architecture
if !found {
if nonMatchingArchCount > 0 {
fmt.Printf("Binary '%s' found, but not for the system's architecture.\n", binaryName)
} else {
fmt.Printf("Info for the requested binary ('%s') not found in the metadata.json file. Please contribute to %s.\n", binaryName, RMetadataURL)
}
}
}
3 changes: 1 addition & 2 deletions findURL.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// findURL.go // This file implements the findURL function //

// findURL.go // This file implements the findURL function //>
package main

import (
Expand Down
33 changes: 22 additions & 11 deletions fsearch.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// fsearch.go // this file implements the search option

// fsearch.go // this file implements the search option //>
package main

import (
Expand Down Expand Up @@ -30,17 +29,29 @@ func fSearch(searchTerm string, desiredArch string) {
return
}

var metadata map[string][]BinaryInfo
if err := json.Unmarshal(body, &metadata); err != nil {
// Define a struct to match the JSON structure from RMetadataURL
type Binary struct {
Name string `json:"name"`
Description string `json:"description"`
// Include other fields if needed
}

type RMetadata struct {
Binaries []Binary `json:"packages"`
}

// Unmarshal the description as an RMetadata object
var rMetadata RMetadata
if err := json.Unmarshal(body, &rMetadata); err != nil {
fmt.Println("Failed to decode metadata.")
return
}

// Filter binaries based on the search term and architecture
searchResultsSet := make(map[string]struct{}) // Use a set to keep track of unique entries
for _, bin := range metadata["binaries"] {
if strings.Contains(strings.ToLower(bin.Name+bin.Description), strings.ToLower(searchTerm)) {
entry := fmt.Sprintf("%s - %s", bin.Name, bin.Description)
for _, pkg := range rMetadata.Binaries {
if strings.Contains(strings.ToLower(pkg.Name+pkg.Description), strings.ToLower(searchTerm)) {
entry := fmt.Sprintf("%s - %s", pkg.Name, pkg.Description)
searchResultsSet[entry] = struct{}{} // Add the entry to the set
}
}
Expand All @@ -50,7 +61,7 @@ func fSearch(searchTerm string, desiredArch string) {
fmt.Printf("No matching binaries found for '%s'.\n", searchTerm)
return
} else if len(searchResultsSet) > 90 {
fmt.Printf("Too many matching binaries found for '%s'.\n", searchTerm)
fmt.Printf("Too many matching binaries (+90. [Limit defined in fsearch.go:63:36,37]) found for '%s'.\n", searchTerm)
return
}

Expand All @@ -69,11 +80,11 @@ func fSearch(searchTerm string, desiredArch string) {
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
return 80 // Default to 80 columns if unable to get terminal width
return 80 // Default to 80 columns if unable to get terminal width
}
width, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
return 80 // Default to 80 columns if unable to convert width to integer
return 80 // Default to 80 columns if unable to convert width to integer
}
return width
}
Expand Down Expand Up @@ -103,7 +114,7 @@ func fSearch(searchTerm string, desiredArch string) {
}

// Calculate available space for description
availableSpace := getTerminalWidth() - len(prefix) - len(name) - 4 // 4 accounts for space around ' - '
availableSpace := getTerminalWidth() - len(prefix) - len(name) - 4 // 4 accounts for space around ' - '

// Truncate the description if it exceeds the available space
if len(description) > availableSpace {
Expand Down
2 changes: 1 addition & 1 deletion helperFunctions.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// helperFunctions.go // This file contains commonly used functions //
// helperFunctions.go // This file contains commonly used functions //>

package main

Expand Down
69 changes: 26 additions & 43 deletions info.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// info.go // this file implements the functionality of 'info'

// info.go // this file implements the functionality of 'info' //>
package main

import (
Expand All @@ -12,96 +11,80 @@ import (
// BinaryInfo represents the structure of binary information.
type BinaryInfo struct {
Name string `json:"Name"`
Description string `json:"Description"`
Repo string `json:"Repo"`
ModTime string `json:"ModTime"`
Size string `json:"Size"`
SHA256 string `json:"SHA256"`
B3SUM string `json:"B3SUM"`
Description string `json:"Description"`
Source string `json:"Source"`
}

// BinaryMetadata represents the structure of the metadata for a binary.
type BinaryMetadata struct {
Binaries []BinaryInfo `json:"binaries"`
}

// showBinaryInfo fetches binary information from MetadataURLs and prints it.
func showBinaryInfo(binaryName string) {
// getBinaryInfo fetches binary information from MetadataURLs and returns it as a BinaryInfo struct.
func getBinaryInfo(binaryName string) (*BinaryInfo, error) {
for i, metadataURL := range MetadataURLs {
if i >= 2 { // TODO: Correctly unmarshal Github's REST API's "contents" endpoint.
if i >= 2 { // TODO: Correctly unmarshal Github's REST API's "contents" endpoint. In order not to do this ugly thing.
break
}

response, err := http.Get(metadataURL)
if err != nil {
fmt.Printf("Error fetching metadata from %s: %v\n", metadataURL, err)
continue
return nil, fmt.Errorf("error fetching metadata from %s: %v", metadataURL, err)
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("Error reading metadata from %s: %v\n", metadataURL, err)
continue
return nil, fmt.Errorf("error reading metadata from %s: %v", metadataURL, err)
}

var metadata []BinaryInfo
if err := json.Unmarshal(body, &metadata); err != nil {
fmt.Printf("Error decoding metadata from %s: %v\n", metadataURL, err)
continue
return nil, fmt.Errorf("error decoding metadata from %s: %v", metadataURL, err)
}

for _, bin := range metadata {
if bin.Name == binaryName {
fmt.Printf("Binary Name: %s\n", bin.Name)
if bin.Repo != "" {
fmt.Printf("Repo: %s\n", bin.Repo)
}
if bin.Size != "" {
fmt.Printf("Size: %s\n", bin.Size)
}
if bin.SHA256 != "" {
fmt.Printf("SHA256: %s\n", bin.SHA256)
}
if bin.B3SUM != "" {
fmt.Printf("B3SUM: %s\n", bin.B3SUM)
}

// Fetch the description from RMetadataURL
response, err = http.Get(RMetadataURL)
if err != nil {
fmt.Printf("Error fetching description from %s: %v\n", RMetadataURL, err)
return
return nil, fmt.Errorf("error fetching description from %s: %v", RMetadataURL, err)
}
defer response.Body.Close()

body, err = ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("Error reading description from %s: %v\n", RMetadataURL, err)
return
return nil, fmt.Errorf("error reading description from %s: %v", RMetadataURL, err)
}

// Unmarshal the description as a BinaryMetadata object
var binaryMetadata BinaryMetadata
if err := json.Unmarshal(body, &binaryMetadata); err != nil {
fmt.Printf("Error decoding description from %s: %v\n", RMetadataURL, err)
return
// Unmarshal the description as a map with a slice of BinaryInfo objects
var descriptionMetadata map[string][]BinaryInfo
if err := json.Unmarshal(body, &descriptionMetadata); err != nil {
return nil, fmt.Errorf("error decoding description from %s: %v", RMetadataURL, err)
}

// Find the binary in the metadata and set the description
for _, binInfo := range binaryMetadata.Binaries {
// TODO: Once Github repos are supported, optionally detect METADATA.json files in the repos.
binaries, exists := descriptionMetadata["packages"]
if !exists {
return nil, fmt.Errorf("invalid description metadata format. No 'packages' field found.")
}

// Find the binary with the matching name and set its description
for _, binInfo := range binaries {
if binInfo.Name == binaryName {
bin.Description = binInfo.Description
break
}
}

if bin.Description != "" {
fmt.Printf("Description: %s\n", bin.Description)
}
return
return &bin, nil
}
}
}

fmt.Printf("Info for the requested binary ('%s') not found in the metadata.json files. Please contribute to the repositories.\n", binaryName)
return nil, fmt.Errorf("info for the requested binary ('%s') not found in the metadata.json files", binaryName)
}
25 changes: 12 additions & 13 deletions install.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// install.go // This file implements the install functionality //

package main

import (
Expand All @@ -8,19 +7,22 @@ import (
"path/filepath"
)

func installCommand(binaryName string, args []string) {
func installCommand(binaryName string, args []string) error {
installDir := os.Getenv("INSTALL_DIR")
if len(args) > 0 && args[0] != "" {
installDir = args[0]
}

if installDir == "" {
installDir = filepath.Join(os.Getenv("HOME"), ".local", "bin")
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("Error: Failed to get user's Home directory: %w", err)
}
installDir = filepath.Join(homeDir, ".local", "bin")
}

if err := os.MkdirAll(installDir, os.ModePerm); err != nil {
fmt.Printf("Error creating installation directory: %v\n", err)
os.Exit(1)
return fmt.Errorf("Error: Could not create installation directory: %v", err)
}

installPath := filepath.Join(installDir, binaryName)
Expand All @@ -32,23 +34,20 @@ func installCommand(binaryName string, args []string) {
fmt.Printf("Using cached file: %s\n", cachedFile)
// Copy the cached file to the install path
if err := copyFile(cachedFile, installPath); err != nil {
fmt.Printf("Error copying cached file: %v\n", err)
os.Exit(1)
return fmt.Errorf("Error: Could not copy cached file: %v", err)
}
} else {
// If the cached file does not exist, download the binary
url, err := findURL(binaryName)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
return fmt.Errorf("Error: %v", err)
}
if err := fetchBinaryFromURL(url, installPath); err != nil {
fmt.Printf("Error installing binary: %v\n", err)
os.Exit(1)
return fmt.Errorf("Error: Could not install binary: %v", err)
}
}

// Use the escape sequence for newline directly
fmt.Printf("Installation complete: %s\n", installPath)
fmt.Println() // Adding a newline for proper PS1 behavior
fmt.Printf("Installation complete: %s \n", installPath)
return nil
}
Loading

0 comments on commit e83d145

Please sign in to comment.