Skip to content

Commit

Permalink
feat(store): add parameter families (#208)
Browse files Browse the repository at this point in the history
feat(store): add parameter families (#208)

feat(store): add worker pool family (#208)

feat(store): add profile family (#208)

feat(store): add preview family (#208)

feat(store): add family usage test (#208)
  • Loading branch information
plastikfan committed Oct 21, 2023
1 parent 7e96fe5 commit c02c728
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"magick",
"memfs",
"nakedret",
"nicksnyder",
"nolint",
"nolintlint",
"paramset",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/nicksnyder/go-i18n/v2 v2.2.1 // indirect
github.com/nicksnyder/go-i18n/v2 v2.2.1
github.com/samber/lo v1.38.1
github.com/snivilised/extendio v0.3.0
github.com/spf13/viper v1.17.0
Expand Down
6 changes: 6 additions & 0 deletions src/assistant/i18n/i18n-defs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package i18n

import (
"github.com/nicksnyder/go-i18n/v2/i18n"
)

const CobrassSourceID = "github.com/snivilised/cobrass"

// These definitions are in support of extendio's Localisable
Expand All @@ -10,3 +14,5 @@ type CobrassTemplData struct{}
func (td CobrassTemplData) SourceID() string {
return CobrassSourceID
}

type Message = i18n.Message
113 changes: 113 additions & 0 deletions src/assistant/i18n/messages-command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package i18n

// FilesGlobParamUsageTemplData
// 🧊
type FilesGlobParamUsageTemplData struct {
CobrassTemplData
}

func (td FilesGlobParamUsageTemplData) Message() *Message {
return &Message{
ID: "files-glob-filter.param-usage",
Description: "files glob filter (negate-able with leading !)",
Other: "files-gb files glob filter (negate-able with leading !)",
}
}

// FilesRegExParamUsageTemplData
// 🧊
type FilesRegExParamUsageTemplData struct {
CobrassTemplData
}

func (td FilesRegExParamUsageTemplData) Message() *Message {
return &Message{
ID: "files-regex-filter.param-usage",
Description: "files regex filter (negate-able with leading !)",
Other: "files-rx folder regular expression filter (negate-able with leading !)",
}
}

// FolderGlobParamUsageTemplData
// 🧊
type FolderGlobParamUsageTemplData struct {
CobrassTemplData
}

func (td FolderGlobParamUsageTemplData) Message() *Message {
return &Message{
ID: "folders-glob-filter.param-usage",
Description: "folders glob (negate-able with leading !)",
Other: "folders-gb folder glob filter (negate-able with leading !)",
}
}

// FolderRexExParamUsageTemplData
// 🧊
type FolderRexExParamUsageTemplData struct {
CobrassTemplData
}

func (td FolderRexExParamUsageTemplData) Message() *Message {
return &Message{
ID: "folders-regex-filter.param-usage",
Description: "folders regex filter (negate-able with leading !)",
Other: "folders-rx folder regular expression filter (negate-able with leading !)",
}
}

// WorkerPoolCPUParamUsageTemplData
// 🧊
type WorkerPoolCPUParamUsageTemplData struct {
CobrassTemplData
}

func (td WorkerPoolCPUParamUsageTemplData) Message() *Message {
return &Message{
ID: "worker-pool-cpu.param-usage",
Description: "run with the number of workers in pool set to number of CPUs available",
Other: "cpu denotes parallel execution with all available processors",
}
}

// WorkerPoolCPUParamUsageTemplData
// 🧊
type WorkerPoolNoWParamUsageTemplData struct {
CobrassTemplData
}

func (td WorkerPoolNoWParamUsageTemplData) Message() *Message {
return &Message{
ID: "worker-pool-cpu.param-usage",
Description: "run with the number of workers in pool set to this number",
Other: "now denotes parallel execution with this number of workers in pool",
}
}

// WorkerPoolCPUParamUsageTemplData
// 🧊
type ProfileParamUsageTemplData struct {
CobrassTemplData
}

func (td ProfileParamUsageTemplData) Message() *Message {
return &Message{
ID: "profile.param-usage",
Description: "pre-defined flag/option list in config file",
Other: "profile specifies which set of flags/options to load from config",
}
}

// WorkerPoolCPUParamUsageTemplData
// 🧊
type DryRunParamUsageTemplData struct {
CobrassTemplData
}

func (td DryRunParamUsageTemplData) Message() *Message {
return &Message{
ID: "dry-run.param-usage",
Description: "allows the user to preview the effects of a command without running it",
Other: "dry-run allows the user to see the effects of a command without running it",
}
}
13 changes: 13 additions & 0 deletions src/assistant/i18n/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package i18n

import (
"strings"
)

func LeadsWith(name, text string) string {
if strings.HasPrefix(text, name) {
return text
}

return name + " " + text
}
5 changes: 5 additions & 0 deletions src/assistant/param-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ type ParamSet[N any] struct {
// FlagSet is the default Cobra FlagSet
//
FlagSet *pflag.FlagSet

// Command is the cobra command that the parameter set is bound to.
//
Command *cobra.Command
}

// NewParamSet is the factory function, which creates a 'parameter set' for
Expand All @@ -140,6 +144,7 @@ func NewParamSet[N any](command *cobra.Command) (ps *ParamSet[N]) {
ps = new(ParamSet[N])
ps.FlagSet = command.Flags()
ps.Native = new(N)
ps.Command = command

if reflect.TypeOf(*ps.Native).Kind() != reflect.Struct {
typeName := reflect.TypeOf(*ps.Native).Name()
Expand Down
178 changes: 178 additions & 0 deletions src/store/families_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package store_test

import (
"fmt"

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

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

func reason(binder string, err error) string {
return fmt.Sprintf("🔥 expected '%v' error to be nil, but was '%v'\n",
binder, err,
)
}

const (
shouldMessage = "🧪 should: bind all parameters without error"
)

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

from xi18n.LoadFrom
rootCommand *cobra.Command
execute func(args []string)
)

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())
}

execute = func(args []string) {
_, err := helpers.ExecuteCommand(
rootCommand, args...,
)
Expect(err).Error().To(BeNil(), reason("BindAll", err))
}
})

BeforeEach(func() {
rootCommand = &cobra.Command{
Use: "scorch",
Short: "scotch",
Long: "scorch is a fake test command which contains filtering capabilities",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
})

DescribeTable("filter family",
func(commandLine []string) {
ps := assistant.NewParamSet[store.FilterParameterSet](rootCommand)
ps.Native.BindAll(ps)

execute(commandLine)
},
func(args []string) string {
return shouldMessage
},
Entry(
nil, []string{"--files-rx", "^foo", "--folders-gb", "bar*"},
),
Entry(
nil, []string{"-X", "^foo", "-z", "bar*"},
),
Entry(
nil, []string{"--files-gb", "foo*", "--folders-rx", "^bar"},
),
Entry(
nil, []string{"-G", "foo*", "-y", "^bar"},
),
)

DescribeTable("worker pool family",
func(commandLine []string) {
ps := assistant.NewParamSet[store.WorkerPoolParameterSet](rootCommand)
ps.Native.BindAll(ps)

execute(commandLine)
},
func(args []string) string {
return shouldMessage
},
Entry(
nil, []string{"--cpu"},
),
Entry(
nil, []string{"-C"},
),
Entry(
nil, []string{"--now", "4"},
),
Entry(
nil, []string{"-N", "4"},
),
)

DescribeTable("profile family",
func(commandLine []string) {
ps := assistant.NewParamSet[store.ProfileParameterSet](rootCommand)
ps.Native.BindAll(ps)

execute(commandLine)
},
func(args []string) string {
return shouldMessage
},
Entry(
nil, []string{"--profile", "foo"},
),
Entry(
nil, []string{"-P", "foo"},
),
)

DescribeTable("profile family",
func(commandLine []string) {
ps := assistant.NewParamSet[store.PreviewParameterSet](rootCommand)
ps.Native.BindAll(ps)

execute(commandLine)
},
func(args []string) string {
return shouldMessage
},
Entry(
nil, []string{"--dry-run"},
),
Entry(
nil, []string{"-D"},
),
)

When("usage requested", func() {
It("should: 🧪 show help text", func() {
filtersPS := assistant.NewParamSet[store.FilterParameterSet](rootCommand)
filtersPS.Native.BindAll(filtersPS)
//
poolPS := assistant.NewParamSet[store.WorkerPoolParameterSet](rootCommand)
poolPS.Native.BindAll(poolPS)
//
profilePS := assistant.NewParamSet[store.ProfileParameterSet](rootCommand)
profilePS.Native.BindAll(profilePS)
//
previewPS := assistant.NewParamSet[store.PreviewParameterSet](rootCommand)
previewPS.Native.BindAll(previewPS)
//
commandLine := []string{"scorch", "--help"}
_, err := helpers.ExecuteCommand(
rootCommand, commandLine...,
)
Expect(err).Error().To(BeNil(), reason("BindAll", err))
})
})
})
Loading

0 comments on commit c02c728

Please sign in to comment.