feat: add spec definition api with versioning
This commit is contained in:
@ -6,7 +6,6 @@ import (
|
||||
agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/client/agent/flag"
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/command/client/apierr"
|
||||
clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/client/flag"
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/spec"
|
||||
"forge.cadoles.com/Cadoles/emissary/pkg/client"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
@ -23,6 +22,11 @@ func DeleteCommand() *cli.Command {
|
||||
Name: "spec-name",
|
||||
Usage: "use `NAME` as specification's name",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "spec-version",
|
||||
Usage: "use `VERSION` as specification's version",
|
||||
Value: "0.0.0",
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
baseFlags := clientFlag.GetBaseFlags(ctx)
|
||||
@ -37,14 +41,19 @@ func DeleteCommand() *cli.Command {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
specName, err := assertSpecName(ctx)
|
||||
specDefName, err := assertSpecDefName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
specDefVersion, err := assertSpecDefVersion(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||
|
||||
specName, err = client.DeleteAgentSpec(ctx.Context, agentID, specName)
|
||||
specDefName, specDefVersion, err = client.DeleteAgentSpec(ctx.Context, agentID, specDefName, specDefVersion)
|
||||
if err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
@ -54,9 +63,11 @@ func DeleteCommand() *cli.Command {
|
||||
}
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, struct {
|
||||
Name spec.Name `json:"name"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
}{
|
||||
Name: specName,
|
||||
Name: specDefName,
|
||||
Version: specDefVersion,
|
||||
}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
@ -36,9 +36,7 @@ func GetCommand() *cli.Command {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := format.Hints{
|
||||
OutputMode: baseFlags.OutputMode,
|
||||
}
|
||||
hints := specHints(baseFlags.OutputMode)
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(specs)...); err != nil {
|
||||
return errors.WithStack(err)
|
||||
|
21
internal/command/client/agent/spec/hints.go
Normal file
21
internal/command/client/agent/spec/hints.go
Normal file
@ -0,0 +1,21 @@
|
||||
package spec
|
||||
|
||||
import (
|
||||
"gitlab.com/wpetit/goweb/cli/format"
|
||||
"gitlab.com/wpetit/goweb/cli/format/table"
|
||||
)
|
||||
|
||||
func specHints(outputMode format.OutputMode) format.Hints {
|
||||
return format.Hints{
|
||||
OutputMode: outputMode,
|
||||
Props: []format.Prop{
|
||||
format.NewProp("ID", "ID"),
|
||||
format.NewProp("Revision", "Revision"),
|
||||
format.NewProp("DefinitionName", "Def. Name"),
|
||||
format.NewProp("DefinitionVersion", "Def. Version"),
|
||||
format.NewProp("Data", "Data"),
|
||||
format.NewProp("CreatedAt", "CreatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
format.NewProp("UpdatedAt", "UpdatedAt", table.WithCompactModeMaxColumnWidth(20)),
|
||||
},
|
||||
}
|
||||
}
|
@ -24,6 +24,11 @@ func UpdateCommand() *cli.Command {
|
||||
Name: "spec-name",
|
||||
Usage: "use `NAME` as specification's name",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "spec-version",
|
||||
Usage: "use `VERSION` as specification's version",
|
||||
Value: "0.0.0",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "spec-data",
|
||||
Usage: "use `DATA` as specification's data, '-' to read from STDIN",
|
||||
@ -44,7 +49,12 @@ func UpdateCommand() *cli.Command {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
specName, err := assertSpecName(ctx)
|
||||
specDefName, err := assertSpecDefName(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
specDefVersion, err := assertSpecDefVersion(ctx)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
@ -71,7 +81,7 @@ func UpdateCommand() *cli.Command {
|
||||
var existingSpec spec.Spec
|
||||
|
||||
for _, s := range specs {
|
||||
if s.SpecName() != specName {
|
||||
if s.SpecDefinitionName() != specDefName || s.SpecDefinitionVersion() != specDefVersion {
|
||||
continue
|
||||
}
|
||||
|
||||
@ -100,13 +110,10 @@ func UpdateCommand() *cli.Command {
|
||||
}
|
||||
|
||||
rawSpec := &spec.RawSpec{
|
||||
Name: specName,
|
||||
Revision: revision,
|
||||
Data: specData,
|
||||
}
|
||||
|
||||
if err := spec.Validate(ctx.Context, rawSpec); err != nil {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
DefinitionName: specDefName,
|
||||
DefinitionVersion: specDefVersion,
|
||||
Revision: revision,
|
||||
Data: specData,
|
||||
}
|
||||
|
||||
spec, err := client.UpdateAgentSpec(ctx.Context, agentID, rawSpec)
|
||||
@ -114,9 +121,7 @@ func UpdateCommand() *cli.Command {
|
||||
return errors.WithStack(apierr.Wrap(err))
|
||||
}
|
||||
|
||||
hints := format.Hints{
|
||||
OutputMode: baseFlags.OutputMode,
|
||||
}
|
||||
hints := specHints(baseFlags.OutputMode)
|
||||
|
||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, spec); err != nil {
|
||||
return errors.WithStack(err)
|
||||
@ -127,14 +132,24 @@ func UpdateCommand() *cli.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func assertSpecName(ctx *cli.Context) (spec.Name, error) {
|
||||
specName := ctx.String("spec-name")
|
||||
func assertSpecDefName(ctx *cli.Context) (string, error) {
|
||||
specDefName := ctx.String("spec-name")
|
||||
|
||||
if specName == "" {
|
||||
if specDefName == "" {
|
||||
return "", errors.New("flag 'spec-name' is required")
|
||||
}
|
||||
|
||||
return spec.Name(specName), nil
|
||||
return specDefName, nil
|
||||
}
|
||||
|
||||
func assertSpecDefVersion(ctx *cli.Context) (string, error) {
|
||||
specDefVersion := ctx.String("spec-version")
|
||||
|
||||
if specDefVersion == "" {
|
||||
return "", errors.New("flag 'spec-name' is required")
|
||||
}
|
||||
|
||||
return specDefVersion, nil
|
||||
}
|
||||
|
||||
func assertSpecData(ctx *cli.Context) (map[string]any, error) {
|
||||
|
Reference in New Issue
Block a user