From 6e1718af964943ce1e517313d7eebcfe223cbaf9 Mon Sep 17 00:00:00 2001 From: Shi Han NG Date: Tue, 10 Dec 2019 23:08:39 +0900 Subject: [PATCH] feat(version): add version subcommand --- .goreleaser.yml | 3 +- cmd/root.go | 7 ++-- cmd/version.go | 41 ++++++++++++++++++++++ main.go | 4 ++- main_integ_test.go | 78 ----------------------------------------- main_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 137 insertions(+), 83 deletions(-) create mode 100644 cmd/version.go delete mode 100644 main_integ_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 9fe5e0a..44c94c0 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,7 +9,8 @@ builds: - darwin - windows - linux - + ldflags: + - -s -w -X main.version={{.Version}} archives: - replacements: darwin: Darwin diff --git a/cmd/root.go b/cmd/root.go index 5339de9..57b5b18 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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 { diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..3db9141 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,41 @@ +/* +Copyright © 2019 Shi Han NG + +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) + }, + } +} diff --git a/main.go b/main.go index bc18ed8..0becdca 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,8 @@ import ( "github.com/shihanng/gig/cmd" ) +var version = "dev" + func main() { - cmd.Execute(os.Stdout) + cmd.Execute(os.Stdout, version) } diff --git a/main_integ_test.go b/main_integ_test.go deleted file mode 100644 index 27f9417..0000000 --- a/main_integ_test.go +++ /dev/null @@ -1,78 +0,0 @@ -// +build integration - -package main - -import ( - "bufio" - "bytes" - "io/ioutil" - "net/http" - "os" - "strings" - "testing" - - "github.com/shihanng/gig/cmd" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -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) - - 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) - - actualS := bytes.Split(bytes.ToLower(actual.Bytes()), []byte("\n")) - actualS = actualS[:len(actualS)-1] - - assert.Equal(t, expectedS, actualS) -} diff --git a/main_test.go b/main_test.go index 8cb8752..f860521 100644 --- a/main_test.go +++ b/main_test.go @@ -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" @@ -19,7 +27,7 @@ func TestCli(t *testing.T) { actual := new(bytes.Buffer) - cmd.Execute(actual) + cmd.Execute(actual, "test") goldenPath := `./testdata/cli.golden` @@ -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()) +}