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

implement file ignore in the profile enable process #104

Merged
merged 1 commit into from
Oct 29, 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
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels to me that wksctl in wksctlignore refers to our tool in an overly specific way. It is not impossible that we have another tool (other than wksctl) in the future that deals with profiles, for example.

Unless we're already committed to calling this .wksctlignore, I suggest renaming to .profileignore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mflendrich basically yes, it is very specific to wksctl. Let me explain by referring to this example profile:
https://github.com/chanwit/eks-quickstart-app-dev

In the above profile, there are two ignore files .eksctlignore and .wksctlignore.
Each of them contains a set of file needs to be removed by each tool.

This is by design to make a single profile repo works on both eksctl and wksctl.
Thinking of this as a situation that we have .circleci, .travis.yaml and .drone.yaml files on a git repo to allow each CI to independently process our repo (in its own way).

chanwit marked this conversation as resolved.
Show resolved Hide resolved
)
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)
chanwit marked this conversation as resolved.
Show resolved Hide resolved
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