generated from skanehira/go-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from kyoshidajp/refactor_registry
Refactor registry
- Loading branch information
Showing
11 changed files
with
144 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// https://docs.npmjs.com/cli/v8/using-npm/registry | ||
const NODEJS_REGISTRY_API = "https://registry.npmjs.org/%s" | ||
|
||
type NodejsRegistryResponse struct { | ||
Repository struct { | ||
Url string `json:"url"` | ||
} | ||
} | ||
|
||
type Nodejs struct { | ||
} | ||
|
||
func (n *Nodejs) fetchURLFromRegistry(name string) (string, error) { | ||
url := fmt.Sprintf(NODEJS_REGISTRY_API, name) | ||
req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
client := new(http.Client) | ||
resp, _ := client.Do(req) | ||
body, _ := io.ReadAll(resp.Body) | ||
|
||
var NodejsRegistryResponse NodejsRegistryResponse | ||
err := json.Unmarshal(body, &NodejsRegistryResponse) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
return NodejsRegistryResponse.Repository.Url, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// https://warehouse.pypa.io/api-reference/json.html | ||
const PYPI_REGISTRY_API = "https://pypi.org/pypi/%s/json" | ||
|
||
type PypiRegistryResponse struct { | ||
Info struct { | ||
ProjectUrls struct { | ||
SourceCode string `json:"Source Code"` | ||
Source string `json:"Source"` | ||
} `json:"project_urls"` | ||
} `json:"info"` | ||
} | ||
|
||
type Pypi struct { | ||
} | ||
|
||
func (p *Pypi) fetchURLFromRepository(name string) (string, error) { | ||
url := fmt.Sprintf(PYPI_REGISTRY_API, name) | ||
req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
client := new(http.Client) | ||
resp, _ := client.Do(req) | ||
body, _ := io.ReadAll(resp.Body) | ||
|
||
var PypiRegistryResponse PypiRegistryResponse | ||
err := json.Unmarshal(body, &PypiRegistryResponse) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
if PypiRegistryResponse.Info.ProjectUrls.SourceCode != "" { | ||
return PypiRegistryResponse.Info.ProjectUrls.SourceCode, nil | ||
} | ||
|
||
return PypiRegistryResponse.Info.ProjectUrls.Source, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// https://guides.rubygems.org/rubygems-org-api/ | ||
const RUBY_GEMS_REGISTRY_API = "https://rubygems.org/api/v1/gems/%s.json" | ||
|
||
type RubyGemsRegistryResponse struct { | ||
Name string `json:"name"` | ||
SourceCodeUri string `json:"source_code_uri"` | ||
HomepageUri string `json:"homepage_uri"` | ||
} | ||
|
||
type RubyGems struct { | ||
} | ||
|
||
func (g *RubyGems) fetchURLFromRegistry(name string) (string, error) { | ||
url := fmt.Sprintf(RUBY_GEMS_REGISTRY_API, name) | ||
req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
client := new(http.Client) | ||
resp, _ := client.Do(req) | ||
body, _ := io.ReadAll(resp.Body) | ||
|
||
var Gem RubyGemsRegistryResponse | ||
err := json.Unmarshal(body, &Gem) | ||
if err != nil { | ||
return "", errors.New("error: Unknown response") | ||
} | ||
|
||
if Gem.SourceCodeUri != "" { | ||
return Gem.SourceCodeUri, nil | ||
} else if Gem.HomepageUri != "" { | ||
return Gem.HomepageUri, nil | ||
} | ||
|
||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.