-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store)!:type renames; and add GetThirdParytCL (#233)
- Loading branch information
1 parent
6923c30
commit dea7ec4
Showing
7 changed files
with
183 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
"gradientf", | ||
"gradientsf", | ||
"ineffassign", | ||
"infex", | ||
"Infexion", | ||
"ipaddress", | ||
"ipmask", | ||
|
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,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 | ||
} |
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,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"}, | ||
}), | ||
) | ||
}) |
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