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

feat(sggolicenses): add ignored dependencies #545

Closed
wants to merge 1 commit into from
Closed
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
91 changes: 76 additions & 15 deletions tools/sggolicenses/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func Command(ctx context.Context, args ...string) *exec.Cmd {
return sg.Command(ctx, sg.FromBinDir(name), args...)
}

// Check for disallowed types of Go licenses in a specific directory.
// By default, Google's forbidden and restricted types are disallowed.
func CheckDir(ctx context.Context, directory string, disallowedTypes ...string) error {
func checkDir(ctx context.Context, directory string, appendArgs []string) error {
args := []string{
"check",
".",
Expand All @@ -37,11 +35,9 @@ func CheckDir(ctx context.Context, directory string, disallowedTypes ...string)
"--ignore",
"go.einride.tech",
}
if len(disallowedTypes) > 0 {
args = append(args, "--disallowed_types="+strings.Join(disallowedTypes, ","))
} else {
args = append(args, "--disallowed_types=forbidden,restricted")
}

args = append(args, appendArgs...)

cmd := Command(ctx, args...)
cmd.Dir = directory
// go-licenses tries to exclude standard library packages by checking if they are prefixed
Expand All @@ -57,9 +53,45 @@ func CheckDir(ctx context.Context, directory string, disallowedTypes ...string)
return cmd.Run()
}

// Check for disallowed types of Go licenses.
// Check for disallowed types of Go licenses in a specific directory.
// By default, Google's forbidden and restricted types are disallowed.
func Check(ctx context.Context, disallowedTypes ...string) error {
func CheckDir(ctx context.Context, directory string, disallowedTypes ...string) error {
var appendArgs []string
if len(disallowedTypes) > 0 {
appendArgs = append(appendArgs, "--disallowed_types="+strings.Join(disallowedTypes, ","))
} else {
appendArgs = append(appendArgs, "--disallowed_types=forbidden,restricted")
}

return checkDir(ctx, directory, appendArgs)
}

// Check for disallowed types of Go licenses in a specific directory.
// By default, Google's forbidden and restricted types are disallowed.
// Allows to ignore some dependencies by specifying their package path (ie "github.com/einride/sage").
func CheckDirWithIgnored(
Copy link
Member

Choose a reason for hiding this comment

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

with this many helpers it might be better to inline it in the specific repos where it's used - the code here is just meant to be the "most common default" use case - not all possible use cases

Copy link
Member Author

Choose a reason for hiding this comment

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

will close for now, the problem in the repo has been solved in another way

ctx context.Context,
directory string,
ignoredPackages []string,
disallowedTypes ...string,
) error {
appendArgs := make([]string, 0, len(ignoredPackages)+1)

for _, ignored := range ignoredPackages {
appendArgs = append(appendArgs, "--ignore")
appendArgs = append(appendArgs, ignored)
}

if len(disallowedTypes) > 0 {
appendArgs = append(appendArgs, "--disallowed_types="+strings.Join(disallowedTypes, ","))
} else {
appendArgs = append(appendArgs, "--disallowed_types=forbidden,restricted")
}

return checkDir(ctx, directory, appendArgs)
}

func check(ctx context.Context, appendArgs []string) error {
var commands []*exec.Cmd
if err := filepath.WalkDir(sg.FromGitRoot(), func(path string, d fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -81,11 +113,8 @@ func Check(ctx context.Context, disallowedTypes ...string) error {
"--ignore",
"go.einride.tech",
}
if len(disallowedTypes) > 0 {
args = append(args, "--disallowed_types="+strings.Join(disallowedTypes, ","))
} else {
args = append(args, "--disallowed_types=forbidden,restricted")
}

args = append(args, appendArgs...)
cmd := Command(ctx, args...)
cmd.Dir = filepath.Dir(path)
// go-licenses tries to exclude standard library packages by checking if they are prefixed
Expand All @@ -111,6 +140,38 @@ func Check(ctx context.Context, disallowedTypes ...string) error {
return nil
}

// Check for disallowed types of Go licenses.
// By default, Google's forbidden and restricted types are disallowed.
func Check(ctx context.Context, disallowedTypes ...string) error {
var appendArgs []string
if len(disallowedTypes) > 0 {
appendArgs = append(appendArgs, "--disallowed_types="+strings.Join(disallowedTypes, ","))
} else {
appendArgs = append(appendArgs, "--disallowed_types=forbidden,restricted")
}

return check(ctx, appendArgs)
}

// Check for disallowed types of Go licenses.
// By default, Google's forbidden and restricted types are disallowed.
// Allows to ignore some dependencies by specifying their package path (ie "github.com/einride/sage").
func CheckWithIgnored(ctx context.Context, ignoredPackages []string, disallowedTypes ...string) error {
appendArgs := make([]string, 0, len(ignoredPackages)+1)
for _, ignored := range ignoredPackages {
appendArgs = append(appendArgs, "--ignore")
appendArgs = append(appendArgs, ignored)
}

if len(disallowedTypes) > 0 {
appendArgs = append(appendArgs, "--disallowed_types="+strings.Join(disallowedTypes, ","))
} else {
appendArgs = append(appendArgs, "--disallowed_types=forbidden,restricted")
}

return check(ctx, appendArgs)
}

func PrepareCommand(ctx context.Context) error {
_, err := sgtool.GoInstall(ctx, "github.com/google/go-licenses", version)
return err
Expand Down