Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
drewgonzales360 committed Mar 31, 2022
1 parent de5bd0f commit 1c9f2ef
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 28 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:latest

COPY goenv /usr/local/bin

RUN \
apt-get update \
&& apt-get install -y ca-certificates
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BUILD_METADATA=+$(shell git rev-parse --short HEAD)
PRERELEASE=
SEMVER=0.0.1

build:
@go build -ldflags="-X 'github.com/drewgonzales360/goenv/version.Semver=${SEMVER}${PRERELEASE}${BUILD_METADATA}'"

image: build
@docker build -t goenv .

run-image:
@docker run --rm -it goenv bash -c 'goenv install 1.18; go version'

tty:
@docker run --rm -it goenv bash
14 changes: 5 additions & 9 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package cmd

import (
"fmt"
"os"

"github.com/Masterminds/semver"
"github.com/urfave/cli/v2"

"github.com/drewgonzales360/goenv/pkg"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)

const (
Expand All @@ -28,17 +27,14 @@ func Install(c *cli.Context) error {
downloadURL := pkg.FormatDownloadURL(*goVersion)
filePath, err := pkg.DownloadFile(downloadURL)
if err != nil {
return fmt.Errorf("could not download go")
return errors.Wrap(err, "could not download go")
}

err = pkg.ExtractTarGz(filePath, InstallDirectory+goVersion.Original())
if err != nil {
return fmt.Errorf("could not extract go")
}

if err := os.Symlink("/usr/local/bin/go", InstallDirectory+goVersion.Original()+"/bin/go"); err != nil {
return fmt.Errorf("could not link go")
return errors.Wrap(err, "could not extract go")
}

Use(c)
return nil
}
29 changes: 28 additions & 1 deletion cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@ package cmd

import (
"fmt"
"os"
"os/exec"

"github.com/Masterminds/semver"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)

func Use(c *cli.Context) error {
fmt.Println("Hello world!")
version := ""
if c.NArg() > 0 {
version = c.Args().Get(0)
}

goVersion, err := semver.NewVersion(version)
if err != nil {
return fmt.Errorf("could not parse version as a semver")
}

if err := os.Symlink(InstallDirectory+goVersion.Original()+"/bin/go", "/usr/local/bin/go"); err != nil {
return errors.Wrap(err, "could not link go")
}

if err := os.Symlink(InstallDirectory+goVersion.Original()+"/bin/gofmt", "/usr/local/bin/gofmt"); err != nil {
return errors.Wrap(err, "could not link gofmt")
}

output, _ := exec.Command("go version").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(output))

return nil
}
30 changes: 17 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@ func main() {
app := &cli.App{
Name: version.AppName,
Usage: "Manages multiple go versions for linux",
Version: version.Version,
Version: version.Version().Original(),
Commands: []*cli.Command{
{
Name: "install",
Usage: "install a go version",
Action: cmd.Install,
Name: "install",
Usage: "install a go version",
Aliases: []string{"i"},
Action: cmd.Install,
},
{
Name: "uninstall",
Usage: "uninstall a go version",
Action: cmd.Uninstall,
Name: "uninstall",
Usage: "uninstall a go version",
Aliases: []string{"rm"},
Action: cmd.Uninstall,
},
{
Name: "use",
Usage: "use a go version",
Action: cmd.Use,
Name: "use",
Usage: "use a go version",
Aliases: []string{"u"},
Action: cmd.Use,
},
{
Name: "list",
Usage: "list all available go versions",
Action: cmd.List,
Name: "list",
Usage: "list all available go versions",
Aliases: []string{"ls", "l"},
Action: cmd.List,
},
},
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
"strings"
"time"

"github.com/pkg/errors"

"github.com/Masterminds/semver"
"github.com/pkg/errors"
)

const (
Expand All @@ -37,7 +36,13 @@ func RandomString() string {
}

func FormatDownloadURL(v semver.Version) string {
return fmt.Sprintf("https://go.dev/dl/go%s.linux-amd64.tar.gz", v.String())
urlVersion := v.String()
// If we have 1.18, we'd parse the version to 1.18.0, but the URL doesn't
// actually inclued the last .0
if v.Patch() == 0 {
urlVersion = strings.TrimSuffix(urlVersion, ".0")
}
return fmt.Sprintf("https://go.dev/dl/go%s.linux-amd64.tar.gz", urlVersion)
}

// DownloadFile will download a url to a local file. It's efficient because it will
Expand Down Expand Up @@ -105,6 +110,12 @@ func ExtractTarGz(tarballPath, destinationPath string) error {
if _, err := io.Copy(outFile, tarReader); err != nil {
return errors.Wrap(err, "could not write to file")
}

err = os.Chmod(outFile.Name(), header.FileInfo().Mode())
if err != nil {
return errors.Wrap(err, "could not set extracted file permissions")
}

outFile.Close()

default:
Expand Down
12 changes: 10 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package version

import (
"github.com/Masterminds/semver"
)

// AppName represents the name of the application
const AppName = "app-name"
const AppName = "goenv"

// Version semvers the app
const Version = "0.0.1"
var Semver string = "unknown; please create an issue for the maintainers"

func Version() *semver.Version {
return semver.MustParse(Semver)
}

0 comments on commit 1c9f2ef

Please sign in to comment.