feat: add use, init and ls commands
This commit is contained in:
parent
1814f5c935
commit
a95592485a
262
cmd/main.go
262
cmd/main.go
|
@ -4,78 +4,228 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/datahearth/doggo-fetcher/pkg"
|
||||
"github.com/sirupsen/logrus"
|
||||
"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",
|
||||
},
|
||||
var (
|
||||
logger = logrus.New()
|
||||
app = &cli.App{
|
||||
Name: "dg",
|
||||
Usage: "I bring you your latest GoLang release with ease and efficiency (like a stick) !",
|
||||
Description: `Doggo-fetcher is a utility tool that manage for you your installed GoLang releases.
|
||||
You can select a specific GoLang release or even set a specific one for directories.`,
|
||||
EnableBashCompletion: true,
|
||||
Authors: []*cli.Author{
|
||||
{
|
||||
Name: "Antoine <DataHearth> Langlois",
|
||||
Email: "antoine.l@antoine-langlois.net",
|
||||
},
|
||||
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|--lts\" is not passed")
|
||||
}
|
||||
release = "lts"
|
||||
} else {
|
||||
release = ctx.Args().First()
|
||||
},
|
||||
Suggest: true,
|
||||
Version: "0.1.0",
|
||||
Commands: []*cli.Command{
|
||||
useCmd,
|
||||
initCmd,
|
||||
uninstallCmd,
|
||||
lsCmd,
|
||||
},
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}, Usage: "Enable verbose mode"},
|
||||
},
|
||||
}
|
||||
useCmd = &cli.Command{
|
||||
Name: "use",
|
||||
Usage: "Set a specific golang version",
|
||||
Description: `Use a specific golang version as primary golang binary for the user.
|
||||
If the version is not already downloaded, it'll downloaded and installed automatically.`,
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "latest",
|
||||
Aliases: []string{"lts"},
|
||||
Usage: "Use 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 {
|
||||
if ctx.Bool("verbose") {
|
||||
logger.Level = logrus.DebugLevel
|
||||
}
|
||||
|
||||
var release string
|
||||
if ctx.NArg() == 0 {
|
||||
if !ctx.Bool("latest") {
|
||||
return errors.New("a release is required if \"--latest|--lts\" is not passed")
|
||||
}
|
||||
release = pkg.LTS
|
||||
} else {
|
||||
release = ctx.Args().First()
|
||||
}
|
||||
|
||||
localFolder := strings.Replace(pkg.DGF_FOLDER, "~", os.Getenv("HOME"), 1)
|
||||
fi, err := os.Stat(localFolder)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
ghTags := pkg.NewTags(release, ctx.Context)
|
||||
release, err := ghTags.GetRelease(ctx.Bool("beta"), ctx.Bool("rc"))
|
||||
if err := os.MkdirAll(localFolder, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if !fi.IsDir() {
|
||||
if err := os.RemoveAll(localFolder); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(localFolder, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
release, err = pkg.NewTags(release, ctx.Context).GetRelease(ctx.Bool("beta"), ctx.Bool("rc"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hash, err := pkg.NewHash(localFolder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r := pkg.NewRelease(release, filepath.Join(localFolder, release))
|
||||
|
||||
if err := r.CheckReleaseExists(); err != nil {
|
||||
if err != pkg.ErrReleaseNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("Release not found, downloading...")
|
||||
if err := r.DownloadRelease(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("Release downloaded, installing...")
|
||||
if err := r.ExtractRelease(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Debug("Release installed, generating hash...")
|
||||
h, err := hash.GetFolderHash(r.GetReleaseFolder())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := hash.AddHash(r.GetReleaseFolder(), h); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
logger.Debug("Release found, checking hash...")
|
||||
h, err := hash.GetFolderHash(r.GetReleaseFolder())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("release: %v\n", release)
|
||||
if err := hash.CompareReleaseHash(r.GetReleaseFolder(), h); err != nil {
|
||||
if err == pkg.ErrHashNotFound {
|
||||
logger.Warnln("Hash not found in hash table, adding...")
|
||||
if err := hash.AddHash(r.GetReleaseFolder(), h); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err == pkg.ErrHashInvalid {
|
||||
logger.Warnln("Hash invalid, replacing...")
|
||||
if err := hash.ReplaceHash(r.GetReleaseFolder(), h); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
logger.Info("Setting golang binary")
|
||||
if err := pkg.UpdateRelease(r.GetReleaseFolder()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("Everything done!")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
fmt.Println("Hello friend!")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
initCmd = &cli.Command{
|
||||
Name: "init",
|
||||
Usage: "Initialize doggofetcher",
|
||||
Description: `Initialize doggofetcher by adding a custom path inside your sheel configuration file.
|
||||
It also creates a folder at "~/.local/doggofetcher" to store your custom golang binaries.`,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if ctx.Bool("verbose") {
|
||||
logger.Level = logrus.DebugLevel
|
||||
}
|
||||
|
||||
logger.Infoln("Initializing doggofetcher...")
|
||||
return pkg.Init()
|
||||
},
|
||||
}
|
||||
uninstallCmd = &cli.Command{
|
||||
Name: "uninstall",
|
||||
Usage: "Uninstall golang release",
|
||||
Description: `Uninstall current golang release.
|
||||
It will remove the folder at "~/.local/doggofetcher/go"`,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return os.RemoveAll(filepath.Join(strings.Replace(pkg.DGF_FOLDER, "~", os.Getenv("HOME"), 1), "go"))
|
||||
},
|
||||
}
|
||||
lsCmd = &cli.Command{
|
||||
Name: "ls",
|
||||
Usage: "List available releases",
|
||||
Description: `List available releases.
|
||||
It will list all available releases in the "~/.local/doggofetcher/go*" folder.`,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if ctx.Bool("verbose") {
|
||||
logger.Level = logrus.DebugLevel
|
||||
}
|
||||
|
||||
items, err := os.ReadDir(strings.Replace(pkg.DGF_FOLDER, "~", os.Getenv("HOME"), 1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, i := range items {
|
||||
if i.IsDir() && i.Name() != "go" {
|
||||
fmt.Println(i.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// todo: ls (list downloaded-releases)
|
||||
// todo: ls-remote (list remote releases)
|
||||
// todo: exec (without setting release=
|
||||
// todo: alias (set alias for a release)
|
||||
// todo: run-bg (check each 10mins)
|
||||
// todo: auto-use (automatically switch when changing directory)
|
||||
// todo: remove (remove a release from local folder)
|
||||
|
||||
func main() {
|
||||
cli.VersionFlag = &cli.BoolFlag{
|
||||
Name: "version",
|
||||
Aliases: []string{"V"},
|
||||
Usage: "print the version",
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
fmt.Println(err)
|
||||
logger.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
package doggofetcher
|
11
go.mod
11
go.mod
|
@ -5,10 +5,21 @@ go 1.18
|
|||
require github.com/urfave/cli/v2 v2.8.1
|
||||
|
||||
require (
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/briandowns/spinner v1.18.1 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
|
||||
github.com/fatih/color v1.7.0 // indirect
|
||||
github.com/google/go-github/v45 v45.1.0 // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.2 // indirect
|
||||
github.com/mattn/go-isatty v0.0.8 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
)
|
||||
|
|
64
go.sum
64
go.sum
|
@ -1,16 +1,80 @@
|
|||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/briandowns/spinner v1.18.1 h1:yhQmQtM1zsqFsouh09Bk/jCjd50pC3EOGsh28gLVvwY=
|
||||
github.com/briandowns/spinner v1.18.1/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
|
||||
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
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/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
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/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
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=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
|
||||
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
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/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
package pkg
|
||||
|
||||
const GO_DL_SERVER = "https://dl.google.com/go"
|
||||
|
|
@ -0,0 +1,157 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HashActions interface {
|
||||
GetFolderHash(path string) (string, error)
|
||||
CompareReleaseHash(path string, hash string) error
|
||||
AddHash(path, hash string) error
|
||||
ReplaceHash(path, hash string) error
|
||||
}
|
||||
|
||||
type Hash struct {
|
||||
hashTable map[string]string
|
||||
hashTablePath string
|
||||
}
|
||||
|
||||
// NewHash returns a new Hash object. It reads the hashes from the ~/.local/doggofetcher/hashes.txt file then loads
|
||||
// them into the hash table.
|
||||
func NewHash(localFolder string) (HashActions, error) {
|
||||
hashTablePath := filepath.Join(localFolder, HASHES_FILE)
|
||||
f, err := os.OpenFile(hashTablePath, os.O_CREATE|os.O_RDONLY, 0644)
|
||||
if err != nil {
|
||||
return &Hash{}, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
hashTable := map[string]string{}
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
l := strings.Split(sc.Text(), " ")
|
||||
hashTable[l[0]] = l[1]
|
||||
}
|
||||
|
||||
return &Hash{
|
||||
hashTable: hashTable,
|
||||
hashTablePath: hashTablePath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetFolderHash returns the hash of the folder by using the Merkle tree.
|
||||
func (h *Hash) GetFolderHash(path string) (string, error) {
|
||||
hashes := [][]byte{}
|
||||
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !d.Type().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
if d.IsDir() {
|
||||
hash, err := h.GetFolderHash(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hashes = append(hashes, []byte(hash))
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
sha := sha256.New()
|
||||
buf := make([]byte, 10*1024)
|
||||
for {
|
||||
n, err := f.Read(buf)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
sha.Write(buf[:n])
|
||||
}
|
||||
|
||||
hashes = append(hashes, sha.Sum(nil))
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sha := sha256.New()
|
||||
for _, h := range hashes {
|
||||
if _, err := sha.Write(h); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return string(sha.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// CompareReleaseHash compares the hash of the release with the hash in the hash table.
|
||||
func (h *Hash) CompareReleaseHash(path, hash string) error {
|
||||
if h, ok := h.hashTable[path]; !ok {
|
||||
return ErrHashNotFound
|
||||
} else if h != hash {
|
||||
return ErrHashInvalid
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// AddHash adds a hash to the hash table by writing the "hashTable" property and the file.
|
||||
func (h *Hash) AddHash(path, hash string) error {
|
||||
h.hashTable[path] = hash
|
||||
|
||||
f, err := os.OpenFile(h.hashTablePath, os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.WriteString(fmt.Sprintf("%s %s\n", path, hash)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReplaceHash replaces the hash in the hash table with the new hash.
|
||||
func (h *Hash) ReplaceHash(path, hash string) error {
|
||||
h.hashTable[path] = hash
|
||||
|
||||
d, err := os.ReadFile(h.hashTablePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// todo: find a way to write only the relevent line
|
||||
var out []byte
|
||||
for _, l := range strings.Split(string(d), "\n") {
|
||||
if strings.Contains(l, path) {
|
||||
out = append(out, []byte(strings.Replace(l, strings.Split(l, " ")[1], hash, 1))...)
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.WriteFile(h.hashTablePath, out, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,161 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/briandowns/spinner"
|
||||
)
|
||||
|
||||
type ReleaseActions interface {
|
||||
DownloadRelease() error
|
||||
ExtractRelease() error
|
||||
CheckReleaseExists() error
|
||||
GetReleaseFolder() string
|
||||
downloadFile(*os.File) error
|
||||
}
|
||||
|
||||
type Release struct {
|
||||
release string
|
||||
releaseFolder string
|
||||
client *http.Client
|
||||
archivePath string
|
||||
}
|
||||
|
||||
// NewRelease returns a Release object
|
||||
func NewRelease(release, releaseFolder string) ReleaseActions {
|
||||
return &Release{
|
||||
release: release,
|
||||
releaseFolder: releaseFolder,
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
}
|
||||
|
||||
// DownloadRelease downloads the golang release
|
||||
func (d *Release) DownloadRelease() error {
|
||||
d.archivePath = fmt.Sprintf("%s/%s.tar.gz", os.TempDir(), d.release)
|
||||
f, err := os.Create(d.archivePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return d.downloadFile(f)
|
||||
}
|
||||
|
||||
// downloadFile downloads the file to the given writer
|
||||
func (d *Release) downloadFile(f *os.File) error {
|
||||
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond)
|
||||
s.Suffix = " Downloading release..."
|
||||
s.Start()
|
||||
defer s.Stop()
|
||||
|
||||
rsp, err := d.client.Get(fmt.Sprintf("%s/%s.%s-amd64.tar.gz", GO_DL_SERVER, d.release, runtime.GOOS))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
|
||||
if rsp.StatusCode != 200 {
|
||||
return errors.New("golang download server return a non success status code")
|
||||
}
|
||||
|
||||
if _, err := io.Copy(f, rsp.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractRelease extracts the golang release
|
||||
func (d *Release) ExtractRelease() error {
|
||||
f, err := os.Open(d.archivePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
gzipReader, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(gzipReader)
|
||||
|
||||
if err := os.MkdirAll(d.releaseFolder, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rootFolder string
|
||||
init := true
|
||||
for {
|
||||
h, err := tarReader.Next()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return os.Remove(d.archivePath)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// skip first folder
|
||||
if init {
|
||||
rootFolder = h.Name
|
||||
init = false
|
||||
continue
|
||||
}
|
||||
|
||||
target := filepath.Join(d.releaseFolder, strings.Replace(h.Name, rootFolder, "", 1))
|
||||
switch h.Typeflag {
|
||||
case tar.TypeDir:
|
||||
if err = os.MkdirAll(target, h.FileInfo().Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
file, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, h.FileInfo().Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = io.Copy(file, tarReader); err != nil {
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
file.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CheckReleaseExists checks if the release exists in doggofetcher folder
|
||||
func (d *Release) CheckReleaseExists() error {
|
||||
fi, err := os.Stat(d.releaseFolder)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return ErrReleaseNotFound
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if !fi.IsDir() {
|
||||
return ErrReleaseNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetReleaseFolder returns the release folder
|
||||
func (d *Release) GetReleaseFolder() string {
|
||||
return d.releaseFolder
|
||||
}
|
18
pkg/tags.go
18
pkg/tags.go
|
@ -2,7 +2,6 @@ package pkg
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -11,15 +10,10 @@ import (
|
|||
"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 TagsAction interface {
|
||||
CheckReleaseExists(beta, rc bool) (string, error)
|
||||
GetRelease(beta, rc bool) (string, error)
|
||||
getTagsRef() ([]*github.Reference, error)
|
||||
getLatestRelease(beta, rc bool) (string, error)
|
||||
getLatestTag(beta, rc bool) (string, error)
|
||||
}
|
||||
|
||||
type Tags struct {
|
||||
|
@ -28,7 +22,7 @@ type Tags struct {
|
|||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewTags(release string, ctx context.Context) Tags {
|
||||
func NewTags(release string, ctx context.Context) TagsAction {
|
||||
return Tags{
|
||||
release: release,
|
||||
client: *github.NewClient(nil),
|
||||
|
@ -41,7 +35,7 @@ func NewTags(release string, ctx context.Context) Tags {
|
|||
//
|
||||
// Returns the found release or an error
|
||||
func (t Tags) GetRelease(beta, rc bool) (string, error) {
|
||||
if t.release == "lts" {
|
||||
if t.release == LTS {
|
||||
return t.getLatestTag(beta, rc)
|
||||
}
|
||||
|
||||
|
@ -100,7 +94,7 @@ func (t Tags) getTagsRef() ([]*github.Reference, error) {
|
|||
return nil, err
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
fmt.Fprintf(os.Stderr, "non success github status code %d\n", response.StatusCode)
|
||||
fmt.Fprintf(os.Stderr, "%s: %d\n", ErrBadResponse.Error(), response.StatusCode)
|
||||
return nil, ErrBadResponse
|
||||
}
|
||||
if len(refs) == 0 {
|
||||
|
@ -123,9 +117,7 @@ func (t Tags) getLatestTag(beta, rc bool) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
fmt.Printf("len(tags): %v\n", len(refs))
|
||||
for i := len(refs) - 1; i >= 0; i-- {
|
||||
fmt.Printf("[%d] tags[i].Name: %v\n", i, *refs[i].Ref)
|
||||
if (!beta && strings.Contains(*refs[i].Ref, "beta")) || (!rc && strings.Contains(*refs[i].Ref, "rc")) {
|
||||
continue
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func UpdateRelease(releaseFolder string) error {
|
||||
activeReleaseFolder := filepath.Join(strings.Replace(DGF_FOLDER, "~", os.Getenv("HOME"), 1), "go")
|
||||
if err := os.RemoveAll(activeReleaseFolder); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(activeReleaseFolder, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return CopyFolder(releaseFolder, activeReleaseFolder, true)
|
||||
}
|
||||
|
||||
func CopyFolder(src, dst string, init bool) error {
|
||||
items, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, i := range items {
|
||||
info, err := i.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
src := filepath.Join(src, i.Name())
|
||||
dst := filepath.Join(dst, i.Name())
|
||||
if i.IsDir() {
|
||||
if err := os.MkdirAll(dst, info.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
CopyFolder(src, dst, false)
|
||||
continue
|
||||
}
|
||||
|
||||
f, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, info.Mode())
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := io.Copy(dstFile, f); err != nil {
|
||||
f.Close()
|
||||
dstFile.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
f.Close()
|
||||
dstFile.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Init() error {
|
||||
dgfFolder := strings.Replace(DGF_FOLDER, "~", os.Getenv("HOME"), 1)
|
||||
if err := os.MkdirAll(dgfFolder, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(getShellConfigFile(), os.O_WRONLY|os.O_APPEND, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
msg := fmt.Sprintln("\n\n# doggofetcher section")
|
||||
msg += fmt.Sprintf("export PATH=%s:$PATH\n", filepath.Join(dgfFolder, "go", "bin"))
|
||||
if _, err := f.WriteString(msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getShellConfigFile() string {
|
||||
if strings.Contains(os.Getenv("SHELL"), "zsh") {
|
||||
return fmt.Sprintf("%s/.zshrc", os.Getenv("HOME"))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/.bashrc", os.Getenv("HOME"))
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package pkg
|
||||
|
||||
import "errors"
|
||||
|
||||
const GO_DL_SERVER = "https://go.dev/dl"
|
||||
const DGF_FOLDER = "~/.local/doggofetcher"
|
||||
const HASHES_FILE = "hashes.txt"
|
||||
const LTS = "lts"
|
||||
|
||||
var (
|
||||
ErrReleaseNotFound = errors.New("release not found")
|
||||
ErrBadResponse = errors.New("github API responde with a non success code")
|
||||
ErrEmptyTags = errors.New("no tags found")
|
||||
ErrHashNotFound = errors.New("release hash not found")
|
||||
ErrHashInvalid = errors.New("release hash doesn't match")
|
||||
)
|
Reference in New Issue