initial commit

This commit is contained in:
DataHearth 2022-06-14 21:34:57 +02:00
commit cc7e971fee
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
7 changed files with 189 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.env
.DS_STORE
build

81
cmd/main.go Normal file
View File

@ -0,0 +1,81 @@
package main
import (
"errors"
"fmt"
"os"
"github.com/datahearth/doggo-fetcher/pkg"
"github.com/urfave/cli/v2"
)
var app = &cli.App{
Name: "dg",
Usage: "I bring you your latest Golang version with ease and efficiency (like a stick) !",
Description: `Doggo-fetcher is a utility tool that manage for you your Golang version.
You can select a specific go version or even set version for directories.`,
EnableBashCompletion: true,
Authors: []*cli.Author{{
Name: "Antoine <DataHearth> Langlois",
Email: "antoine.l@antoine-langlois.net",
}},
Suggest: true,
Version: "0.1.0",
Commands: []*cli.Command{
{
Name: "install",
Usage: "Download a given release",
Description: "Download a given release and set it as first release to be use",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "keep-release",
Usage: "Downloaded release archive will be stored",
},
&cli.BoolFlag{
Name: "latest",
Aliases: []string{"lts"},
Usage: "Download and install the latest release",
},
&cli.BoolFlag{
Name: "rc",
Usage: "Allow \"rc\" version to be fetched",
},
&cli.BoolFlag{
Name: "beta",
Usage: "Allow \"beta\" version to be fetched",
},
},
Action: func(ctx *cli.Context) error {
var release string
if ctx.NArg() == 0 {
if !ctx.Bool("latest") {
return errors.New("a release is required if \"--latest\" is not passed")
}
release = "lts"
} else {
release = ctx.Args().First()
}
ghTags := pkg.NewTags(release, ctx.Context)
release, err := ghTags.CheckReleaseExists(ctx.Bool("beta"), ctx.Bool("rc"))
if err != nil {
return err
}
fmt.Printf("release: %v\n", release)
return nil
},
},
},
Action: func(c *cli.Context) error {
fmt.Println("Hello friend!")
return nil
},
}
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}

1
doggo-fetcher.go Normal file
View File

@ -0,0 +1 @@
package doggofetcher

14
go.mod Normal file
View File

@ -0,0 +1,14 @@
module github.com/datahearth/doggo-fetcher
go 1.18
require github.com/urfave/cli/v2 v2.8.1
require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/google/go-github/v45 v45.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
)

16
go.sum Normal file
View File

@ -0,0 +1,16 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github/v45 v45.1.0 h1:SbUjHMRiCe9cHfu6Me4idWxLQEV8ZW9DLPz69zopyWo=
github.com/google/go-github/v45 v45.1.0/go.mod h1:FObaZJEDSTa/WGCzZ2Z3eoCDXWJKMenWWTrd8jrta28=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.8.1 h1:CGuYNZF9IKZY/rfBe3lJpccSoIY1ytfvmgQT90cNOl4=
github.com/urfave/cli/v2 v2.8.1/go.mod h1:Z41J9TPoffeoqP0Iza0YbAhGvymRdZAd2uPmZ5JxRdY=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

4
pkg/download.go Normal file
View File

@ -0,0 +1,4 @@
package pkg
const GO_DL_SERVER = "https://dl.google.com/go"

70
pkg/tags.go Normal file
View File

@ -0,0 +1,70 @@
package pkg
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/v45/github"
)
var (
ErrBadResponse = errors.New("github API responde with a non success code")
ErrEmptyTags = errors.New("no tags found")
)
type Tags struct {
release string
client github.Client
ctx context.Context
}
func NewTags(release string, ctx context.Context) Tags {
return Tags{
release: release,
client: *github.NewClient(nil),
ctx: ctx,
}
}
// checkReleaseExists retrieves tags from "golang/go" and check whether
// the given release exists in it
func (t Tags) CheckReleaseExists(beta, rc bool) (string, error) {
var tags []*github.RepositoryTag
var response *github.Response
var err error
tags, response, err = t.client.Repositories.ListTags(t.ctx, "golang", "go", nil)
if err != nil {
return "", err
}
if response.StatusCode != http.StatusOK {
return "", ErrBadResponse
}
if len(tags) == 0 {
return "", ErrEmptyTags
}
if t.release == "lts" {
return *tags[len(tags)-1].Name, nil
}
for i, tag := range tags {
userRelease := fmt.Sprintf("go%s", t.release)
if strings.Contains(*tag.Name, userRelease) {
if (!beta && strings.Contains(*tag.Name, "beta")) || (!rc && strings.Contains(*tag.Name, "rc")) {
continue
}
if strings.Contains(*tags[i+1].Name, userRelease) {
continue
}
return "", nil
}
}
return "", nil
}