Skip to content

Commit

Permalink
add latest tag
Browse files Browse the repository at this point in the history
Signed-off-by: Rafi <[email protected]>
  • Loading branch information
refoo0 committed Nov 26, 2024
1 parent b51ae2b commit 5714ede
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cmd/devguard-scanner/commands/intoto/intoto_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

toto "github.com/in-toto/in-toto-golang/in_toto"
"github.com/l3montree-dev/devguard/internal/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -47,6 +48,24 @@ func getTokenFromCommandOrKeyring(cmd *cobra.Command) (string, error) {

return token, nil
}

func parseGitIgnore(path string) ([]string, error) {
// read .gitignore if exists
content, err := os.ReadFile(path)
if err == nil {
ignorePaths := strings.Split(string(content), "\n")
// make sure to remove new lines and empty strings
ignorePaths = utils.Filter(
utils.Map(ignorePaths, strings.TrimSpace),
func(e string) bool {
return e != "" && e != "\n"
})
return ignorePaths, nil
}

return nil, err
}

func parseCommand(cmd *cobra.Command) (
step string, supplyChainId string, key toto.Key, materials, products, ignore []string, err error) {
token, err := getTokenFromCommandOrKeyring(cmd)
Expand All @@ -68,10 +87,12 @@ func parseCommand(cmd *cobra.Command) (
return "", "", toto.Key{}, nil, nil, nil, err
}

// read .gitignore if exists
content, err := os.ReadFile(".gitignore")
if err == nil {
ignore = append(ignore, strings.Split(string(content), "\n")...)
pathsFromGitIgnore, err := parseGitIgnore(".gitignore")
if err != nil {
// just swallow the error
slog.Warn("could not read .gitignore file. This is not to bad if you do not have a .gitignore file.", "error", err)
} else {
ignore = append(ignore, pathsFromGitIgnore...)
}

key, err = tokenToInTotoKey(token)
Expand Down
32 changes: 32 additions & 0 deletions cmd/devguard-scanner/commands/intoto/intoto_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package intotocmd

import (
"os"
"path"
"testing"

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

func TestParseGitIgnore(t *testing.T) {
t.Run("parseGitIgnore with empty strings", func(t *testing.T) {
// create temp dir for testing
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "failed to create temporary directory")

defer os.RemoveAll(dir)

// Create a temporary .gitignore file for testing
gitignoreContent := "\n.DS_Store\n\t\t\t\n"

filepath := path.Join(dir, ".gitignore")

err = os.WriteFile(filepath, []byte(gitignoreContent), 0644)
assert.NoError(t, err, "failed to create temporary .gitignore file")

ignorePaths, err := parseGitIgnore(filepath)
assert.NoError(t, err, "expected no error when reading .gitignore")
assert.Equal(t, []string{".DS_Store"}, ignorePaths, "unexpected ignore paths")

})
}

0 comments on commit 5714ede

Please sign in to comment.