Skip to content

Commit

Permalink
cobra cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Aug 31, 2022
1 parent 0fbdabd commit 633091f
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 41 deletions.
26 changes: 13 additions & 13 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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!")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"},
})
Expand All @@ -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"},
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 9 additions & 11 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions cmd/ping.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
35 changes: 35 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
29 changes: 29 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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!")
}
},
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
13 changes: 2 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 633091f

Please sign in to comment.