Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
implement file ignore in the profile enable process
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwit committed Oct 27, 2019
1 parent 07f3d20 commit 6fd56e3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/wksctl/profile/constants/constants.go
Original file line number Diff line number Diff line change
@@ -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"
)
44 changes: 44 additions & 0 deletions cmd/wksctl/profile/enable/enable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package enable

import (
"bufio"
"errors"
"io"
"os"
"path"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
45 changes: 45 additions & 0 deletions cmd/wksctl/profile/enable/enable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package enable

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestProcessIgnoreFile(t *testing.T) {
fixture := `a
b/
#c
#d
e
f
../g
h..
`
r := strings.NewReader(fixture)
lines, err := processIgnoreFile("", r)
assert.NoError(t, err, "process ignore file should not be error")
assert.Equal(t, 6, len(lines), "ignore file entries should be 6")
assert.Equal(t, []string{"a", "b", "e", "f", "../g", "h.."}, lines)
}

func TestProcessIgnoreFileWithPrefix(t *testing.T) {
fixture := `a
b/
#c
#d
e
f
../g
h..
`
r := strings.NewReader(fixture)
lines, err := processIgnoreFile("profiles", r)
assert.NoError(t, err, "process 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)
}
13 changes: 11 additions & 2 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down

0 comments on commit 6fd56e3

Please sign in to comment.