-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0625bc9
commit 5dd5512
Showing
13 changed files
with
552 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package internal | ||
|
||
import ( | ||
"ocm.software/ocm/api/credentials" | ||
) | ||
|
||
type InputSpecInfo struct { | ||
Short string `json:"short"` | ||
MediaType string `json:"mediaType"` | ||
Hint string `json:"hint"` | ||
ConsumerId credentials.ConsumerIdentity `json:"consumerId"` | ||
} |
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,71 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/mandelsoft/goutils/errors" | ||
|
||
"ocm.software/ocm/api/ocm/plugin/ppi" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/accessmethod" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input/compose" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input/get" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input/validate" | ||
"ocm.software/ocm/api/utils/cobrautils/flagsets" | ||
) | ||
|
||
func (p *pluginImpl) ValidateInputSpec(spec []byte) (*ppi.InputSpecInfo, error) { | ||
result, err := p.Exec(nil, nil, input.Name, validate.Name, string(spec)) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "plugin %s", p.Name()) | ||
} | ||
|
||
var info ppi.InputSpecInfo | ||
err = json.Unmarshal(result, &info) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "plugin %s: cannot unmarshal input spec info", p.Name()) | ||
} | ||
return &info, nil | ||
} | ||
|
||
func (p *pluginImpl) ComposeInputSpec(name string, opts flagsets.ConfigOptions, base flagsets.Config) error { | ||
cfg := flagsets.Config{} | ||
for _, o := range opts.Options() { | ||
cfg[o.GetName()] = o.Value() | ||
} | ||
optsdata, err := json.Marshal(cfg) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot marshal option values") | ||
} | ||
basedata, err := json.Marshal(base) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot marshal input specification base value") | ||
} | ||
result, err := p.Exec(nil, nil, input.Name, compose.Name, name, string(optsdata), string(basedata)) | ||
if err != nil { | ||
return err | ||
} | ||
var r flagsets.Config | ||
err = json.Unmarshal(result, &r) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot unmarshal composition result") | ||
} | ||
|
||
for k := range base { | ||
delete(base, k) | ||
} | ||
for k, v := range r { | ||
base[k] = v | ||
} | ||
return nil | ||
} | ||
|
||
func (p *pluginImpl) GetInputBlob(w io.Writer, creds, spec json.RawMessage) error { | ||
args := []string{accessmethod.Name, get.Name, string(spec)} | ||
if creds != nil { | ||
args = append(args, "--"+get.OptCreds, string(creds)) | ||
} | ||
_, err := p.Exec(nil, w, args...) | ||
return err | ||
} |
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,26 @@ | ||
package input | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"ocm.software/ocm/api/ocm/plugin/ppi" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input/compose" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input/get" | ||
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/input/validate" | ||
) | ||
|
||
const Name = "input" | ||
|
||
func New(p ppi.Plugin) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: Name, | ||
Short: "input operations for component version composition", | ||
Long: `This command group provides all commands used to implement an input type | ||
described by an input type descriptor (<CMD>` + p.Name() + ` descriptor</CMD>.`, | ||
} | ||
|
||
cmd.AddCommand(validate.New(p)) | ||
cmd.AddCommand(get.New(p)) | ||
cmd.AddCommand(compose.New(p)) | ||
return cmd | ||
} |
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,93 @@ | ||
package compose | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/mandelsoft/goutils/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"ocm.software/ocm/api/ocm/plugin/descriptor" | ||
|
||
"ocm.software/ocm/api/ocm/extensions/accessmethods/options" | ||
"ocm.software/ocm/api/ocm/plugin/ppi" | ||
"ocm.software/ocm/api/utils/runtime" | ||
) | ||
|
||
const Name = "compose" | ||
|
||
func New(p ppi.Plugin) *cobra.Command { | ||
opts := Options{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: Name + " <name> <options json> <base spec json>", | ||
Short: "compose input specification from options and base specification", | ||
Long: ` | ||
The task of this command is to compose an input specification based on some | ||
explicitly given input options and preconfigured specifications. | ||
The finally composed input specification has to be returned as JSON document | ||
on *stdout*. | ||
This command is only used, if for an input method descriptor configuration | ||
options are defined (<CMD>` + p.Name() + ` descriptor</CMD>). | ||
If possible, predefined standard options should be used. In such a case only the | ||
<code>name</code> field should be defined for an option. If required, new options can be | ||
defined by additionally specifying a type and a description. New options should | ||
be used very carefully. The chosen names MUST not conflict with names provided | ||
by other plugins. Therefore, it is highly recommended to use names prefixed | ||
by the plugin name. | ||
` + options.DefaultRegistry.Usage(), | ||
Args: cobra.ExactArgs(3), | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.Complete(args) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return Command(p, cmd, &opts) | ||
}, | ||
} | ||
opts.AddFlags(cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
type Options struct { | ||
Name string | ||
Options ppi.Config | ||
Base ppi.Config | ||
} | ||
|
||
func (o *Options) AddFlags(fs *pflag.FlagSet) { | ||
} | ||
|
||
func (o *Options) Complete(args []string) error { | ||
o.Name = args[0] | ||
if err := runtime.DefaultYAMLEncoding.Unmarshal([]byte(args[1]), &o.Options); err != nil { | ||
return errors.Wrapf(err, "invalid input specification options") | ||
} | ||
if err := runtime.DefaultYAMLEncoding.Unmarshal([]byte(args[2]), &o.Base); err != nil { | ||
return errors.Wrapf(err, "invalid base input specification") | ||
} | ||
return nil | ||
} | ||
|
||
func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) error { | ||
m := p.GetInputType(opts.Name) | ||
if m == nil { | ||
return errors.ErrUnknown(descriptor.KIND_INPUTTYPE, opts.Name) | ||
} | ||
err := opts.Options.ConvertFor(m.Options()...) | ||
if err != nil { | ||
return err | ||
} | ||
err = m.ComposeSpecification(p, opts.Options, opts.Base) | ||
if err != nil { | ||
return err | ||
} | ||
data, err := json.Marshal(opts.Base) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Printf("%s\n", string(data)) | ||
return nil | ||
} |
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,87 @@ | ||
package get | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/mandelsoft/goutils/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
||
"ocm.software/ocm/api/credentials" | ||
"ocm.software/ocm/api/ocm/plugin/descriptor" | ||
"ocm.software/ocm/api/ocm/plugin/ppi" | ||
commonppi "ocm.software/ocm/api/ocm/plugin/ppi/cmds/common" | ||
"ocm.software/ocm/api/utils/cobrautils/flag" | ||
"ocm.software/ocm/api/utils/runtime" | ||
) | ||
|
||
const ( | ||
Name = "get" | ||
OptCreds = commonppi.OptCreds | ||
) | ||
|
||
func New(p ppi.Plugin) *cobra.Command { | ||
opts := Options{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: Name + " [<flags>] <access spec>", | ||
Short: "get blob", | ||
Long: ` | ||
Evaluate the given input specification and return the described blob on | ||
*stdout*.`, | ||
Args: cobra.ExactArgs(1), | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.Complete(args) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return Command(p, cmd, &opts) | ||
}, | ||
} | ||
opts.AddFlags(cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
type Options struct { | ||
Credentials credentials.DirectCredentials | ||
Specification json.RawMessage | ||
} | ||
|
||
func (o *Options) AddFlags(fs *pflag.FlagSet) { | ||
flag.YAMLVarP(fs, &o.Credentials, OptCreds, "c", nil, "credentials") | ||
flag.StringToStringVarPFA(fs, &o.Credentials, "credential", "C", nil, "dedicated credential value") | ||
} | ||
|
||
func (o *Options) Complete(args []string) error { | ||
if err := runtime.DefaultYAMLEncoding.Unmarshal([]byte(args[0]), &o.Specification); err != nil { | ||
return errors.Wrapf(err, "invalid repository specification") | ||
} | ||
|
||
fmt.Fprintf(os.Stderr, "credentials: %s\n", o.Credentials.String()) | ||
return nil | ||
} | ||
|
||
func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) error { | ||
spec, err := p.DecodeInputSpecification(opts.Specification) | ||
if err != nil { | ||
return errors.Wrapf(err, "access specification") | ||
} | ||
|
||
m := p.GetInputType(spec.GetType()) | ||
if m == nil { | ||
return errors.ErrUnknown(descriptor.KIND_INPUTTYPE, spec.GetType()) | ||
} | ||
_, err = m.ValidateSpecification(p, spec) | ||
if err != nil { | ||
return err | ||
} | ||
r, err := m.Reader(p, spec, opts.Credentials) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = io.Copy(os.Stdout, r) | ||
r.Close() | ||
return err | ||
} |
Oops, something went wrong.