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

Commit

Permalink
Merge pull request #104 from weaveworks/support_ignore_file
Browse files Browse the repository at this point in the history
implement file ignore in the profile enable process
  • Loading branch information
chanwit committed Oct 29, 2019
2 parents 07f3d20 + 6d0d192 commit dbf21d2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/wksctl/profile/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package constants
const (
AppDevAlias = "app-dev"
AppDevRepoURL = "git@github.com:weaveworks/eks-quickstart-app-dev"

// WKSctlIgnoreFilename is the ignore file recognized by wksctl
WKSctlIgnoreFilename = ".wksctlignore"
)
60 changes: 58 additions & 2 deletions cmd/wksctl/profile/enable/enable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package enable

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path"
"regexp"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,15 +74,20 @@ func profileEnableRun(params profileEnableFlags) error {
if err != nil {
return err
}
clonePath := path.Join(params.profileDir, hostName, repoName)
profilePath := path.Join(params.profileDir, hostName, repoName)

log.Info("Adding the profile to the local repository...")
err = git.SubtreeAdd(clonePath, repoURL, params.revision)
err = git.SubtreeAdd(profilePath, repoURL, params.revision)
if err != nil {
return err
}
log.Info("Added the profile to the local repository.")

// Detect and process the ignore file if found at the top most directory of the profile
if err := doIgnoreFiles(profilePath); err != nil {
return err
}

// The default behaviour is auto-commit and push
if params.push {
if err := git.Push(); err != nil {
Expand All @@ -87,3 +97,49 @@ func profileEnableRun(params profileEnableFlags) error {

return nil
}

func doIgnoreFiles(profilePath string) error {
ignoreFilePath := path.Join(profilePath, constants.WKSctlIgnoreFilename)
if _, err := os.Stat(ignoreFilePath); err == nil {
log.Infof("Ignoring files declared in %s...", constants.WKSctlIgnoreFilename)
file, err := os.Open(ignoreFilePath)
if err != nil {
return err
}
defer file.Close()
pathsToIgnores, err := parseDotIgnorefile(profilePath, file)
if err != nil {
return err
}
if err := removePathsFromGit(pathsToIgnores...); err != nil {
return err
}
log.Info("Ignored files successfully.")
}
return nil
}

func parseDotIgnorefile(dir string, reader io.Reader) ([]string, error) {
result := []string{}
scanner := bufio.NewScanner(reader)
re := regexp.MustCompile(`(?ms)^\s*(?P<pathToIgnore>[^\s#]+).*$`)
for scanner.Scan() {
groups := re.FindStringSubmatch(scanner.Text())
if len(groups) != 2 {
continue
}
pathToIgnore := groups[1]
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(fmt.Sprintf("Ignored files declared in %s", constants.WKSctlIgnoreFilename)); 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"
)

const (
ignoreFileFixture = `a
b/
#c
#d
e
f
../g
h..
i # this is i
j/# this is j
`
)

func TestParseDotIgnoreFile(t *testing.T) {
r := strings.NewReader(ignoreFileFixture)
lines, err := parseDotIgnorefile("", r)
assert.NoError(t, err, "parsing ignore file should not be error")
assert.Equal(t, 8, len(lines), "ignore file entries should be 8")
// Entry: 'b/' is resolved to 'b' by path.Join()
assert.Equal(t, []string{"a", "b", "e", "f", "../g", "h..", "i", "j"}, lines)
}

func TestParseDotIgnoreFileWithPrefix(t *testing.T) {
r := strings.NewReader(ignoreFileFixture)
lines, err := parseDotIgnorefile("profiles", r)
assert.NoError(t, err, "parsing ignore file should not be error")
assert.Equal(t, 8, len(lines), "ignore file entries should be 8")
// 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..",
"profiles/i", "profiles/j"}, lines)
}
4 changes: 2 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 Down

0 comments on commit dbf21d2

Please sign in to comment.