Skip to content

Commit

Permalink
primitive support for externally hosted releases
Browse files Browse the repository at this point in the history
  • Loading branch information
quiquelhappy committed Aug 26, 2021
1 parent dccba08 commit 14d590e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func GetResources(name string, author string, service int) ([]Resource, error) {
if service>-1 {
data["service"]= string(rune(service))
}
request, err := request("resource/query/", data)
request, err := Request("resource/query/", data)
resourceList := make([]Resource,0)
err = json.NewDecoder(request).Decode(&resourceList)
return resourceList, err
Expand All @@ -45,15 +45,15 @@ func getRelease(resource Resource, version string) (Release, error) {
if version!="" {
data["version"] = version
}
request, err := request("resource/release/get/", data)
request, err := Request("resource/release/get/", data)
release := Release{}
err = json.NewDecoder(request).Decode(&release)
return release, err
}

func GetReleases(resource Resource) ([]Release, error) {
var err error = nil
request, err := request("resource/release/list/", map[string]string{
request, err := Request("resource/release/list/", map[string]string{
"resource":resource.Id,
})

Expand All @@ -79,7 +79,7 @@ func DownloadResource(resource Resource, version string, projectConfig config.Pr
if release.HasSuggestions!=nil {
if release.HasSuggestions == true {
// gets suggestion list release
request, err := request("resource/release/suggestion/list/", map[string]string{
request, err := Request("resource/release/suggestion/list/", map[string]string{
"release": releaseId,
})
suggestionList := make([]UrlSuggestion,0)
Expand All @@ -104,21 +104,34 @@ func DownloadResource(resource Resource, version string, projectConfig config.Pr
} else if strings.HasPrefix(resultURL,"skip") {
return DownloadableRelease{}, errors.New("skipped release")
} else {
downloadableRelease.Url=strings.Split(resultURL,"| ")[1]
downloadableRelease.Release=release
downloadableRelease.Resource=resource
downloadableRelease.Release.FileExtension=".jar"
downloadableRelease.Release.FileName="primitiveExternalSupport.jar"
resultSuggestionId := strings.TrimSpace(strings.Split(resultURL,"|")[0])
var selectedSuggestion = UrlSuggestion{}
var found = false
for _, suggestion := range suggestionList {
if suggestion.Id==resultSuggestionId {
selectedSuggestion=suggestion
found=true
break
}
}
if !found {
return DownloadableRelease{}, errors.New("selection mismatch")
}
// notify usage
err := UseSuggestion(selectedSuggestion)
if err != nil {
return DownloadableRelease{}, errors.New("couldn't register usage")
}

downloadableRelease = DownloadableReleaseFromSuggestion(selectedSuggestion,release,resource)
}
} else {
if err != nil {
return DownloadableRelease{}, errors.New("this release is hosted externally and no valid download URLs have been suggested, please, suggest a download URL for this release here: https://grifpkg.com/suggest/"+resource.Id+"/")
}
return DownloadableRelease{}, errors.New("this release is hosted externally and no valid download URLs have been suggested, please, suggest a download URL for this release here: https://grifpkg.com/suggest/"+resource.Id+"/")
}
} else {

// gets downloadable release
request, err := request("resource/release/download/", map[string]string{
request, err := Request("resource/release/download/", map[string]string{
"release": releaseId,
})
if err != nil {
Expand Down Expand Up @@ -196,7 +209,7 @@ func UpdateReleases(project config.Project) (updated int, skipped int, upToDate

func downloadFile(downloadableRelease DownloadableRelease, projectConfig config.Project) (string, string, string, error){
if downloadableRelease.Url == "" {
return "", "", "", errors.New("this release is hosted externally. support for externally hosted releases is coming this week")
return "", "", "", errors.New("this release is hosted externally. support for externally hosted releases is here but it has to make it to all commands, you can run 'grif install <name>' instead of the standalone command to choose an URL")
}

resp, err := http.Get(downloadableRelease.Url)
Expand All @@ -218,7 +231,7 @@ func downloadFile(downloadableRelease DownloadableRelease, projectConfig config.
return finalPath, separator, path+separator, err
}

func request(path string, data map[string]string) (io.Reader,error) {
func Request(path string, data map[string]string) (io.Reader,error) {
var result io.Reader = nil
var err error = nil
client := &http.Client{}
Expand Down
14 changes: 14 additions & 0 deletions src/client/downloadableRelease.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package client

import "strings"

type DownloadableRelease struct {
Url string `json:"url"`
Release Release `json:"release"`
Resource Resource `json:"resource"`
}

func DownloadableReleaseFromSuggestion(suggestion UrlSuggestion, release Release, resource Resource) DownloadableRelease {
return DownloadableRelease{suggestion.Url,Release{release.Id,release.Service,release.ReleaseId,release.Version,release.Creation,suggestion.FileName,extensionFromFileName(suggestion.FileName),0,nil,nil,nil,nil,nil,nil,nil,true},resource}
}

func extensionFromFileName(fileName string) interface{} {
parts := strings.Split(fileName,".")
if len(parts) > 1 {
return "." + parts[len(parts)-1]
}
return nil
}
7 changes: 7 additions & 0 deletions src/client/urlSuggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ type UrlSuggestion struct {
Creation int `json:"creation"`
Uses int `json:"uses"`
}

func UseSuggestion(suggestion UrlSuggestion) error{
_, err := Request("resource/release/suggestion/use/", map[string]string{
"suggestion": suggestion.Id,
})
return err
}

0 comments on commit 14d590e

Please sign in to comment.