diff --git a/api/api_test.go b/api/api_test.go index 83572d9..ab16361 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -6,7 +6,7 @@ import ( func Test_GetProject_Success(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) project, err := c.GetProject("fabric-api") if err != nil { t.Fatal(err) @@ -21,7 +21,7 @@ func Test_GetProject_Success(t *testing.T) { func Test_GetProject_404(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) _, err := c.GetProject("x") if err.Error() != "http status 404" { t.Fatal("wrong status!") @@ -30,7 +30,7 @@ func Test_GetProject_404(t *testing.T) { func TestClient_GetProjects_Count(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) projects, err := c.GetProjects([]string{"P7dR8mSH", "XxWD5pD3", "x"}) if err != nil { t.Fatal(err) @@ -42,7 +42,7 @@ func TestClient_GetProjects_Count(t *testing.T) { func TestClient_GetProjects_Slug(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) projects, err := c.GetProjects([]string{"P7dR8mSH"}) if err != nil { t.Fatal(err) @@ -54,7 +54,7 @@ func TestClient_GetProjects_Slug(t *testing.T) { func TestClient_CheckProjectValidity_Slug(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) response, err := c.CheckProjectValidity("fabric-api") if err != nil { t.Fatal(err) @@ -66,7 +66,7 @@ func TestClient_CheckProjectValidity_Slug(t *testing.T) { func TestClient_CheckProjectValidity_Id(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) response, err := c.CheckProjectValidity("P7dR8mSH") if err != nil { t.Fatal(err) @@ -78,7 +78,7 @@ func TestClient_CheckProjectValidity_Id(t *testing.T) { func TestClient_GetDependencies(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) dependencies, err := c.GetDependencies("rinthereout") if err != nil { t.Fatal(err) @@ -90,7 +90,7 @@ func TestClient_GetDependencies(t *testing.T) { func TestClient_GetProjectVersions_Count(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) versions, err := c.GetProjectVersions("fabric-api", &GetProjectVersionsParams{}) if err != nil { t.Fatal(err) @@ -102,7 +102,7 @@ func TestClient_GetProjectVersions_Count(t *testing.T) { func TestClient_GetProjectVersions_Filter_Results(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) versions, err := c.GetProjectVersions("fabric-api", &GetProjectVersionsParams{ GameVersions: []string{"1.16.5"}, }) @@ -116,7 +116,7 @@ func TestClient_GetProjectVersions_Filter_Results(t *testing.T) { func TestClient_GetProjectVersions_Filter_NoResults(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) versions, err := c.GetProjectVersions("fabric-api", &GetProjectVersionsParams{ Loaders: []string{"forge"}, }) @@ -130,7 +130,7 @@ func TestClient_GetProjectVersions_Filter_NoResults(t *testing.T) { func TestClient_GetVersion(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) version, err := c.GetVersion("IQ3UGSc2") if err != nil { t.Fatal(err) @@ -142,7 +142,7 @@ func TestClient_GetVersion(t *testing.T) { func TestClient_GetVersions(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) versions, err := c.GetVersions([]string{"IQ3UGSc2", "DrzwF8io", "foobar"}) if err != nil { t.Fatal(err) @@ -154,7 +154,7 @@ func TestClient_GetVersions(t *testing.T) { func TestClient_VersionFromHash(t *testing.T) { t.Parallel() - c := NewClient() + c := NewClient(nil) version, err := c.VersionFromHash("619e250c133106bacc3e3b560839bd4b324dfda8", "sha1") if err != nil { t.Fatal(err) diff --git a/api/client.go b/api/client.go index 487ed9c..35bf711 100644 --- a/api/client.go +++ b/api/client.go @@ -13,19 +13,24 @@ import ( type Client struct { UserAgent string + BaseUrl string HTTPClient *http.Client } -func NewClient() *Client { - userAgent := "gorinth" +func NewClient(host *string) *Client { + client := &Client{ + UserAgent: "gorinth", + BaseUrl: "https://api.modrinth.com/", + HTTPClient: &http.Client{}, + } info, ok := debug.ReadBuildInfo() if ok { - userAgent = info.Main.Path + "/" + info.Main.Version + client.UserAgent = info.Main.Path + "/" + info.Main.Version } - return &Client{ - UserAgent: userAgent, - HTTPClient: &http.Client{}, + if host != nil { + client.BaseUrl = "https://" + *host + "/" } + return client } func (client *Client) buildRequest(method string, url string, body io.Reader) *http.Request { diff --git a/api/endpoints.go b/api/endpoints.go index cf0b88f..3f421e9 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -4,12 +4,10 @@ import ( url2 "net/url" ) -const baseUrl = "https://api.modrinth.com/" const apiVersion = "v2" -const apiUrl = baseUrl + apiVersion func (client *Client) LabrinthInfo() (*LabrinthInfo, error) { - url, err := url2.Parse(baseUrl) + url, err := url2.Parse(client.BaseUrl) if err != nil { return nil, err } @@ -27,7 +25,7 @@ func (client *Client) LabrinthInfo() (*LabrinthInfo, error) { // GetProject https://docs.modrinth.com/api-spec/#tag/projects/operation/getProject func (client *Client) GetProject(id string) (*Project, error) { - url, err := url2.Parse(apiUrl + "/project/" + id) + url, err := url2.Parse(client.BaseUrl + apiVersion + "/project/" + id) if err != nil { return nil, err } @@ -43,7 +41,7 @@ func (client *Client) GetProject(id string) (*Project, error) { // GetProjects https://docs.modrinth.com/api-spec/#tag/projects/operation/getProjects func (client *Client) GetProjects(ids []string) ([]*Project, error) { - url, err := url2.Parse(apiUrl + "/projects") + url, err := url2.Parse(client.BaseUrl + apiVersion + "/projects") if err != nil { return nil, err } @@ -63,7 +61,7 @@ func (client *Client) GetProjects(ids []string) ([]*Project, error) { // CheckProjectValidity https://docs.modrinth.com/api-spec/#tag/projects/operation/checkProjectValidity func (client *Client) CheckProjectValidity(id string) (*CheckResponse, error) { - url, err := url2.Parse(apiUrl + "/project/" + id + "/check") + url, err := url2.Parse(client.BaseUrl + apiVersion + "/project/" + id + "/check") if err != nil { return nil, err } @@ -79,7 +77,7 @@ func (client *Client) CheckProjectValidity(id string) (*CheckResponse, error) { // GetDependencies https://docs.modrinth.com/api-spec/#tag/projects/operation/getDependencies func (client *Client) GetDependencies(id string) (*Dependencies, error) { - url, err := url2.Parse(apiUrl + "/project/" + id + "/dependencies") + url, err := url2.Parse(client.BaseUrl + apiVersion + "/project/" + id + "/dependencies") if err != nil { return nil, err } @@ -97,7 +95,7 @@ func (client *Client) GetDependencies(id string) (*Dependencies, error) { // GetProjectVersions https://docs.modrinth.com/api-spec/#tag/versions/operation/getProjectVersions func (client *Client) GetProjectVersions(id string, params *GetProjectVersionsParams) ([]*Version, error) { - url, err := url2.Parse(apiUrl + "/project/" + id + "/version") + url, err := url2.Parse(client.BaseUrl + apiVersion + "/project/" + id + "/version") if err != nil { return nil, err } @@ -131,7 +129,7 @@ type GetProjectVersionsParams struct { // GetVersion https://docs.modrinth.com/api-spec/#tag/versions/operation/getVersion func (client *Client) GetVersion(id string) (*Version, error) { - url, err := url2.Parse(apiUrl + "/version/" + id) + url, err := url2.Parse(client.BaseUrl + apiVersion + "/version/" + id) if err != nil { return nil, err } @@ -147,7 +145,7 @@ func (client *Client) GetVersion(id string) (*Version, error) { // GetVersions https://docs.modrinth.com/api-spec/#tag/versions/operation/getVersions func (client *Client) GetVersions(ids []string) ([]*Version, error) { - url, err := url2.Parse(apiUrl + "/versions") + url, err := url2.Parse(client.BaseUrl + apiVersion + "/versions") if err != nil { return nil, err } @@ -169,7 +167,7 @@ func (client *Client) GetVersions(ids []string) ([]*Version, error) { // VersionFromHash https://docs.modrinth.com/api-spec/#tag/version-files/operation/versionFromHash func (client *Client) VersionFromHash(hash string, algorithm HashAlgo) (*Version, error) { - url, err := url2.Parse(apiUrl + "/version_file/" + hash) + url, err := url2.Parse(client.BaseUrl + apiVersion + "/version_file/" + hash) if err != nil { return nil, err } diff --git a/cmd/ping.go b/cmd/ping.go new file mode 100644 index 0000000..ad7ae13 --- /dev/null +++ b/cmd/ping.go @@ -0,0 +1,36 @@ +package cmd + +import ( + "fmt" + "log" + + "github.com/nothub/gorinth/api" + "github.com/spf13/cobra" +) + +var host *string + +func init() { + host = pingCmd.Flags().String("host", "api.modrinth.com", "Host address") + + rootCmd.AddCommand(pingCmd) +} + +var pingCmd = &cobra.Command{ + Use: "ping", + Short: "Ping Labrinth instance", + Long: `Connect to a Labrinth instance and display basic information.`, + + Run: func(cmd *cobra.Command, args []string) { + client := api.NewClient(host) + + info, err := client.LabrinthInfo() + if err != nil { + log.Fatalln(err) + } + + fmt.Println(info.About) + fmt.Println(info.Name, info.Version) + fmt.Println(info.Documentation) + }, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..85ceb3c --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,35 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +func init() { + // global flags + rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution") + rootCmd.PersistentFlags().Bool("dumm", true, "Use dumm for dumm") + + // local flags + rootCmd.Flags().BoolP("toggle", "t", false, "toggle message for toggle flag") +} + +var rootCmd = &cobra.Command{ + Use: "gorinth", + Short: "Modrinth Modpack server deployment", + Long: `A longer description that spans multiple lines and likely +contains examples and usage of using your application. +For example: +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..7d9f6a8 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "fmt" + "log" + "path" + "runtime/debug" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version infos", + Long: `Extract and display the running binaries embedded version information.`, + + Run: func(cmd *cobra.Command, args []string) { + info, ok := debug.ReadBuildInfo() + if ok { + fmt.Println(path.Base(info.Main.Path), info.Main.Version) + } else { + log.Fatalln("Unable to extract build infos from running binary!") + } + }, +} diff --git a/go.mod b/go.mod index 8170640..6962f43 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module github.com/nothub/gorinth go 1.19 + +require github.com/spf13/cobra v1.5.0 + +require ( + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum index e69de29..0d85248 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= +github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index 54fa816..2fdb559 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,9 @@ package main import ( - "fmt" - "github.com/nothub/gorinth/api" - "log" + "github.com/nothub/gorinth/cmd" ) func main() { - client := api.NewClient() - info, err := client.LabrinthInfo() - if err != nil { - log.Fatalln(err) - } - fmt.Println(info.About) - fmt.Println(info.Name, info.Version) - fmt.Println(info.Documentation) + cmd.Execute() }