forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for cookbook uploads (V0 and V2). * Add support for chefignore files * Improve metadata.rb parsing to conform with Chef API * Add ServerApiVersion customization to http * Minor linting changes for newer golang versions Resolves go-chef#250
- Loading branch information
Showing
56 changed files
with
2,055 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package chef | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Matches all lines that only contain comments or regexp | ||
const commentsAndWhitespaceRegex = `^\s*(?:#.*)?$` | ||
|
||
// chefignore object | ||
type Chefignore struct { | ||
Path string | ||
Ignores []string | ||
} | ||
|
||
// Creates a new Chefignore from a path | ||
func NewChefignore(path string) Chefignore { | ||
chefignore := Chefignore{ | ||
Path: path, | ||
Ignores: make([]string, 0), | ||
} | ||
|
||
// Parse ignored lines, respecting comments | ||
chefignore.Ignores = parseChefignoreContent(path) | ||
return chefignore | ||
} | ||
|
||
// Returns true if the given path should be ignored, false otherwise | ||
func (chefignore *Chefignore) Ignore(path string) bool { | ||
for _, ignorePattern := range chefignore.Ignores { | ||
// TODO: Handle antd style wildcards (**) since these are supported in the spec | ||
// but not by filepath.Match | ||
matched, err := filepath.Match(ignorePattern, path) | ||
if err != nil { | ||
// Skip malformed patterns | ||
continue | ||
} | ||
|
||
if matched { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Parses .chefignore content | ||
func parseChefignoreContent(path string) []string { | ||
skipLineRegex := regexp.MustCompile(commentsAndWhitespaceRegex) | ||
|
||
// Read content line-by-line | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return []string{} | ||
} | ||
defer file.Close() | ||
|
||
ignores := make([]string, 0) | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if !skipLineRegex.MatchString(line) { | ||
ignores = append(ignores, strings.TrimSpace(line)) | ||
} | ||
} | ||
|
||
return ignores | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package chef_test | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-chef/chef" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
testChefignore = "test/chefignore" | ||
expectedIgnores = []string{ | ||
".DS_Store", | ||
"ehthumbs.db", | ||
"Icon?", | ||
"nohup.out", | ||
"Thumbs.db", | ||
".envrc", | ||
".#*", | ||
".project", | ||
".settings", | ||
"*_flymake", | ||
"*_flymake.*", | ||
"*.bak", | ||
"*.sw[a-z]", | ||
"*.tmproj", | ||
"*~", | ||
"\\#*", | ||
"REVISION", | ||
"TAGS*", | ||
"tmtags", | ||
".vscode", | ||
".editorconfig", | ||
"*.class", | ||
"*.com", | ||
"*.dll", | ||
"*.exe", | ||
"*.o", | ||
"*.pyc", | ||
"*.so", | ||
"*/rdoc/", | ||
"a.out", | ||
"mkmf.log", | ||
".circleci/*", | ||
".codeclimate.yml", | ||
".delivery/*", | ||
".foodcritic", | ||
".kitchen*", | ||
".mdlrc", | ||
".overcommit.yml", | ||
".rspec", | ||
".rubocop.yml", | ||
".travis.yml", | ||
".watchr", | ||
".yamllint", | ||
"azure-pipelines.yml", | ||
"Dangerfile", | ||
"examples/*", | ||
"features/*", | ||
"Guardfile", | ||
"kitchen.yml*", | ||
"mlc_config.json", | ||
"Procfile", | ||
"Rakefile", | ||
"spec/*", | ||
"test/*", | ||
".git", | ||
".gitattributes", | ||
".gitconfig", | ||
".github/*", | ||
".gitignore", | ||
".gitkeep", | ||
".gitmodules", | ||
".svn", | ||
"*/.bzr/*", | ||
"*/.git", | ||
"*/.hg/*", | ||
"*/.svn/*", | ||
"Berksfile", | ||
"Berksfile.lock", | ||
"cookbooks/*", | ||
"tmp", | ||
"vendor/*", | ||
"Gemfile", | ||
"Gemfile.lock", | ||
"Policyfile.rb", | ||
"Policyfile.lock.json", | ||
"CODE_OF_CONDUCT*", | ||
"CONTRIBUTING*", | ||
"documentation/*", | ||
"TESTING*", | ||
"UPGRADING*", | ||
".vagrant", | ||
"Vagrantfile", | ||
} | ||
unignoredPaths = []string{ | ||
"metadata.rb", | ||
"metadata.json", | ||
"recipes/default.rb", | ||
"attributes/default.rb", | ||
"resources/custom.rb", | ||
"providers/custom.rb", | ||
"templates/default/temp.txt.erb", | ||
} | ||
) | ||
|
||
func TestChefignoreNew(t *testing.T) { | ||
c := chef.NewChefignore(testChefignore) | ||
|
||
assert.Equal(t, testChefignore, c.Path) | ||
assert.Equal(t, expectedIgnores, c.Ignores) | ||
} | ||
|
||
func TestChefignoreIgnore(t *testing.T) { | ||
c := chef.NewChefignore(testChefignore) | ||
|
||
// Ensure appropriate files will be ignored | ||
for _, ignore := range c.Ignores { | ||
ignoreStr := strings.ReplaceAll(ignore, "*", "test") | ||
|
||
// Replace contents of [*] matchers | ||
re := regexp.MustCompile(`\[.*\]`) | ||
ignoreStr = re.ReplaceAllString(ignoreStr, "a") | ||
|
||
// Handle \# | ||
ignoreStr = strings.ReplaceAll(ignoreStr, "\\#", "#") | ||
assert.Equal(t, true, c.Ignore(ignoreStr), "Did not ignore ", ignoreStr, " as expected") | ||
} | ||
|
||
for _, path := range unignoredPaths { | ||
assert.Equal(t, false, c.Ignore(path)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.