Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --cache-path option #47

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ https://github.com/toptal/gitignore.git into $XDG_CACHE_HOME/gig.`,
func (c *command) genRunE(cmd *cobra.Command, args []string) error {
items := args

orders, err := order.ReadOrder(filepath.Join(c.templatePath, `templates`, `order`))
orders, err := order.ReadOrder(filepath.Join(c.templatePath(), `order`))
if err != nil {
return err
}
Expand All @@ -60,5 +60,5 @@ func (c *command) genRunE(cmd *cobra.Command, args []string) error {

defer wc.Close()

return file.Generate(wc, filepath.Join(c.templatePath, `templates`), items...)
return file.Generate(wc, c.templatePath(), items...)
}
3 changes: 1 addition & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package cmd

import (
"io"
"path/filepath"

"github.com/cockroachdb/errors"
"github.com/shihanng/gig/internal/file"
Expand All @@ -40,7 +39,7 @@ func newListCmd(c *command) *cobra.Command {
}

func (c *command) listRunE(cmd *cobra.Command, args []string) error {
templates, err := file.List(filepath.Join(c.templatePath, `templates`))
templates, err := file.List(c.templatePath())
if err != nil {
return err
}
Expand Down
29 changes: 19 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THE SOFTWARE.
package cmd

import (
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -35,16 +36,19 @@ import (

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

rootCmd := newRootCmd(command)

rootCmd.PersistentFlags().StringVarP(&command.commitHash, "commit-hash", "c", "",
"use templates from a specific commit hash of github.com/toptal/gitignore")

rootCmd.PersistentFlags().StringVarP(&command.cachePath, "cache-path", "", filepath.Join(xdg.CacheHome(), `gig`),
"location where the content of github.com/toptal/gitignore will be cached in")

genCmd := newGenCmd(command)

genCmd.Flags().BoolVarP(&command.genIsFile, "file", "f", false,
Expand All @@ -68,21 +72,21 @@ func newRootCmd(c *command) *cobra.Command {
Long: `gig is a command line tool to help you create useful .gitignore files
for your project. It is inspired by gitignore.io and make use of
the large collection of useful .gitignore templates of the web service.`,
PersistentPreRunE: c.RootRunE,
PersistentPreRunE: c.rootRunE,
}
}

type command struct {
output io.Writer
commitHash string
templatePath string
version string
output io.Writer
commitHash string
cachePath string
version string

genIsFile bool
}

func (c *command) RootRunE(cmd *cobra.Command, args []string) error {
r, err := repo.New(c.templatePath, repo.SourceRepo)
func (c *command) rootRunE(cmd *cobra.Command, args []string) error {
r, err := repo.New(c.cachePath, repo.SourceRepo)
if err != nil {
return err
}
Expand All @@ -109,3 +113,8 @@ func (c *command) newWriteCloser() (io.WriteCloser, error) {

return ioutil.WriteNopCloser(c.output), nil
}

func (c *command) templatePath() string {
fmt.Println(c.cachePath)
return filepath.Join(c.cachePath, `templates`)
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func newVersionCmd(c *command) *cobra.Command {

func (c *command) versionRunE(cmd *cobra.Command, args []string) {
fmt.Fprintf(c.output, "gig version %s\n", c.version)
fmt.Fprintf(c.output, "Cached github.com/toptal/gitignore in: %s\n", c.templatePath)
fmt.Fprintf(c.output, "Cached github.com/toptal/gitignore in: %s\n", c.cachePath)
fmt.Fprintf(c.output, "Using github.com/toptal/gitignore commit hash: %s\n", c.commitHash)
}
63 changes: 40 additions & 23 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@ import (
"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"
"github.com/stretchr/testify/suite"
)

var update = flag.Bool("update", false, "update .golden files")

func TestCli(t *testing.T) {
os.Args = []string{"gig", "gen", "go"}
type MainTestSuite struct {
suite.Suite
tempDir string
}

func (s *MainTestSuite) SetupSuite() {
dir, err := ioutil.TempDir("", "gig")
s.Require().NoError(err)
s.tempDir = dir
}

func (s *MainTestSuite) TearDownSuite() {
s.Require().NoError(os.RemoveAll(s.tempDir))
}

func (s *MainTestSuite) TestCli() {
os.Args = []string{"gig", "gen", "go", "--cache-path", s.tempDir}

actual := new(bytes.Buffer)

Expand All @@ -32,19 +44,19 @@ func TestCli(t *testing.T) {
goldenPath := `./testdata/cli.golden`

if *update {
require.NoError(t, ioutil.WriteFile(goldenPath, actual.Bytes(), 0644))
s.Require().NoError(ioutil.WriteFile(goldenPath, actual.Bytes(), 0644))
}

expected, err := ioutil.ReadFile(goldenPath)
require.NoError(t, err)
assert.Equal(t, expected, actual.Bytes())
s.Require().NoError(err)
s.Assert().Equal(expected, actual.Bytes())
}

func TestCheckGitIgnoreIO(t *testing.T) {
func (s *MainTestSuite) TestCheckGitIgnoreIO() {
// 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)
s.Require().NoError(err)

defer resp.Body.Close()

Expand All @@ -63,33 +75,34 @@ func TestCheckGitIgnoreIO(t *testing.T) {
}

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

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

os.Args = []string{"gig", "gen", "Django", "androidstudio", "java", "go", "ada", "zsh", "c", "gradle", "go"}
os.Args = []string{"gig", "--cache-path", s.tempDir, "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())
s.Assert().Equal(expectedBytes, actual.Bytes())
}

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

defer resp.Body.Close()

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

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

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

actual := new(bytes.Buffer)

Expand All @@ -98,21 +111,25 @@ func TestList(t *testing.T) {
actualS := bytes.Split(bytes.ToLower(actual.Bytes()), []byte("\n"))
actualS = actualS[:len(actualS)-1]

assert.Equal(t, expectedS, actualS)
s.Assert().Equal(expectedS, actualS)
}

func TestVersion(t *testing.T) {
os.Args = []string{"gig", "version", "-c", "f0bddaeda3368130d52bde2b62a9df741f6117d4"}
func (s *MainTestSuite) TestVersion() {
os.Args = []string{"gig", "--cache-path", s.tempDir, "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`)),
fmt.Sprintf("Cached github.com/toptal/gitignore in: %s", s.tempDir),
"Using github.com/toptal/gitignore commit hash: f0bddaeda3368130d52bde2b62a9df741f6117d4",
}, "\n") + "\n"

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

func TestMainTestSuite(t *testing.T) {
suite.Run(t, new(MainTestSuite))
}