From 20672570e6d421287559ed1fa6c52f23b3202562 Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Fri, 25 Oct 2019 16:24:51 +0700 Subject: [PATCH] implement file ignore in the profile enable process --- cmd/wksctl/profile/constants/constants.go | 5 +-- cmd/wksctl/profile/enable/enable.go | 44 +++++++++++++++++++++++ cmd/wksctl/profile/enable/enable_test.go | 40 +++++++++++++++++++++ pkg/git/git.go | 13 +++++-- 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 cmd/wksctl/profile/enable/enable_test.go diff --git a/cmd/wksctl/profile/constants/constants.go b/cmd/wksctl/profile/constants/constants.go index 2c544873..848385a5 100644 --- a/cmd/wksctl/profile/constants/constants.go +++ b/cmd/wksctl/profile/constants/constants.go @@ -1,6 +1,7 @@ package constants const ( - AppDevAlias = "app-dev" - AppDevRepoURL = "git@github.com:weaveworks/eks-quickstart-app-dev" + AppDevAlias = "app-dev" + AppDevRepoURL = "git@github.com:weaveworks/eks-quickstart-app-dev" + WKSctlIgnoreFilename = ".wksctlignore" ) diff --git a/cmd/wksctl/profile/enable/enable.go b/cmd/wksctl/profile/enable/enable.go index 109226dd..7003b67d 100644 --- a/cmd/wksctl/profile/enable/enable.go +++ b/cmd/wksctl/profile/enable/enable.go @@ -1,8 +1,12 @@ package enable import ( + "bufio" "errors" + "io" + "os" "path" + "strings" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -78,6 +82,23 @@ func profileEnableRun(params profileEnableFlags) error { } log.Info("Added the profile to the local repository.") + ignoreFilePath := path.Join(clonePath, constants.WKSctlIgnoreFilename) + if _, err := os.Stat(ignoreFilePath); err == nil { + log.Info("Ignoring files declared in .wksctlignore...") + file, err := os.Open(ignoreFilePath) + if err != nil { + return err + } + pathsToIgnores, err := processIgnoreFile(clonePath, file) + if err != nil { + return err + } + if err := removePathsFromGit(pathsToIgnores...); err != nil { + return err + } + log.Info("Ignored files successfully.") + } + // The default behaviour is auto-commit and push if params.push { if err := git.Push(); err != nil { @@ -87,3 +108,26 @@ func profileEnableRun(params profileEnableFlags) error { return nil } + +func processIgnoreFile(dir string, reader io.Reader) ([]string, error) { + result := []string{} + scanner := bufio.NewScanner(reader) + for scanner.Scan() { + pathToIgnore := strings.Trim(scanner.Text(), " ") + if len(pathToIgnore) == 0 || strings.HasPrefix(pathToIgnore, "#") { + continue + } + result = append(result, path.Join(dir, pathToIgnore)) + } + return result, nil +} + +func removePathsFromGit(paths ...string) error { + if err := git.RmRecursive(paths...); err != nil { + return err + } + if err := git.Commit("Ignored files declared in .wksctlignore"); err != nil { + return err + } + return nil +} diff --git a/cmd/wksctl/profile/enable/enable_test.go b/cmd/wksctl/profile/enable/enable_test.go new file mode 100644 index 00000000..626d86e1 --- /dev/null +++ b/cmd/wksctl/profile/enable/enable_test.go @@ -0,0 +1,40 @@ +package enable + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + ignoreFileFixture = `a +b/ + #c +#d +e +f +../g +h.. +` +) + +func TestProcessIgnoreFile(t *testing.T) { + r := strings.NewReader(ignoreFileFixture) + lines, err := processIgnoreFile("", r) + assert.NoError(t, err, "processing ignore file should not be error") + assert.Equal(t, 6, len(lines), "ignore file entries should be 6") + // b/ is resolved to b by path.Join() + assert.Equal(t, []string{"a", "b", "e", "f", "../g", "h.."}, lines) +} + +func TestProcessIgnoreFileWithPrefix(t *testing.T) { + r := strings.NewReader(ignoreFileFixture) + lines, err := processIgnoreFile("profiles", r) + assert.NoError(t, err, "processing ignore file should not be error") + assert.Equal(t, 6, len(lines), "ignore file entries should be 6") + // Note: + // - profiles/b/ is resolved to profiles/b + // - profiles/../g is resolved to g + assert.Equal(t, []string{"profiles/a", "profiles/b", "profiles/e", "profiles/f", "g", "profiles/h.."}, lines) +} diff --git a/pkg/git/git.go b/pkg/git/git.go index 3b4b3acc..009c75e9 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -22,8 +22,8 @@ func HasNoStagedChanges() error { return errors.Wrap(gitExec("diff", "--staged", "--exit-code"), "repository contains staged changes") } -func RmRecursive(path string) error { - return gitExec("rm", "-r", "--", path) +func RmRecursive(paths ...string) error { + return gitExec(append([]string{"rm", "-r", "--"}, paths...)...) } func AddAll(path string) error { @@ -43,6 +43,15 @@ func Commit(message string) error { return err } +func CommitAmend() error { + log.Infof("Amending the changes...") + err := gitExec("commit", "--amend", "--no-edit") + if err == nil { + log.Info("Amended the change to the previous commit.") + } + return err +} + func Push() error { log.Info("Pushing to the remote...") err := gitExec("push", "-v", "origin", "HEAD")