Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library - GetDependancy #5

Open
3 tasks
bawdeveloppement opened this issue Apr 18, 2023 · 0 comments
Open
3 tasks

Library - GetDependancy #5

bawdeveloppement opened this issue Apr 18, 2023 · 0 comments

Comments

@bawdeveloppement
Copy link

  • Verify Cache
  • Fetch Dependancy
  • Store to cache

`
package main

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

type Dependency struct {
ModulePath string json:"module_path"
Version string json:"version"
}

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run main.go ")
os.Exit(1)
}

repo := os.Args[1]

// Determine the module path based on the repository URL.
modulePath := strings.TrimSuffix(repo, ".git")
modulePath = strings.TrimPrefix(modulePath, "https://github.com/")
modulePath = strings.ReplaceAll(modulePath, "/", "%2F")

// Download the go.mod file.
goModUrl := fmt.Sprintf("https://raw.githubusercontent.com/%s/main/go.mod", repo)
resp, err := http.Get(goModUrl)
if err != nil {
    fmt.Println("Error downloading go.mod:", err)
    os.Exit(1)
}
defer resp.Body.Close()

// Read the contents of the go.mod file.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    fmt.Println("Error reading go.mod:", err)
    os.Exit(1)
}

// Parse the contents of the go.mod file.
dependencies := make([]Dependency, 0)
for _, line := range strings.Split(string(body), "\n") {
    if strings.HasPrefix(line, "require ") {
        parts := strings.Split(line[8:], " ")
        modulePath := parts[0]
        version := parts[1]
        dependencies = append(dependencies, Dependency{ModulePath: modulePath, Version: version})
    }
}

// Print the dependencies.
fmt.Println(dependencies)

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant