refactor(api module): enforce typed definitions (#2238)
This commit is contained in:
1 parent
d6c1402223
commit
c2bfcc0664
79 files changed
+906
-551
No files matched your search
@@ -160,16 +160,19 @@ func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, opts *[]hostsets.O
|
||||
return true
|
||||
}
|
||||
|
||||
func executeExtraActionsImpl(c *Command, origResult api.GenericResult, origError error, hostsetClient *hostsets.Client, version uint32, opts []hostsets.Option) (api.GenericResult, error) {
|
||||
func executeExtraActionsImpl(c *Command, origResp *api.Response, origItem *hostsets.HostSet, origItems []*hostsets.HostSet, origError error, hostsetClient *hostsets.Client, version uint32, opts []hostsets.Option) (*api.Response, *hostsets.HostSet, []*hostsets.HostSet, error) {
|
||||
switch c.Func {
|
||||
case "add-hosts":
|
||||
return hostsetClient.AddHosts(c.Context, c.FlagId, version, c.flagHosts, opts...)
|
||||
result, err := hostsetClient.AddHosts(c.Context, c.FlagId, version, c.flagHosts, opts...)
|
||||
return result.GetResponse(), result.GetItem(), nil, err
|
||||
case "remove-hosts":
|
||||
return hostsetClient.RemoveHosts(c.Context, c.FlagId, version, c.flagHosts, opts...)
|
||||
result, err := hostsetClient.RemoveHosts(c.Context, c.FlagId, version, c.flagHosts, opts...)
|
||||
return result.GetResponse(), result.GetItem(), nil, err
|
||||
case "set-hosts":
|
||||
return hostsetClient.SetHosts(c.Context, c.FlagId, version, c.flagHosts, opts...)
|
||||
result, err := hostsetClient.SetHosts(c.Context, c.FlagId, version, c.flagHosts, opts...)
|
||||
return result.GetResponse(), result.GetItem(), nil, err
|
||||
}
|
||||
return origResult, origError
|
||||
return origResp, origItem, origItems, origError
|
||||
}
|
||||
|
||||
func (c *Command) printListTable(items []*hostsets.HostSet) string {
|
||||
@@ -231,8 +234,7 @@ func (c *Command) printListTable(items []*hostsets.HostSet) string {
|
||||
return base.WrapForHelpText(output)
|
||||
}
|
||||
|
||||
func printItemTable(result api.GenericResult) string {
|
||||
item := result.GetItem().(*hostsets.HostSet)
|
||||
func printItemTable(item *hostsets.HostSet, resp *api.Response) string {
|
||||
nonAttributeMap := map[string]interface{}{}
|
||||
if item.Id != "" {
|
||||
nonAttributeMap["ID"] = item.Id
|
||||
|
||||
@@ -225,24 +225,36 @@ func (c *Command) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *hostsets.HostSet
|
||||
|
||||
var listResult api.GenericListResult
|
||||
var items []*hostsets.HostSet
|
||||
|
||||
var readResult *hostsets.HostSetReadResult
|
||||
|
||||
var deleteResult *hostsets.HostSetDeleteResult
|
||||
|
||||
var listResult *hostsets.HostSetListResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "read":
|
||||
result, err = hostsetsClient.Read(c.Context, c.FlagId, opts...)
|
||||
readResult, err = hostsetsClient.Read(c.Context, c.FlagId, opts...)
|
||||
resp = readResult.GetResponse()
|
||||
item = readResult.GetItem()
|
||||
|
||||
case "delete":
|
||||
result, err = hostsetsClient.Delete(c.Context, c.FlagId, opts...)
|
||||
deleteResult, err = hostsetsClient.Delete(c.Context, c.FlagId, opts...)
|
||||
resp = deleteResult.GetResponse()
|
||||
|
||||
case "list":
|
||||
listResult, err = hostsetsClient.List(c.Context, c.FlagHostCatalogId, opts...)
|
||||
resp = listResult.GetResponse()
|
||||
items = listResult.GetItems()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraActions(c, result, err, hostsetsClient, version, opts)
|
||||
resp, item, items, err = executeExtraActions(c, resp, item, items, err, hostsetsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -269,7 +281,7 @@ func (c *Command) Run(args []string) int {
|
||||
case "delete":
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
@@ -282,13 +294,12 @@ func (c *Command) Run(args []string) int {
|
||||
case "list":
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
if ok := c.PrintJsonItems(listResult); !ok {
|
||||
if ok := c.PrintJsonItems(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
case "table":
|
||||
listedItems := listResult.GetItems().([]*hostsets.HostSet)
|
||||
c.UI.Output(c.printListTable(listedItems))
|
||||
c.UI.Output(c.printListTable(items))
|
||||
}
|
||||
|
||||
return base.CommandSuccess
|
||||
@@ -297,10 +308,10 @@ func (c *Command) Run(args []string) int {
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
c.UI.Output(printItemTable(item, resp))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
@@ -315,8 +326,8 @@ var (
|
||||
extraSynopsisFunc = func(*Command) string { return "" }
|
||||
extraFlagsFunc = func(*Command, *base.FlagSets, *base.FlagSet) {}
|
||||
extraFlagsHandlingFunc = func(*Command, *base.FlagSets, *[]hostsets.Option) bool { return true }
|
||||
executeExtraActions = func(_ *Command, inResult api.GenericResult, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraActions = func(_ *Command, inResp *api.Response, inItem *hostsets.HostSet, inItems []*hostsets.HostSet, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (*api.Response, *hostsets.HostSet, []*hostsets.HostSet, error) {
|
||||
return inResp, inItem, inItems, inErr
|
||||
}
|
||||
printCustomActionOutput = func(*Command) (bool, error) { return false, nil }
|
||||
)
|
||||
@@ -211,19 +211,28 @@ func (c *PluginCommand) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *hostsets.HostSet
|
||||
|
||||
var createResult *hostsets.HostSetCreateResult
|
||||
|
||||
var updateResult *hostsets.HostSetUpdateResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
result, err = hostsetsClient.Create(c.Context, c.FlagHostCatalogId, opts...)
|
||||
createResult, err = hostsetsClient.Create(c.Context, c.FlagHostCatalogId, opts...)
|
||||
resp = createResult.GetResponse()
|
||||
item = createResult.GetItem()
|
||||
|
||||
case "update":
|
||||
result, err = hostsetsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
updateResult, err = hostsetsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
resp = updateResult.GetResponse()
|
||||
item = updateResult.GetItem()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraPluginActions(c, result, err, hostsetsClient, version, opts)
|
||||
resp, item, err = executeExtraPluginActions(c, resp, item, err, hostsetsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -251,10 +260,10 @@ func (c *PluginCommand) Run(args []string) int {
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
c.UI.Output(printItemTable(item, resp))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
@@ -267,8 +276,8 @@ var (
|
||||
extraPluginSynopsisFunc = func(*PluginCommand) string { return "" }
|
||||
extraPluginFlagsFunc = func(*PluginCommand, *base.FlagSets, *base.FlagSet) {}
|
||||
extraPluginFlagsHandlingFunc = func(*PluginCommand, *base.FlagSets, *[]hostsets.Option) bool { return true }
|
||||
executeExtraPluginActions = func(_ *PluginCommand, inResult api.GenericResult, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraPluginActions = func(_ *PluginCommand, inResp *api.Response, inItem *hostsets.HostSet, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (*api.Response, *hostsets.HostSet, error) {
|
||||
return inResp, inItem, inErr
|
||||
}
|
||||
printCustomPluginActionOutput = func(*PluginCommand) (bool, error) { return false, nil }
|
||||
)
|
||||
@@ -191,19 +191,28 @@ func (c *StaticCommand) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *hostsets.HostSet
|
||||
|
||||
var createResult *hostsets.HostSetCreateResult
|
||||
|
||||
var updateResult *hostsets.HostSetUpdateResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
result, err = hostsetsClient.Create(c.Context, c.FlagHostCatalogId, opts...)
|
||||
createResult, err = hostsetsClient.Create(c.Context, c.FlagHostCatalogId, opts...)
|
||||
resp = createResult.GetResponse()
|
||||
item = createResult.GetItem()
|
||||
|
||||
case "update":
|
||||
result, err = hostsetsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
updateResult, err = hostsetsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
resp = updateResult.GetResponse()
|
||||
item = updateResult.GetItem()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraStaticActions(c, result, err, hostsetsClient, version, opts)
|
||||
resp, item, err = executeExtraStaticActions(c, resp, item, err, hostsetsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -231,10 +240,10 @@ func (c *StaticCommand) Run(args []string) int {
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
c.UI.Output(printItemTable(item, resp))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
@@ -247,8 +256,8 @@ var (
|
||||
extraStaticSynopsisFunc = func(*StaticCommand) string { return "" }
|
||||
extraStaticFlagsFunc = func(*StaticCommand, *base.FlagSets, *base.FlagSet) {}
|
||||
extraStaticFlagsHandlingFunc = func(*StaticCommand, *base.FlagSets, *[]hostsets.Option) bool { return true }
|
||||
executeExtraStaticActions = func(_ *StaticCommand, inResult api.GenericResult, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraStaticActions = func(_ *StaticCommand, inResp *api.Response, inItem *hostsets.HostSet, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (*api.Response, *hostsets.HostSet, error) {
|
||||
return inResp, inItem, inErr
|
||||
}
|
||||
printCustomStaticActionOutput = func(*StaticCommand) (bool, error) { return false, nil }
|
||||
)
|
||||
Reference in new issue
Block a user