Skip to content

Commit

Permalink
templater options + names for command plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Oct 14, 2024
1 parent 76f6a39 commit 45d2ebd
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 17 deletions.
7 changes: 7 additions & 0 deletions api/cli/internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/general"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/vfs"

"ocm.software/ocm/api/config"
Expand Down Expand Up @@ -39,6 +40,12 @@ type FileSystem struct {
vfs.FileSystem
}

var _ osfs.OsFsCheck = (*FileSystem)(nil)

func (f *FileSystem) IsOsFileSystem() bool {
return osfs.IsOsFileSystem(f.FileSystem)
}

func (f *FileSystem) ApplyOption(options accessio.Options) error {
options.SetPathFileSystem(f.FileSystem)
return nil
Expand Down
2 changes: 1 addition & 1 deletion api/utils/template/gotmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func init() {
Register("go", func(_ vfs.FileSystem) Templater { return NewGo() }, `go templating supports complex values.
Register("go", func(_ vfs.FileSystem, _ TemplaterOptions) Templater { return NewGo() }, `go templating supports complex values.
<pre>
key:
subkey: "abc {{.MY_VAL}}"
Expand Down
2 changes: 1 addition & 1 deletion api/utils/template/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func init() {
Register("none", func(_ vfs.FileSystem) Templater { return NewSubst() }, `do not do any substitution.
Register("none", func(_ vfs.FileSystem, _ TemplaterOptions) Templater { return NewSubst() }, `do not do any substitution.
`)
}

Expand Down
19 changes: 15 additions & 4 deletions api/utils/template/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ import (
"sync"

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/general"
"github.com/mandelsoft/vfs/pkg/vfs"

"ocm.software/ocm/api/utils"
)

const KIND_TEMPLATER = "templater"

type TemplaterFactory func(system vfs.FileSystem) Templater
type (
TemplaterOptions map[string]interface{}
TemplaterFactory func(system vfs.FileSystem, options TemplaterOptions) Templater
)

func (t TemplaterOptions) Get(name string) interface{} {
if t == nil {
return nil
}
return t[name]
}

type Registry interface {
Register(name string, fac TemplaterFactory, desc string)
Create(name string, fs vfs.FileSystem) (Templater, error)
Create(name string, fs vfs.FileSystem, options ...TemplaterOptions) (Templater, error)
Describe(name string) (string, error)
KnownTypeNames() []string
}
Expand Down Expand Up @@ -48,15 +59,15 @@ func (r *registry) Register(name string, fac TemplaterFactory, desc string) {
}
}

func (r *registry) Create(name string, fs vfs.FileSystem) (Templater, error) {
func (r *registry) Create(name string, fs vfs.FileSystem, options ...TemplaterOptions) (Templater, error) {
r.lock.RLock()
defer r.lock.RUnlock()

t, ok := r.templaters[name]
if !ok {
return nil, errors.ErrNotSupported(KIND_TEMPLATER, name)
}
return t.templater(fs), nil
return t.templater(fs, general.Optional(options...)), nil
}

func (r *registry) Describe(name string) (string, error) {
Expand Down
13 changes: 11 additions & 2 deletions api/utils/template/spiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/mandelsoft/vfs/pkg/vfs"
)

const SPIFF_MODE = "mode"

func init() {
Register("spiff", NewSpiff, `[spiff templating](https://github.com/mandelsoft/spiff).
It supports complex values. the settings are accessible using the binding <code>values</code>.
Expand All @@ -23,9 +25,16 @@ type Spiff struct {
spiff spiffing.Spiff
}

func NewSpiff(fs vfs.FileSystem) Templater {
func NewSpiff(fs vfs.FileSystem, opts TemplaterOptions) Templater {
s := spiffing.New().WithFileSystem(fs).WithFeatures(features.CONTROL, features.INTERPOLATION)

m := opts.Get(SPIFF_MODE)

if mode, ok := m.(int); ok {
s = s.WithMode(mode)
}
return &Spiff{
spiffing.New().WithFileSystem(fs).WithFeatures(features.CONTROL, features.INTERPOLATION),
s,
}
}

Expand Down
2 changes: 1 addition & 1 deletion api/utils/template/subst.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func init() {
Register("subst", func(_ vfs.FileSystem) Templater { return NewSubst() }, `simple value substitution with the <code>drone/envsubst</code> templater.
Register("subst", func(_ vfs.FileSystem, _ TemplaterOptions) Templater { return NewSubst() }, `simple value substitution with the <code>drone/envsubst</code> templater.
It supports string values, only. Complex settings will be json encoded.
<pre>
key:
Expand Down
3 changes: 2 additions & 1 deletion api/utils/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
UseEnv bool
Templater Templater
Vars Values
Options map[string]interface{}
}

func (o *Options) DefaultMode() string {
Expand Down Expand Up @@ -63,7 +64,7 @@ func (o *Options) Complete(fs vfs.FileSystem) error {
}
}
}
o.Templater, err = DefaultRegistry().Create(o.Mode, fs)
o.Templater, err = DefaultRegistry().Create(o.Mode, fs, o.Options)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion cmds/ocm/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"ocm.software/ocm/cmds/ocm/commands/ocmcmds"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/componentarchive"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/components"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/names"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/plugins"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/pubsub"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/references"
Expand Down Expand Up @@ -309,11 +310,17 @@ func newCliCommand(opts *CLIOptions, mod ...func(clictx.Context, *cobra.Command)
v = verbs.NewCommand(ctx, c.Verb, "additional plugin based commands")
cmd.AddCommand(v)
}
types := []string{objtype}
if len(names.Aliases[objtype]) != 0 {
types = names.Aliases[objtype]
}

s := cobrautils.Find(v, objtype)
if s != nil {
out.Errf(opts.Context, "duplicate cli command %q of plugin %q for verb %q", objtype, p.Name(), c.Verb)
} else {
v.AddCommand(plugin.NewCommand(ctx, p, c.Name, objtype))
cmd := plugin.NewCommand(ctx, p, c.Name, types...)
v.AddCommand(cmd)
}

if c.Realm != "" {
Expand Down
29 changes: 29 additions & 0 deletions cmds/ocm/commands/ocmcmds/names/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,32 @@ var (
PubSub = []string{"pubsub", "ps"}
Verified = []string{"verified"}
)

var Aliases = map[string][]string{}

func init() {
add(
ComponentArchive,
CommonTransportArchive,
Components,
CLI,
Configuration,
ResourceConfig,
SourceConfig,
Resources,
Sources,
References,
Versions,
Plugins,
Action,
RoutingSlips,
PubSub,
Verified,
)
}

func add(aliases ...[]string) {
for _, a := range aliases {
Aliases[a[0]] = a
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ require (
github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3
github.com/mandelsoft/goutils v0.0.0-20240915132328-95975bffaef0
github.com/mandelsoft/logging v0.0.0-20240618075559-fdca28a87b0a
github.com/mandelsoft/spiff v1.7.0-beta-5
github.com/mandelsoft/vfs v0.4.4-0.20240915223828-8bc9369139c8
github.com/mandelsoft/spiff v1.3.0-beta-7.0.20241014123229-b7af70637d7b
github.com/mandelsoft/vfs v0.4.4
github.com/marstr/guid v1.1.0
github.com/mikefarah/yq/v4 v4.44.3
github.com/mitchellh/copystructure v1.2.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,10 @@ github.com/mandelsoft/goutils v0.0.0-20240915132328-95975bffaef0 h1:MtIgCrVzBP5t
github.com/mandelsoft/goutils v0.0.0-20240915132328-95975bffaef0/go.mod h1:9TJgkwSY43RWHiIAAz7fL8SEIHf0L13Pk4w8fDIt+i4=
github.com/mandelsoft/logging v0.0.0-20240618075559-fdca28a87b0a h1:MAvh0gbP2uwKmf7wWCkYCzrYa6vPjBvYeGhoUlVHwtI=
github.com/mandelsoft/logging v0.0.0-20240618075559-fdca28a87b0a/go.mod h1:uO460C1lIB3IOOgrbXhAlz3AKsOv4T2K6ALBn3PwuSg=
github.com/mandelsoft/spiff v1.7.0-beta-5 h1:3kC10nTviDQhL8diSxp7i4IC2iSiDg6KPbH1CAq7Lfw=
github.com/mandelsoft/spiff v1.7.0-beta-5/go.mod h1:TwEeOPuRZxlzQBCLEyVTlHmBSruSGGNdiQ2fovVJ8ao=
github.com/mandelsoft/vfs v0.4.4-0.20240915223828-8bc9369139c8 h1:tMAYF0nqqsoiUb6SZ+CneodHYTqQ0jLjL0PqOmKGBbo=
github.com/mandelsoft/vfs v0.4.4-0.20240915223828-8bc9369139c8/go.mod h1:zmbhx2ueQc96buqNXg2S88McBMm2mNFNeyGSpSebrHw=
github.com/mandelsoft/spiff v1.3.0-beta-7.0.20241014123229-b7af70637d7b h1:SyTUzkWcjNOZ1O1iULcTo65DzQbP8KDbKMre1aw/hKs=
github.com/mandelsoft/spiff v1.3.0-beta-7.0.20241014123229-b7af70637d7b/go.mod h1:HXurS33cKPLXXAI3SYll+Z6QotB1yzFFDyOFZ4QODcA=
github.com/mandelsoft/vfs v0.4.4 h1:hq+nI7NWzLLWR3Ii/w4agup4KpWjLpw6dAGtmvWr1Vw=
github.com/mandelsoft/vfs v0.4.4/go.mod h1:3ODt1ze/dCdOJCbhHX8ARAw7l422fDZUhbt0wqplBRs=
github.com/marstr/guid v1.1.0 h1:/M4H/1G4avsieL6BbUwCOBzulmoeKVP5ux/3mQNnbyI=
github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down

0 comments on commit 45d2ebd

Please sign in to comment.