Skip to content

Commit

Permalink
Add tests for e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
drewgonzales360 committed Apr 2, 2022
1 parent 1c9f2ef commit 5ab9709
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 41 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: goenv

on:
push:
branches: [ $default-branch ]
pull_request:
branches: [ $default-branch ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.17

- name: Run pre-commit hook
run: ./.githooks/pre-commit
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
FROM ubuntu:latest
FROM drewgonzales360/drew:latest

COPY goenv /usr/local/bin

RUN \
apt-get update \
&& apt-get install -y ca-certificates
COPY scripts/goenv-test.sh /usr/local/bin/goenv-test
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ SEMVER=0.0.1
build:
@go build -ldflags="-X 'github.com/drewgonzales360/goenv/version.Semver=${SEMVER}${PRERELEASE}${BUILD_METADATA}'"

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

image: build-linux
@docker build -t goenv .

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

tty:
@docker run --rm -it goenv bash
ty:
@docker run --rm -it goenv
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Alfred
# goenv

Alfred is a template for Go projects that we can use in the future. It sets up a _very_ basic CLI.
![example workflow](https://github.com/drewgonzales360/goenv/actions/workflows/github-actions.yml/badge.svg)

goenv is an small, simple binary that executes the [install instructions](https://go.dev/doc/install) on the Go website. There are several other implementations that have much more support. This has fewer features and is only meant to serve me.
19 changes: 14 additions & 5 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ const (
InstallDirectory string = "/usr/local/go/"
)

func Install(c *cli.Context) error {
func InstallCommand(c *cli.Context) error {
version := ""
if c.NArg() > 0 {
version = c.Args().Get(0)
}

if err := Install(version); err != nil {
pkg.Fail(err.Error())
}
return nil
}

func Install(version string) error {
if inaccessible := pkg.CheckRW(); len(inaccessible) > 0 {
return fmt.Errorf(PermError, inaccessible)
}

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

downloadURL := pkg.FormatDownloadURL(*goVersion)
filePath, err := pkg.DownloadFile(downloadURL)
filePath, err := pkg.DownloadFile(*goVersion)
if err != nil {
return errors.Wrap(err, "could not download go")
}
Expand All @@ -35,6 +45,5 @@ func Install(c *cli.Context) error {
return errors.Wrap(err, "could not extract go")
}

Use(c)
return nil
return Use(version)
}
14 changes: 12 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ package cmd

import (
"fmt"
"os"

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

func List(c *cli.Context) error {
fmt.Println("Hello world!")
func ListCommand(c *cli.Context) error {
versions, err := os.ReadDir(InstallDirectory)
if err != nil {
pkg.Fail(err.Error())
}

for _, version := range versions {
fmt.Println(version.Name())
}

return nil
}
33 changes: 31 additions & 2 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@ package cmd

import (
"fmt"
"os"

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

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

if err := Uninstall(version); err != nil {
pkg.Fail(err.Error())
}
return nil
}

func Uninstall(version string) error {
if inaccessible := pkg.CheckRW(); len(inaccessible) > 0 {
return fmt.Errorf(PermError, inaccessible)
}

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

if err = os.RemoveAll(InstallDirectory + goVersion.Original()); err != nil {
return errors.Wrap(err, "could not uninstall go")
}

pkg.Success("Uninstalled Go " + goVersion.Original())
return nil
}
58 changes: 49 additions & 9 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,76 @@ import (
"fmt"
"os"
"os/exec"
"strings"

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

func Use(c *cli.Context) error {
const (
UsrLocalBin string = "/usr/local/bin/"
PermError string = "are you sure you're root? you do not have access to %v"
)

func UseCommand(c *cli.Context) error {
version := ""
if c.NArg() > 0 {
version = c.Args().Get(0)
}

if err := Use(version); err != nil {
pkg.Fail(err.Error())
}
return nil
}

func Use(version string) error {
if inaccessible := pkg.CheckRW(); len(inaccessible) > 0 {
return fmt.Errorf(PermError, inaccessible)
}

goVersion, err := semver.NewVersion(version)
if err != nil {
return fmt.Errorf("could not parse version as a semver")
return errors.Wrap(err, "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 = Link(goVersion, "go"); err != nil {
return err
}

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

output, _ := exec.Command("go version").Output()
output, err := exec.Command("/usr/local/bin/go", "version").Output()
if err != nil {
fmt.Println(err.Error())
pkg.Debug(err.Error())
return err
}

pkg.Success(fmt.Sprintf("Using %s", strings.TrimSuffix(string(output), "\n")))
return nil
}

func Link(goVersion *semver.Version, binary string) error {
usrLocalBinSymlink := UsrLocalBin + binary
if _, err := os.Stat(usrLocalBinSymlink); err == nil {
if err = os.Remove(usrLocalBinSymlink); err != nil {
return errors.Wrap(err, "could not remove "+usrLocalBinSymlink)
}
}

usrLocalGoVersionBin := InstallDirectory + goVersion.Original() + "/bin/" + binary
if _, err := os.Stat(usrLocalGoVersionBin); err != nil {
pkg.Debug(err.Error())
return fmt.Errorf("could not find go version %s. goenv install %s", goVersion.Original(), goVersion.Original())
}

if err := os.Symlink(usrLocalGoVersionBin, usrLocalBinSymlink); err != nil {
return errors.Wrap(err, "could not link "+binary)
}
fmt.Println(string(output))

return nil
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ go 1.17

require (
github.com/Masterminds/semver v1.5.0
github.com/briandowns/spinner v1.18.1
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.3.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/fatih/color v1.7.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.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
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.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
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.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -13,5 +21,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ func main() {
Name: "install",
Usage: "install a go version",
Aliases: []string{"i"},
Action: cmd.Install,
Action: cmd.InstallCommand,
},
{
Name: "uninstall",
Usage: "uninstall a go version",
Aliases: []string{"rm"},
Action: cmd.Uninstall,
Action: cmd.UninstallCommand,
},
{
Name: "use",
Usage: "use a go version",
Aliases: []string{"u"},
Action: cmd.Use,
Action: cmd.UseCommand,
},
{
Name: "list",
Usage: "list all available go versions",
Aliases: []string{"ls", "l"},
Action: cmd.List,
Action: cmd.ListCommand,
},
},
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pkg

import (
"fmt"
"os"
)

const (
colorRed string = "\033[31m"
colorGreen string = "\033[32m"
colorReset string = "\033[0m"
)

func Success(mesg string) {
fmt.Printf("😎 %s%s%s\n", colorGreen, mesg, colorReset)
}

func Fail(mesg string) {
fmt.Printf("😭 %s%s%s\n", colorRed, mesg, colorReset)
}

func Debug(mesg string) {
logLevel := os.Getenv("GOENV_LOG")
if logLevel == "DEBUG" {
fmt.Printf("🤔 %s%s%s\n", colorRed, mesg, colorReset)
}
}
25 changes: 25 additions & 0 deletions pkg/permissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pkg

import (
"os"
"time"
)

func CheckRW() []string {
accessDenied := []string{}
currentTime := time.Now().Local()

installDirectory := "/usr/local/"
if err := os.Chtimes(installDirectory, currentTime, currentTime); err != nil {
Debug(err.Error())
accessDenied = append(accessDenied, installDirectory)
}

binDirectory := "/usr/local/bin/"
if err := os.Chtimes(binDirectory, currentTime, currentTime); err != nil {
Debug(err.Error())
accessDenied = append(accessDenied, binDirectory)
}

return accessDenied
}
Loading

0 comments on commit 5ab9709

Please sign in to comment.