Skip to content

Commit

Permalink
Merge pull request #44 from shihanng/issues-20__version
Browse files Browse the repository at this point in the history
Add version subcommand
  • Loading branch information
shihanng authored Dec 10, 2019
2 parents b00c2e9 + 6e1718a commit d2d570e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ builds:
- darwin
- windows
- linux

ldflags:
- -s -w -X main.version={{.Version}}
archives:
- replacements:
darwin: Darwin
Expand Down
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ func rootCmdRun(templatePath string, commitHash *string) func(cmd *cobra.Command
return err
}

_, err = repo.Checkout(r, *commitHash)
ch, err := repo.Checkout(r, *commitHash)
if err != nil {
return err
}

*commitHash = ch

return nil
}
}

func Execute(w io.Writer) {
func Execute(w io.Writer, version string) {
templatePath := filepath.Join(xdg.CacheHome(), `gig`)

var commitHash string
Expand All @@ -73,6 +75,7 @@ func Execute(w io.Writer) {
rootCmd.AddCommand(
newListCmd(w, templatePath),
newGenCmd(w, templatePath),
newVersionCmd(w, templatePath, version, &commitHash),
)

if err := rootCmd.Execute(); err != nil {
Expand Down
41 changes: 41 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright © 2019 Shi Han NG <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"
"io"

"github.com/spf13/cobra"
)

func newVersionCmd(w io.Writer, templatePath, version string, commitHash *string) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number and other useful info",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(w, "gig version %s\n", version)
fmt.Fprintf(w, "Cached github.com/toptal/gitignore in: %s\n", templatePath)
fmt.Fprintf(w, "Using github.com/toptal/gitignore commit hash: %s\n", *commitHash)
},
}
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/shihanng/gig/cmd"
)

var version = "dev"

func main() {
cmd.Execute(os.Stdout)
cmd.Execute(os.Stdout, version)
}
78 changes: 0 additions & 78 deletions main_integ_test.go

This file was deleted.

87 changes: 86 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// +build integration

package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"testing"

"github.com/OpenPeeDeeP/xdg"
"github.com/shihanng/gig/cmd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -19,7 +27,7 @@ func TestCli(t *testing.T) {

actual := new(bytes.Buffer)

cmd.Execute(actual)
cmd.Execute(actual, "test")

goldenPath := `./testdata/cli.golden`

Expand All @@ -31,3 +39,80 @@ func TestCli(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, expected, actual.Bytes())
}

func TestCheckGitIgnoreIO(t *testing.T) {
// Testing against "reactnative", "mean" is avoided because the result for stack from
// gitignore.io seems not in order.
resp, err := http.Get(`https://www.gitignore.io/api/django,androidstudio,java,go,ada,zsh,c,gradle`)
require.NoError(t, err)

defer resp.Body.Close()

expected := new(bytes.Buffer)
scanner := bufio.NewScanner(resp.Body)

for i := 0; scanner.Scan(); i++ {
if i < 3 {
continue
}

content := scanner.Text()

if strings.HasPrefix(content, `# End of https://www.gitignore.io/api/`) {
break
}

_, err := expected.WriteString(content + "\n")
require.NoError(t, err)
}

expectedBytes := expected.Bytes()
expectedBytes = expectedBytes[:len(expectedBytes)-1]

os.Args = []string{"gig", "gen", "Django", "androidstudio", "java", "go", "ada", "zsh", "c", "gradle", "go"}

actual := new(bytes.Buffer)

cmd.Execute(actual, "test")

assert.Equal(t, string(expectedBytes), actual.String())
}

func TestList(t *testing.T) {
resp, err := http.Get(`https://www.gitignore.io/api/list`)
require.NoError(t, err)

defer resp.Body.Close()

expected, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)

expectedS := bytes.Split(bytes.ReplaceAll(expected, []byte(","), []byte("\n")), []byte("\n"))

os.Args = []string{"gig", "-c", "640f03b1f9906c5dcb788d36ec5c1095264a10ae", "list"}

actual := new(bytes.Buffer)

cmd.Execute(actual, "test")

actualS := bytes.Split(bytes.ToLower(actual.Bytes()), []byte("\n"))
actualS = actualS[:len(actualS)-1]

assert.Equal(t, expectedS, actualS)
}

func TestVersion(t *testing.T) {
os.Args = []string{"gig", "version", "-c", "f0bddaeda3368130d52bde2b62a9df741f6117d4"}

actual := new(bytes.Buffer)

cmd.Execute(actual, "test")

expected := strings.Join([]string{
"gig version test",
fmt.Sprintf("Cached github.com/toptal/gitignore in: %s", filepath.Join(xdg.CacheHome(), `gig`)),
"Using github.com/toptal/gitignore commit hash: f0bddaeda3368130d52bde2b62a9df741f6117d4",
}, "\n") + "\n"

assert.Equal(t, expected, actual.String())
}

0 comments on commit d2d570e

Please sign in to comment.