Skip to content

Commit

Permalink
Configure work
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoConstantine committed Jul 4, 2024
1 parent f4a408d commit 277f73c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/commands/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -159,6 +160,22 @@ func configureTool(item utils.ConfigureItem, ctx context.Context, force bool) er
if err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
// Convert URL if it's a GitHub URL
convertedURL, err := utils.ConvertToRawGitHubURL(item.ConfigURL)
if err != nil {
return fmt.Errorf("error converting URL: %v", err)
}
item.ConfigURL = convertedURL
// Validate the URL
parsedURL, err := url.Parse(item.ConfigURL)
fmt.Println(parsedURL)
if err != nil {
return fmt.Errorf("invalid configuration URL: %v", err)
}

if parsedURL.Scheme == "" {
return fmt.Errorf("URL scheme is missing. Please provide a complete URL including http:// or https://")
}

// Download the configuration file
resp, err := http.Get(item.ConfigURL)
Expand All @@ -167,6 +184,10 @@ func configureTool(item utils.ConfigureItem, ctx context.Context, force bool) er
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download configuration: HTTP status %d", resp.StatusCode)
}

// Create the configuration file
out, err := os.Create(installPath)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"mycli/pkg/iostreams"
"net/url"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -93,6 +94,31 @@ func GetOsInfo() map[string]string {
return map[string]string{"sysname": sysname, "release": release, "user": user.Username}
}

func ConvertToRawGitHubURL(inputURL string) (string, error) {
parsedURL, err := url.Parse(inputURL)
if err != nil {
return "", fmt.Errorf("invalid URL: %v", err)
}

if parsedURL.Host != "github.com" {
return inputURL, nil // Not a GitHub URL, return as-is
}

pathParts := strings.Split(parsedURL.Path, "/")
if len(pathParts) < 5 {
return "", fmt.Errorf("invalid GitHub URL format")
}

// Reconstruct the URL for raw content
rawURL := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s",
pathParts[1], // username
pathParts[2], // repository
pathParts[4], // branch (usually "master" or "main")
strings.Join(pathParts[5:], "/")) // file path

return rawURL, nil
}

func getRandomASCIILogo() string {
logos := []string{
`
Expand Down

0 comments on commit 277f73c

Please sign in to comment.