github.com/replit/upm@v0.0.0-20240423230255-9ce4fc3ea24c/internal/backends/java/msch.go (about) 1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2016 Fredy Wijaya 4 // Search() function lightly edited by Dan Stowell for repl.it, February 2020 5 // 6 // Permission is hereby granted, free of charge, to any person obtaining a copy 7 // of this software and associated documentation files (the "Software"), to deal 8 // in the Software without restriction, including without limitation the rights 9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 // copies of the Software, and to permit persons to whom the Software is 11 // furnished to do so, subject to the following conditions: 12 // 13 // The above copyright notice and this permission notice shall be included in all 14 // copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 // SOFTWARE. 23 24 package java 25 26 import ( 27 "encoding/json" 28 "fmt" 29 "io" 30 "net/url" 31 "strings" 32 33 "github.com/replit/upm/internal/api" 34 ) 35 36 const ( 37 mavenURL string = "https://search.maven.org/solrsearch/select?q=" 38 ) 39 40 type SearchDoc struct { 41 Group string `json:"g"` 42 Artifact string `json:"a"` 43 Version string `json:"latestVersion"` 44 PackageType string `json:"p"` 45 CurrentVersion string `json:"v"` 46 } 47 48 type SearchResult struct { 49 Response struct { 50 Docs []SearchDoc `json:"docs"` 51 } `json:"response"` 52 } 53 54 func mavenSearch(searchURL string) ([]SearchDoc, error) { 55 res, err := api.HttpClient.Get(searchURL) 56 if err != nil { 57 return []SearchDoc{}, err 58 } 59 defer res.Body.Close() 60 61 body, err := io.ReadAll(res.Body) 62 if err != nil { 63 fmt.Printf("Could not read response\n") 64 return []SearchDoc{}, err 65 } 66 67 var searchResult SearchResult 68 if err := json.Unmarshal(body, &searchResult); err != nil { 69 fmt.Printf("Failed to decode response %q\n", body) 70 return []SearchDoc{}, err 71 } 72 73 return searchResult.Response.Docs, nil 74 } 75 76 func Search(keyword string) ([]SearchDoc, error) { 77 searchURL := mavenURL + url.QueryEscape(keyword) 78 79 return mavenSearch(searchURL) 80 } 81 82 func Info(name string) (SearchDoc, error) { 83 parts := strings.Split(string(name), ":") 84 85 var searchURL string 86 if len(parts) >= 2 { 87 searchURL = fmt.Sprintf("%sg:%s+AND+a:%s&core=gav", mavenURL, url.QueryEscape(fmt.Sprintf("%q", parts[0])), url.QueryEscape(fmt.Sprintf("%q", parts[1]))) 88 } else { 89 searchURL = fmt.Sprintf("%sa:%s&core=gav", mavenURL, url.QueryEscape(fmt.Sprintf("%q", parts[0]))) 90 } 91 92 docs, err := mavenSearch(searchURL) 93 94 if err != nil { 95 return SearchDoc{}, err 96 } 97 98 var latest = SearchDoc{} 99 if len(docs) > 0 { 100 latest = docs[0] 101 } 102 103 return latest, nil 104 }