-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement network selector similar to cataloger selector
Signed-off-by: Keith Zantow <[email protected]>
- Loading branch information
Showing
11 changed files
with
185 additions
and
42 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
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
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,98 @@ | ||
package options | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/anchore/clio" | ||
"github.com/anchore/fangs" | ||
"github.com/anchore/syft/internal/log" | ||
) | ||
|
||
type Network struct { | ||
Directives []string `yaml:"network" json:"network" mapstructure:"network"` | ||
} | ||
|
||
func (n *Network) PostLoad() error { | ||
n.Directives = flatten(n.Directives) | ||
return nil | ||
} | ||
|
||
func (n *Network) AddFlags(flags clio.FlagSet) { | ||
flags.StringArrayVarP(&n.Directives, "network", "", | ||
"use the network to fetch and augment package information") | ||
|
||
// if pfp, ok := flags.(fangs.PFlagSetProvider); ok { | ||
// flagSet := pfp.PFlagSet() | ||
// flag := flagSet.Lookup("network") | ||
// flag.NoOptDefVal = "all" | ||
//} | ||
} | ||
|
||
func (n *Network) Enabled(features ...string) *bool { | ||
return networkEnabled(n.Directives, features...) | ||
} | ||
|
||
var _ interface { | ||
fangs.PostLoader | ||
fangs.FlagAdder | ||
} = (*Network)(nil) | ||
|
||
func networkEnabled(networkDirectives []string, features ...string) *bool { | ||
if len(networkDirectives) == 0 { | ||
return nil | ||
} | ||
|
||
enabled := func(features ...string) *bool { | ||
for _, directive := range networkDirectives { | ||
enable := true | ||
directive = strings.TrimPrefix(directive, "+") // +java and java are equivalent | ||
if strings.HasPrefix(directive, "-") { | ||
directive = directive[1:] | ||
enable = false | ||
} | ||
for _, feature := range features { | ||
if directive == feature { | ||
return &enable | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
enableAll := enabled("all", "yes", "on", "enable", "enabled") | ||
disableAll := enabled("none", "no", "off", "disable", "disabled") | ||
|
||
if disableAll != nil { | ||
if enableAll != nil { | ||
log.Warn("you have specified to both enable and disable all network functionality, defaulting to disabled") | ||
} else { | ||
enableAll = ptr(!*disableAll) | ||
} | ||
} | ||
|
||
// check for explicit enable/disable of each particular feature, in order | ||
for _, feat := range features { | ||
enableFeature := enabled(feat) | ||
if enableFeature != nil { | ||
return enableFeature | ||
} | ||
} | ||
|
||
return enableAll | ||
} | ||
|
||
func ptr[T any](val T) *T { | ||
return &val | ||
} | ||
|
||
func flatten(commaSeparatedEntries []string) []string { | ||
var out []string | ||
for _, v := range commaSeparatedEntries { | ||
for _, s := range strings.Split(v, ",") { | ||
out = append(out, strings.TrimSpace(s)) | ||
} | ||
} | ||
sort.Strings(out) | ||
return out | ||
} |
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,58 @@ | ||
package options | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_networkEnabled(t *testing.T) { | ||
tests := []struct { | ||
directives string | ||
test string | ||
expected *bool | ||
}{ | ||
{ | ||
directives: "", | ||
test: "java", | ||
expected: nil, | ||
}, | ||
{ | ||
directives: "none", | ||
test: "java", | ||
expected: ptr(false), | ||
}, | ||
{ | ||
directives: "none,+java", | ||
test: "java", | ||
expected: ptr(true), | ||
}, | ||
{ | ||
directives: "all", | ||
test: "java", | ||
expected: ptr(true), | ||
}, | ||
{ | ||
directives: "on", | ||
test: "java", | ||
expected: ptr(true), | ||
}, | ||
{ | ||
directives: "on,-java", | ||
test: "java", | ||
expected: ptr(false), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.directives, func(t *testing.T) { | ||
n := Network{ | ||
Directives: []string{test.directives}, | ||
} | ||
require.NoError(t, n.PostLoad()) | ||
|
||
got := n.Enabled(test.test) | ||
require.Equal(t, test.expected, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.