Skip to content

Commit

Permalink
feat(store)!:type renames; and add GetThirdParytCL (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 8, 2023
1 parent 6923c30 commit dea7ec4
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 33 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"gradientf",
"gradientsf",
"ineffassign",
"infex",
"Infexion",
"ipaddress",
"ipmask",
Expand Down
12 changes: 6 additions & 6 deletions public-cobrass-clif-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ type (
// using the value's String() method.
ThirdPartyOptionValue = clif.ThirdPartyOptionValue

// SpecifiedFlagsCollection represents the set of third party flags
// ChangedFlagsMap represents the set of third party flags
// presented by the user on the command line.
// (NB: Cobra does not currently have a mechanism to collect third
// party flags, by convention, anything that follows " -- "), therefore
// we need to collect and handle these flags/options explicitly,
// which is less than ideal.
// A difference between SpecifiedFlagsCollection and ThirdPartyCommandLine
// is that switch flags have a true/false option value in SpecifiedFlagsCollection
// A difference between ChangedFlagsMap and ThirdPartyCommandLine
// is that switch flags have a true/false option value in ChangedFlagsMap
// but not in ThirdPartyCommandLine.
SpecifiedFlagsCollection = clif.SpecifiedFlagsCollection
ChangedFlagsMap = clif.ChangedFlagsMap

// ThirdPartyPresentFlags (see SpecifiedFlagsCollection)
ThirdPartyPresentFlags = clif.ThirdPartyPresentFlags
// ThirdPartyChangedFlags (see ChangedFlagsMap)
ThirdPartyChangedFlags = clif.ThirdPartyChangedFlags

// KnownByCollection collection maps a full flag name to the
// short name it is also known by. If a flag does not
Expand Down
34 changes: 34 additions & 0 deletions src/assistant/general-helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package assistant

import (
"fmt"

"github.com/snivilised/cobrass"
"github.com/spf13/pflag"
)

// GetThirdPartyCL creates a command line from the flag set. If there are any
// short form flags, they are resolved using the knownBy map, which the client
// provides, mapping long form flag names to their short form. The client can
// choose to compose a command line consisting of all available flags or just
// the ones changed by the user (ie, they are explicitly specified on the
// command line as opposed to be defaulted).
func GetThirdPartyCL(
flagSet *pflag.FlagSet,
knownBy cobrass.KnownByCollection,
) cobrass.ThirdPartyCommandLine {
// move to cobrass/clif
//
cl := cobrass.ThirdPartyCommandLine{}

flagSet.Visit(func(f *pflag.Flag) {
if _, found := knownBy[f.Name]; found {
cl = append(cl, fmt.Sprintf("--%v", f.Name))
if f.Value.Type() != "bool" {
cl = append(cl, f.Value.String())
}
}
})

return cl
}
115 changes: 115 additions & 0 deletions src/assistant/get-third-party-cl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package assistant_test

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"

"github.com/snivilised/cobrass"
"github.com/snivilised/cobrass/src/assistant"
"github.com/snivilised/cobrass/src/assistant/i18n"
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/cobrass/src/internal/helpers"
"github.com/snivilised/cobrass/src/store"
xi18n "github.com/snivilised/extendio/i18n"
)

type baseTE struct {
given string
should string
}

type helperTS struct {
baseTE
args []string
knownBy cobrass.KnownByCollection
expected clif.ThirdPartyCommandLine
}

var _ = Describe("GetThirdPartyCL", Ordered, func() {
var (
repo string
l10nPath string

from xi18n.LoadFrom
rootCommand *cobra.Command

paramSet *assistant.ParamSet[store.ProfileParameterSet]
knownBy cobrass.KnownByCollection
)

BeforeAll(func() {
repo = helpers.Repo("../..")
l10nPath = helpers.Path(repo, "Test/data/l10n")

from = xi18n.LoadFrom{
Path: l10nPath,
Sources: xi18n.TranslationFiles{
i18n.CobrassSourceID: xi18n.TranslationSource{Name: "test"},
},
}

if err := xi18n.Use(func(o *xi18n.UseOptions) {
o.From = from
}); err != nil {
Fail(err.Error())
}

knownBy = cobrass.KnownByCollection{
"profile": "P",
"scheme": "S",
}
})

DescribeTable("GetThirdPartyCL",
func(entry *helperTS) {
entry.knownBy = knownBy
args := entry.args

rootCommand = &cobra.Command{
Use: "scorch",
Short: "scotch",
Long: "scorch is a fake test command which contains filtering capabilities",
RunE: func(cmd *cobra.Command, _ []string) error {
flagSet := cmd.Flags()
actual := assistant.GetThirdPartyCL(flagSet, entry.knownBy)

Expect(actual).To(ContainElements(entry.expected))

return nil
},
}
paramSet = assistant.NewParamSet[store.ProfileParameterSet](rootCommand)
paramSet.Native.BindAll(paramSet, rootCommand.PersistentFlags())

_, err := helpers.ExecuteCommand(
rootCommand, args...,
)

Expect(err).Error().To(BeNil())
},
func(entry *helperTS) string {
return fmt.Sprintf("given: '%v', 🧪 should: '%v'", entry.given, entry.should)
},

Entry(nil, &helperTS{
baseTE: baseTE{
given: "flags in long form",
should: "return a command line containing long form flags",
},
args: []string{"/usr/fuse/home/music", "--profile", "blur", "--scheme", "ectoplasm"},
expected: clif.ThirdPartyCommandLine{"--profile", "blur", "--scheme", "ectoplasm"},
}),

Entry(nil, &helperTS{
baseTE: baseTE{
given: "flags in short form",
should: "return a command line containing long form flags",
},
args: []string{"/usr/fuse/home/music", "-P", "blur", "-S", "ectoplasm"},
expected: clif.ThirdPartyCommandLine{"--profile", "blur", "--scheme", "ectoplasm"},
}),
)
})
6 changes: 3 additions & 3 deletions src/clif/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
bare string
optionValue string
existingCL ThirdPartyCommandLine
presentFlags SpecifiedFlagsCollection
presentFlags ChangedFlagsMap
knownBy KnownByCollection
}

