Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cookbook Uploads #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ package main

import (
"fmt"
"io/ioutil"
"os"

"github.com/go-chef/chef"
)

func main() {
// read a client key
key, err := ioutil.ReadFile("key.pem")
key, err := os.ReadFile("key.pem")
if err != nil {
fmt.Println("Couldn't read key.pem:", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestAssociationMethods(t *testing.T) {
t.Errorf("Associations.List returned error: %v", err)
}
listWant := []Invite{
Invite{Id: "1f", UserName: "jollystranger"},
Invite{Id: "2b", UserName: "fredhamlet"},
{Id: "1f", UserName: "jollystranger"},
{Id: "2b", UserName: "fredhamlet"},
}
if !reflect.DeepEqual(invites, listWant) {
t.Errorf("Associations.InviteList returned %+v, want %+v", invites, listWant)
Expand Down
72 changes: 72 additions & 0 deletions chefignore.go
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
}
135 changes: 135 additions & 0 deletions chefignore_test.go
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))
}
}
4 changes: 2 additions & 2 deletions config_rb_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chef

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func configKeyParser(s []string, path string, c *ConfigRb) error {
size := len(data)
if size > 0 {
keyPath := filepath.Join(path, data[size-1])
keyData, err := ioutil.ReadFile(keyPath)
keyData, err := os.ReadFile(keyPath)
if err != nil {
return err
}
Expand Down
Loading