Skip to content

Commit

Permalink
add resto get-latest command and add it to root cmd, update go modules
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Dec 24, 2021
1 parent 04e5f7d commit 1614839
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 3 deletions.
25 changes: 25 additions & 0 deletions cli/get-latest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

import (
"github.com/spf13/cobra"
)

func GetLatestCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "get-latest <repo || id> [flags]",
Short: "Get Latest repository tag",
Long: `Get The Latest Tag of a repository from a registry (github, gitlab, bitbucket).`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
getLatestOpts.Repo = args[0]
}

return runGetLatest(&getLatestOpts)
},
}

cmd.Flags().StringVarP(&getLatestOpts.Registry, "registry", "r", "", "The registry to use")
cmd.Flags().StringVarP(&getLatestOpts.Token, "token", "t", "", "The access token to use it the registry requires authentication")

return cmd
}
6 changes: 6 additions & 0 deletions cli/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ var withBodyOpts = options.CLIOptions{
IsBodyStdin: false,
},
}

var getLatestOpts = options.GetLatestCommandOptions{
Registry: "",
Repo: "",
Token: "",
}
82 changes: 79 additions & 3 deletions cli/runners.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package cli

import (
// "encoding/json"
"fmt"
"os"
"log"
"io/ioutil"
"log"
"net/http"
"os"
// "strings"

"github.com/abdfnx/resto/tools"
httpClient "github.com/abdfnx/resto/client"
"github.com/abdfnx/resto/core/api"
"github.com/abdfnx/resto/core/editor"
"github.com/abdfnx/resto/core/editor/runtime"
"github.com/abdfnx/resto/core/options"
"github.com/abdfnx/resto/tools"

tcell "github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/tidwall/gjson"
)

func runBasic(opts *options.CLIOptions, method string) error {
Expand Down Expand Up @@ -136,6 +141,7 @@ func runWithBody(opts *options.CLIOptions, method string) error {

return event
})

app.SetRoot(bodyEditor, true)

if err := app.Run(); err != nil {
Expand Down Expand Up @@ -225,3 +231,73 @@ func runWithBody(opts *options.CLIOptions, method string) error {

return nil
}

func runGetLatest(opts *options.GetLatestCommandOptions) error {
registry := "github.com"

if opts.Registry != "" {
registry = opts.Registry
}

url := "https://api.github.com/repos/" + opts.Repo + "/releases/latest"

if registry == "gitlab.com" {
url = "https://gitlab.com/api/v4/projects/" + opts.Repo + "/repository/tags"
} else if registry == "bitbucket.org" {
url = "https://api.bitbucket.org/2.0/repositories/" + opts.Repo + "/refs"
}

req, err := http.NewRequest("GET", url, nil)

if err != nil {
return fmt.Errorf("Error creating request: %s", err.Error())
}

if err != nil {
return err
}

if opts.Token != "" {
if opts.Registry == "gitlab.com" {
req.Header.Add("PRIVATE-TOKEN", opts.Token)
} else {
req.Header.Add("Authorization", "Bearer " + opts.Token)
}
}

client := httpClient.HttpClient()
res, err := client.Do(req)

if err != nil {
return fmt.Errorf("Error sending request: %s", err.Error())
}

defer res.Body.Close()

b, err := ioutil.ReadAll(res.Body)

if err != nil {
return err
}

body := string(b)

v := gjson.Get(body, "tag_name")

if registry == "gitlab.com" {
value := gjson.Get(body, "#.name")
v = gjson.Get(value.String(), "0")
} else if registry == "bitbucket.org" {
value := gjson.Get(body, "values")
value2 := gjson.Get(value.String(), "0")
v = gjson.Get(value2.String(), "name")
}

if v.Exists() {
fmt.Println(v.String())
} else {
fmt.Println("no releases found")
}

return nil
}
1 change: 1 addition & 0 deletions cmd/resto/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func Execute(f *factory.Factory, version string, versionDate string) *cobra.Comm
cli.HeadCMD(),
install_cmd.InstallCMD(),
runCmd.RunCMD(),
cli.GetLatestCMD(),
versionCmd,
)

Expand Down
6 changes: 6 additions & 0 deletions core/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ type RunCommandOptions struct {
Path string
ShowAll bool
}

type GetLatestCommandOptions struct {
Registry string
Repo string
Token string
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/tidwall/gjson v1.12.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.12.1 h1:ikuZsLdhr8Ws0IdROXUS1Gi4v9Z4pGqpX/CvJkxvfpo=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
Expand Down

0 comments on commit 1614839

Please sign in to comment.