Expand Down Expand Up @@ -99,7 +99,7 @@ func notInPresent(input *tokenInput) *handleTokenResult {
// form; eg a flag may be in its short from in specified but in long form
// in secondary. This is resolved by the knownBy set. The specified set
// contains flags in their bare long form.
func Evaluate(presentFlags SpecifiedFlagsCollection,
func Evaluate(presentFlags ChangedFlagsMap,
knownBy KnownByCollection,
secondaryCL ThirdPartyCommandLine,
) ThirdPartyCommandLine {
Expand Down Expand Up @@ -168,7 +168,7 @@ func split(token string) (string, string) { //nolint:gocritic // pedant
return lead, bare
}

func spreadFlags(presentFlags SpecifiedFlagsCollection) ThirdPartyCommandLine {
func spreadFlags(presentFlags ChangedFlagsMap) ThirdPartyCommandLine {
commandLine := ThirdPartyCommandLine{}

for _, flag := range presentFlags.Keys() {
Expand Down
36 changes: 18 additions & 18 deletions src/clif/evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type evaluateTE struct {
baseTE
specified clif.SpecifiedFlagsCollection
specified clif.ChangedFlagsMap
secondary clif.ThirdPartyCommandLine
}

Expand Down Expand Up @@ -48,7 +48,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified",
expected: []string{"--dry-run"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
},
secondary: clif.ThirdPartyCommandLine{},
Expand All @@ -60,7 +60,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified",
expected: []string{"--sampling-factor", "4:2:0"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{},
Expand All @@ -72,7 +72,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified",
expected: []string{"--sampling-factor", "4:2:0"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{},
Expand All @@ -84,7 +84,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "all specified",
expected: []string{"--dry-run", "--sampling-factor", "4:2:0"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
"sampling-factor": "4:2:0",
},
Expand All @@ -102,7 +102,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, ignore secondary",
expected: []string{"--dry-run"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
},
secondary: clif.ThirdPartyCommandLine{"--dry-run"},
Expand All @@ -114,7 +114,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, ignore secondary",
expected: []string{"--dry-run"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
},
secondary: clif.ThirdPartyCommandLine{"-D"},
Expand All @@ -126,7 +126,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified with secondary",
expected: []string{"--dry-run", "--strip"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
},
secondary: clif.ThirdPartyCommandLine{"--strip"},
Expand All @@ -138,7 +138,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified with secondary",
expected: []string{"--dry-run", "-s"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
},
secondary: clif.ThirdPartyCommandLine{"-s"},
Expand All @@ -156,7 +156,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, ignore secondary",
expected: []string{"--sampling-factor", "4:2:0"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{"--sampling-factor", "2x1"},
Expand All @@ -168,7 +168,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, ignore secondary",
expected: []string{"--sampling-factor", "4:2:0"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{"-f", "2x1"},
Expand All @@ -180,7 +180,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified with secondary",
expected: []string{"--sampling-factor", "4:2:0", "--gaussian-blur", "0.05"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{"--gaussian-blur", "0.05"},
Expand All @@ -192,7 +192,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified with secondary",
expected: []string{"--sampling-factor", "4:2:0", "-b", "0.05"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{"-b", "0.05"},
Expand All @@ -208,7 +208,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, ignore secondary",
expected: []string{"--dry-run", "--sampling-factor", "4:2:0"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
"sampling-factor": "4:2:0",
},
Expand All @@ -221,7 +221,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, with secondary flag",
expected: []string{"--dry-run", "--sampling-factor", "2x1"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"dry-run": "true",
},
secondary: clif.ThirdPartyCommandLine{"--dry-run", "--sampling-factor", "2x1"},
Expand All @@ -233,7 +233,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, with secondary switch",
expected: []string{"--sampling-factor", "4:2:0", "--dry-run"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"sampling-factor": "4:2:0",
},
secondary: clif.ThirdPartyCommandLine{"--dry-run", "--sampling-factor", "2x1"},
Expand All @@ -245,7 +245,7 @@ var _ = Describe("Evaluate", Ordered, func() {
shouldReturn: "specified, secondary switch and flag",
expected: []string{"--gaussian-blur", "0.05", "--dry-run", "--sampling-factor", "2x1"},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"gaussian-blur": "0.05",
},
secondary: clif.ThirdPartyCommandLine{"--dry-run", "--sampling-factor", "2x1"},
Expand All @@ -265,7 +265,7 @@ var _ = Describe("Evaluate", Ordered, func() {
"--strip",
},
},
specified: clif.SpecifiedFlagsCollection{
specified: clif.ChangedFlagsMap{
"gaussian-blur": "0.05",
"i": "plane",
},
Expand Down
12 changes: 6 additions & 6 deletions src/clif/public-clif-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ type (
// using the value's String() method.
ThirdPartyOptionValue = string

// SpecifiedFlagsCollection represents the set of third party flags
// ChangedFlagsMap represents the set of third party flags
// presented by the user on the command line.
// (NB: Cobra does not currently have a mechanism to collect third
// party flags, by convention, anything that follows " -- "), therefore
// we need to collect and handle these flags/options explicitly,
// which is less than ideal.
// A difference between SpecifiedFlagsCollection and ThirdPartyCommandLine
// is that switch flags have a true/false option value in SpecifiedFlagsCollection
// A difference between ChangedFlagsMap and ThirdPartyCommandLine
// is that switch flags have a true/false option value in ChangedFlagsMap
// but not in ThirdPartyCommandLine.
SpecifiedFlagsCollection = collections.OrderedKeysMap[ThirdPartyFlagName, ThirdPartyOptionValue]
ChangedFlagsMap = collections.OrderedKeysMap[ThirdPartyFlagName, ThirdPartyOptionValue]

// ThirdPartyPresentFlags (see SpecifiedFlagsCollection).
ThirdPartyPresentFlags SpecifiedFlagsCollection
// ThirdPartyChangedFlags (see ChangedFlagsMap).
ThirdPartyChangedFlags ChangedFlagsMap

// KnownByCollection collection maps a full flag name to the
// short name it is also known by. If a flag does not
Expand Down

0 comments on commit dea7ec4

Please sign in to comment.