CLI Generation (#953)
This commit is contained in:
1 parent
282bbe619e
commit
341e7aff0d
77 files changed
+10303
-6861
No files matched your search
@@ -0,0 +1,12 @@
|
||||
# Determine this makefile's path.
|
||||
# Be sure to place this BEFORE `include` directives, if any.
|
||||
THIS_FILE := $(lastword $(MAKEFILE_LIST))
|
||||
|
||||
export CLI_GEN_BASEPATH := ${GEN_BASEPATH}/internal/cmd/commands
|
||||
|
||||
cli:
|
||||
go run .
|
||||
goimports -w ${CLI_GEN_BASEPATH}
|
||||
gofumpt -w ${CLI_GEN_BASEPATH}
|
||||
|
||||
.PHONY: cli
|
||||
@@ -0,0 +1,308 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
)
|
||||
|
||||
type cmdInfo struct {
|
||||
// The type of the resource, e.g. "target"
|
||||
ResourceType string
|
||||
|
||||
// The API package under the api path
|
||||
Pkg string
|
||||
|
||||
// Standard actions (with standard parameters) used by this resource
|
||||
StdActions []string
|
||||
|
||||
// HasExtraCommandVars controls whether to generate an embedded struct with
|
||||
// extra command variables
|
||||
HasExtraCommandVars bool
|
||||
|
||||
// SkipNormalHelp indicates skipping the normal help case for when it needs
|
||||
// to be only custom (mainly for subactions)
|
||||
SkipNormalHelp bool
|
||||
|
||||
// HasExtraHelpFunc controls whether to include a default statement chaining
|
||||
// to an extra help function
|
||||
HasExtraHelpFunc bool
|
||||
|
||||
// HasExampleCliOutput controls whether to generate code to look for a CLI
|
||||
// output env var and print
|
||||
HasExampleCliOutput bool
|
||||
|
||||
// IsAbstractType triggers some behavior specialized for abstract types,
|
||||
// e.g. those that have subcommands for create/update
|
||||
IsAbstractType bool
|
||||
|
||||
// HasId controls whether to add ID emptiness checking. Note that some
|
||||
// commands that allow name/scope id or name/scope name handling may skip
|
||||
// this in favor of custom logic.
|
||||
HasId bool
|
||||
|
||||
// Container controls what to generate for a required container (scope ID,
|
||||
// auth method ID, etc.)
|
||||
Container string
|
||||
|
||||
// HasName controls whether to add name options
|
||||
HasName bool
|
||||
|
||||
// HasDescription controls whether to add description options
|
||||
HasDescription bool
|
||||
|
||||
// HasScopeName controls whether to add scope name options
|
||||
HasScopeName bool
|
||||
|
||||
// VersionedActions controls which actions to add a case for version checking
|
||||
VersionedActions []string
|
||||
|
||||
// SubActionPrefix specifies the prefix to use when generating sub-action
|
||||
// commands (e.g. "targets update tcp")
|
||||
SubActionPrefix string
|
||||
|
||||
// NeedsSubTypeInCreate controls whether the sub-type must be passed in as
|
||||
// an argument to a create call. Targets need this, accounts do not, etc.
|
||||
NeedsSubTypeInCreate bool
|
||||
}
|
||||
|
||||
var inputStructs = map[string][]*cmdInfo{
|
||||
"accounts": {
|
||||
{
|
||||
ResourceType: resource.Account.String(),
|
||||
Pkg: "accounts",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasExtraHelpFunc: true,
|
||||
IsAbstractType: true,
|
||||
Container: "AuthMethod",
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"change-password", "set-password"},
|
||||
},
|
||||
{
|
||||
ResourceType: resource.Account.String(),
|
||||
Pkg: "accounts",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "password",
|
||||
HasExtraCommandVars: true,
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
Container: "AuthMethod",
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update"},
|
||||
},
|
||||
},
|
||||
"authmethods": {
|
||||
{
|
||||
ResourceType: resource.AuthMethod.String(),
|
||||
Pkg: "authmethods",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
IsAbstractType: true,
|
||||
HasExtraHelpFunc: true,
|
||||
Container: "Scope",
|
||||
HasId: true,
|
||||
},
|
||||
{
|
||||
ResourceType: resource.AuthMethod.String(),
|
||||
Pkg: "authmethods",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "password",
|
||||
HasExtraCommandVars: true,
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
Container: "Scope",
|
||||
VersionedActions: []string{"update"},
|
||||
NeedsSubTypeInCreate: true,
|
||||
},
|
||||
},
|
||||
"authtokens": {
|
||||
{
|
||||
ResourceType: resource.AuthToken.String(),
|
||||
Pkg: "authtokens",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
Container: "Scope",
|
||||
HasId: true,
|
||||
},
|
||||
},
|
||||
"groups": {
|
||||
{
|
||||
ResourceType: resource.Group.String(),
|
||||
Pkg: "groups",
|
||||
StdActions: []string{"create", "read", "update", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
Container: "Scope",
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update", "add-members", "remove-members", "set-members"},
|
||||
},
|
||||
},
|
||||
"hostcatalogs": {
|
||||
{
|
||||
ResourceType: resource.HostCatalog.String(),
|
||||
Pkg: "hostcatalogs",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
IsAbstractType: true,
|
||||
HasExtraHelpFunc: true,
|
||||
Container: "Scope",
|
||||
HasId: true,
|
||||
},
|
||||
{
|
||||
ResourceType: resource.HostCatalog.String(),
|
||||
Pkg: "hostcatalogs",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "static",
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
Container: "Scope",
|
||||
VersionedActions: []string{"update"},
|
||||
NeedsSubTypeInCreate: true,
|
||||
},
|
||||
},
|
||||
"hostsets": {
|
||||
{
|
||||
ResourceType: resource.HostSet.String(),
|
||||
Pkg: "hostsets",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasExtraHelpFunc: true,
|
||||
IsAbstractType: true,
|
||||
Container: "HostCatalog",
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"add-hosts", "set-hosts", "remove-hosts"},
|
||||
},
|
||||
{
|
||||
ResourceType: resource.HostSet.String(),
|
||||
Pkg: "hostsets",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "static",
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
Container: "HostCatalog",
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update"},
|
||||
},
|
||||
},
|
||||
"hosts": {
|
||||
{
|
||||
ResourceType: resource.Host.String(),
|
||||
Pkg: "hosts",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
HasExtraHelpFunc: true,
|
||||
IsAbstractType: true,
|
||||
Container: "HostCatalog",
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
},
|
||||
{
|
||||
ResourceType: resource.Host.String(),
|
||||
Pkg: "hosts",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "static",
|
||||
HasExtraCommandVars: true,
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
Container: "HostCatalog",
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update"},
|
||||
},
|
||||
},
|
||||
"roles": {
|
||||
{
|
||||
ResourceType: resource.Role.String(),
|
||||
Pkg: "roles",
|
||||
StdActions: []string{"create", "read", "update", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
Container: "Scope",
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update", "add-grants", "remove-grants", "set-grants", "add-principals", "remove-principals", "set-principals"},
|
||||
},
|
||||
},
|
||||
"scopes": {
|
||||
{
|
||||
ResourceType: resource.Scope.String(),
|
||||
Pkg: "scopes",
|
||||
StdActions: []string{"create", "read", "update", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasId: true,
|
||||
Container: "Scope",
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update"},
|
||||
},
|
||||
},
|
||||
"sessions": {
|
||||
{
|
||||
ResourceType: resource.Session.String(),
|
||||
Pkg: "sessions",
|
||||
StdActions: []string{"read", "list"},
|
||||
Container: "Scope",
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
VersionedActions: []string{"cancel"},
|
||||
},
|
||||
},
|
||||
"targets": {
|
||||
{
|
||||
ResourceType: resource.Target.String(),
|
||||
Pkg: "targets",
|
||||
StdActions: []string{"read", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasExampleCliOutput: true,
|
||||
IsAbstractType: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
Container: "Scope",
|
||||
VersionedActions: []string{"add-host-sets", "remove-host-sets", "set-host-sets"},
|
||||
},
|
||||
{
|
||||
ResourceType: resource.Target.String(),
|
||||
Pkg: "targets",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "tcp",
|
||||
HasExtraCommandVars: true,
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
Container: "Scope",
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update"},
|
||||
NeedsSubTypeInCreate: true,
|
||||
},
|
||||
},
|
||||
"users": {
|
||||
{
|
||||
ResourceType: resource.User.String(),
|
||||
Pkg: "users",
|
||||
StdActions: []string{"create", "read", "update", "delete", "list"},
|
||||
HasExtraCommandVars: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
Container: "Scope",
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
VersionedActions: []string{"update", "add-accounts", "remove-accounts", "set-accounts"},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
fillTemplates()
|
||||
}
|
||||
@@ -0,0 +1,463 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/iancoleman/strcase"
|
||||
)
|
||||
|
||||
func fillTemplates() {
|
||||
for pkg, structs := range inputStructs {
|
||||
for _, data := range structs {
|
||||
outBuf := new(bytes.Buffer)
|
||||
|
||||
if err := cmdTemplate.Execute(outBuf, data); err != nil {
|
||||
fmt.Printf("error executing struct template for resource %s: %v\n", pkg, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fName := pkg
|
||||
if data.SubActionPrefix != "" {
|
||||
fName = fmt.Sprintf("%s_%s", data.SubActionPrefix, fName)
|
||||
}
|
||||
outFile, err := filepath.Abs(fmt.Sprintf("%s/%scmd/%s.gen.go", os.Getenv("CLI_GEN_BASEPATH"), pkg, fName))
|
||||
if err != nil {
|
||||
fmt.Printf("error opening file for package %s: %v\n", pkg, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
outDir := filepath.Dir(outFile)
|
||||
if _, err := os.Stat(outDir); os.IsNotExist(err) {
|
||||
_ = os.Mkdir(outDir, os.ModePerm)
|
||||
}
|
||||
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0o644); err != nil {
|
||||
fmt.Printf("error writing file %q: %v\n", outFile, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func camelCase(in string) string {
|
||||
return strcase.ToCamel(in)
|
||||
}
|
||||
|
||||
func envCase(in string) string {
|
||||
return strcase.ToScreamingSnake(in)
|
||||
}
|
||||
|
||||
func snakeCase(in string) string {
|
||||
return strcase.ToSnake(in)
|
||||
}
|
||||
|
||||
func kebabCase(in string) string {
|
||||
return strcase.ToKebab(in)
|
||||
}
|
||||
|
||||
func lowerSpaceCase(in string) string {
|
||||
return strcase.ToDelimited(in, ' ')
|
||||
}
|
||||
|
||||
func hasAction(in []string, action string) bool {
|
||||
return strutil.StrListContains(in, action)
|
||||
}
|
||||
|
||||
var cmdTemplate = template.Must(template.New("").Funcs(
|
||||
template.FuncMap{
|
||||
"camelCase": camelCase,
|
||||
"envCase": envCase,
|
||||
"snakeCase": snakeCase,
|
||||
"kebabCase": kebabCase,
|
||||
"lowerSpaceCase": lowerSpaceCase,
|
||||
"hasAction": hasAction,
|
||||
},
|
||||
).Parse(`{{ $input := . }}
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package {{ .Pkg }}cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/{{ .Pkg }}"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/common"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
func init{{ camelCase .SubActionPrefix }}Flags() {
|
||||
flagsOnce.Do(func() {
|
||||
extraFlags := extra{{ camelCase .SubActionPrefix }}ActionsFlagsMapFunc()
|
||||
for k, v := range extraFlags {
|
||||
flags{{ camelCase .SubActionPrefix }}Map[k] = append(flags{{ camelCase .SubActionPrefix }}Map[k], v...)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
_ cli.Command = (*{{ camelCase .SubActionPrefix }}Command)(nil)
|
||||
_ cli.CommandAutocomplete = (*{{ camelCase .SubActionPrefix }}Command)(nil)
|
||||
)
|
||||
|
||||
type {{ camelCase .SubActionPrefix }}Command struct {
|
||||
*base.Command
|
||||
|
||||
Func string
|
||||
|
||||
plural string
|
||||
{{ if .HasExtraCommandVars }}
|
||||
extra{{ camelCase .SubActionPrefix }}CmdVars
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
func (c *{{ camelCase .SubActionPrefix }}Command) AutocompleteArgs() complete.Predictor {
|
||||
init{{ camelCase .SubActionPrefix }}Flags()
|
||||
return complete.PredictAnything
|
||||
}
|
||||
|
||||
func (c *{{ camelCase .SubActionPrefix }}Command) AutocompleteFlags() complete.Flags {
|
||||
init{{ camelCase .SubActionPrefix }}Flags()
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *{{ camelCase .SubActionPrefix }}Command) Synopsis() string {
|
||||
if extra := extra{{ camelCase .SubActionPrefix }}SynopsisFunc(c); extra != "" {
|
||||
return extra
|
||||
}
|
||||
|
||||
synopsisStr := "{{ lowerSpaceCase .ResourceType }}"
|
||||
{{ if .SubActionPrefix }}
|
||||
synopsisStr = fmt.Sprintf("%s %s", "{{ .SubActionPrefix }}-type", synopsisStr)
|
||||
{{ end }}
|
||||
return common.SynopsisFunc(c.Func, synopsisStr)
|
||||
}
|
||||
|
||||
func (c *{{ camelCase .SubActionPrefix }}Command) Help() string {
|
||||
init{{ camelCase .SubActionPrefix }}Flags()
|
||||
|
||||
var helpStr string
|
||||
helpMap := common.HelpMap("{{ lowerSpaceCase .ResourceType }}")
|
||||
|
||||
switch c.Func {
|
||||
{{ if not .SkipNormalHelp }}
|
||||
{{ range $i, $action := .StdActions }}
|
||||
case "{{ $action }}":
|
||||
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
default:
|
||||
{{ if .HasExtraHelpFunc }}
|
||||
helpStr = c.extra{{ camelCase .SubActionPrefix }}HelpFunc(helpMap)
|
||||
{{ else }}
|
||||
helpStr = helpMap["base"]()
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
// Keep linter from complaining if we don't actually generate code using it
|
||||
_ = helpMap
|
||||
return helpStr
|
||||
}
|
||||
|
||||
var flags{{ camelCase .SubActionPrefix }}Map = map[string][]string{
|
||||
{{ range $i, $action := .StdActions }}
|
||||
{{ if eq $action "create" }}
|
||||
"create": { "{{ kebabCase $input.Container }}-id", "name", "description" },
|
||||
{{ end }}
|
||||
{{ if eq $action "read" }}
|
||||
"read": {"id"},
|
||||
{{ end }}
|
||||
{{ if eq $action "update" }}
|
||||
"update": {"id", "name", "description" {{ if hasAction $input.VersionedActions "update" }}, "version" {{ end }} },
|
||||
{{ end }}
|
||||
{{ if eq $action "delete" }}
|
||||
"delete": {"id"},
|
||||
{{ end }}
|
||||
{{ if eq $action "list" }}
|
||||
"list": { "{{ kebabCase $input.Container }}-id" {{ if (eq $input.Container "Scope") }}, "recursive"{{ end }} },
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
func (c *{{ camelCase .SubActionPrefix }}Command) Flags() *base.FlagSets {
|
||||
if len(flags{{ camelCase .SubActionPrefix }}Map[c.Func]) == 0 {
|
||||
return c.FlagSet(base.FlagSetNone)
|
||||
}
|
||||
|
||||
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
|
||||
f := set.NewFlagSet("Command Options")
|
||||
common.PopulateCommonFlags(c.Command, f, "{{ if .SubActionPrefix }}{{ .SubActionPrefix }}-type {{ end }}{{ lowerSpaceCase .ResourceType }}", flags{{ camelCase .SubActionPrefix }}Map[c.Func])
|
||||
|
||||
extra{{ camelCase .SubActionPrefix }}FlagsFunc(c, set, f)
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
|
||||
init{{ camelCase .SubActionPrefix }}Flags()
|
||||
|
||||
{{ if .HasExampleCliOutput }}
|
||||
if os.Getenv("BOUNDARY_EXAMPLE_CLI_OUTPUT") != "" {
|
||||
c.UI.Output(exampleOutput())
|
||||
return 0
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
switch c.Func {
|
||||
case "":
|
||||
return cli.RunResultHelp
|
||||
{{ if .IsAbstractType }}
|
||||
case "create", "update":
|
||||
return cli.RunResultHelp
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
c.plural = "{{ if .SubActionPrefix }}{{ .SubActionPrefix }}-type {{ end }}{{ lowerSpaceCase .ResourceType }}"
|
||||
switch c.Func {
|
||||
case "list":
|
||||
c.plural = "{{ if .SubActionPrefix }}{{ .SubActionPrefix }}-type {{ end }}{{ lowerSpaceCase .ResourceType }}s"
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
{{ if .HasId }}
|
||||
if strutil.StrListContains(flags{{ camelCase .SubActionPrefix }}Map[c.Func], "id") && c.FlagId == "" {
|
||||
c.UI.Error("ID is required but not passed in via -id")
|
||||
return 1
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
var opts []{{ .Pkg }}.Option
|
||||
|
||||
if strutil.StrListContains(flags{{ camelCase .SubActionPrefix }}Map[c.Func], "{{ kebabCase .Container }}-id") {
|
||||
switch c.Func {
|
||||
{{ if hasAction .StdActions "create" }}
|
||||
case "create":
|
||||
if c.Flag{{ .Container }}Id == "" {
|
||||
c.UI.Error("{{ .Container }} ID must be passed in via -{{ kebabCase .Container }}-id or BOUNDARY_{{ envCase .Container }}_ID")
|
||||
return 1
|
||||
}
|
||||
{{ end }}
|
||||
{{ if hasAction .StdActions "list" }}
|
||||
case "list":
|
||||
if c.Flag{{ .Container }}Id == "" {
|
||||
c.UI.Error("{{ .Container }} ID must be passed in via -{{ kebabCase .Container }}-id or BOUNDARY_{{ envCase .Container }}_ID")
|
||||
return 1
|
||||
}
|
||||
{{ end }}
|
||||
}
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
|
||||
return 2
|
||||
}
|
||||
{{ .Pkg }}Client := {{ .Pkg }}.NewClient(client)
|
||||
|
||||
{{ if .HasName }}
|
||||
switch c.FlagName {
|
||||
case "":
|
||||
case "null":
|
||||
opts = append(opts, {{ .Pkg }}.DefaultName())
|
||||
default:
|
||||
opts = append(opts, {{ .Pkg }}.WithName(c.FlagName))
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ if .HasDescription }}
|
||||
switch c.FlagDescription {
|
||||
case "":
|
||||
case "null":
|
||||
opts = append(opts, {{ .Pkg }}.DefaultDescription())
|
||||
default:
|
||||
opts = append(opts, {{ .Pkg }}.WithDescription(c.FlagDescription))
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ if (eq .Container "Scope") }}
|
||||
switch c.FlagRecursive {
|
||||
case true:
|
||||
opts = append(opts, {{ .Pkg }}.WithRecursive(true))
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ if .HasScopeName }}
|
||||
switch c.FlagScopeName {
|
||||
case "":
|
||||
default:
|
||||
opts = append(opts, {{ .Pkg }}.WithScopeName(c.FlagScopeName))
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
var version uint32
|
||||
{{ if .VersionedActions }}
|
||||
switch c.Func {
|
||||
{{ range $i, $action := .VersionedActions }}
|
||||
case "{{ $action }}":
|
||||
switch c.FlagVersion {
|
||||
case 0:
|
||||
opts = append(opts, {{ $input.Pkg }}.WithAutomaticVersioning(true))
|
||||
default:
|
||||
version = uint32(c.FlagVersion)
|
||||
}
|
||||
{{ end }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
if ret := extra{{ camelCase .SubActionPrefix }}FlagsHandlingFunc(c, &opts); ret != 0 {
|
||||
return ret
|
||||
}
|
||||
|
||||
{{ if hasAction .StdActions "delete" }}
|
||||
existed := true
|
||||
{{ end }}
|
||||
var result api.GenericResult
|
||||
{{ if hasAction .StdActions "list" }}
|
||||
var listResult api.GenericListResult
|
||||
{{ end }}
|
||||
|
||||
switch c.Func {
|
||||
{{ range $i, $action := $input.StdActions }}
|
||||
{{ if eq $action "create" }}
|
||||
case "create":
|
||||
result, err = {{ $input.Pkg }}Client.Create(c.Context, {{ if (and $input.SubActionPrefix $input.NeedsSubTypeInCreate) }}"{{ $input.SubActionPrefix }}",{{ end }} c.Flag{{ $input.Container }}Id, opts...)
|
||||
{{ end }}
|
||||
{{ if eq $action "read" }}
|
||||
case "read":
|
||||
result, err = {{ $input.Pkg }}Client.Read(c.Context, c.FlagId, opts...)
|
||||
{{ end }}
|
||||
{{ if eq $action "update" }}
|
||||
case "update":
|
||||
result, err = {{ $input.Pkg }}Client.Update(c.Context, c.FlagId, version, opts...)
|
||||
{{ end }}
|
||||
{{ if eq $action "delete" }}
|
||||
case "delete":
|
||||
_, err = {{ $input.Pkg}}Client.Delete(c.Context, c.FlagId, opts...)
|
||||
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
|
||||
existed = false
|
||||
err = nil
|
||||
}
|
||||
{{ end }}
|
||||
{{ if eq $action "list" }}
|
||||
case "list":
|
||||
listResult, err = {{ $input.Pkg}}Client.List(c.Context, c.Flag{{ $input.Container }}Id, opts...)
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
result, err = executeExtra{{ camelCase .SubActionPrefix }}Actions(c, result, err, {{ .Pkg }}Client, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, c.plural, base.PrintApiError(apiErr)))
|
||||
return 1
|
||||
}
|
||||
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
output, err := printCustom{{ camelCase .SubActionPrefix }}ActionOutput(c)
|
||||
if err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
if output {
|
||||
return 0
|
||||
}
|
||||
|
||||
switch c.Func {
|
||||
{{ range $i, $action := .StdActions }}
|
||||
{{ if eq $action "delete" }}
|
||||
case "delete":
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
c.UI.Output(fmt.Sprintf("{ \"existed\": %t }", existed))
|
||||
|
||||
case "table":
|
||||
output := "The delete operation completed successfully"
|
||||
switch existed {
|
||||
case true:
|
||||
output += "."
|
||||
default:
|
||||
output += ", however the resource did not exist at the time."
|
||||
}
|
||||
c.UI.Output(output)
|
||||
}
|
||||
|
||||
return 0
|
||||
{{ end }}
|
||||
{{ if eq $action "list" }}
|
||||
case "list":
|
||||
listedItems := listResult.GetItems().([]*{{ $input.Pkg }}.{{ camelCase $input.ResourceType }})
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
switch {
|
||||
|
||||
case len(listedItems) == 0:
|
||||
c.UI.Output("null")
|
||||
|
||||
default:
|
||||
b, err := base.JsonFormatter{}.Format(listedItems)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
c.UI.Output(string(b))
|
||||
}
|
||||
|
||||
case "table":
|
||||
c.UI.Output(c.printListTable(listedItems))
|
||||
}
|
||||
|
||||
return 0
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
item := result.GetItem().(*{{ .Pkg }}.{{ camelCase .ResourceType }})
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(item))
|
||||
|
||||
case "json":
|
||||
b, err := base.JsonFormatter{}.Format(item)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
c.UI.Output(string(b))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
var (
|
||||
{{ if not .SubActionPrefix }}
|
||||
flagsOnce = new(sync.Once)
|
||||
{{ end }}
|
||||
extra{{ camelCase .SubActionPrefix }}ActionsFlagsMapFunc = func() map[string][]string { return nil }
|
||||
extra{{ camelCase .SubActionPrefix }}SynopsisFunc = func(*{{ camelCase .SubActionPrefix }}Command) string { return "" }
|
||||
extra{{ camelCase .SubActionPrefix }}FlagsFunc = func(*{{ camelCase .SubActionPrefix }}Command, *base.FlagSets, *base.FlagSet) {}
|
||||
extra{{ camelCase .SubActionPrefix }}FlagsHandlingFunc = func(*{{ camelCase .SubActionPrefix }}Command, *[]{{ .Pkg }}.Option) int { return 0 }
|
||||
executeExtra{{ camelCase .SubActionPrefix }}Actions = func(_ *{{ camelCase .SubActionPrefix }}Command, inResult api.GenericResult, inErr error, _ *{{ .Pkg }}.Client, _ uint32, _ []{{ .Pkg }}.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
}
|
||||
printCustom{{ camelCase .SubActionPrefix }}ActionOutput = func(*{{ camelCase .SubActionPrefix }}Command) (bool, error) { return false, nil }
|
||||
)
|
||||
`))
|
||||
Reference in new issue
Block a user