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
@@ -101,8 +101,7 @@ func (c *Command) printListTable(items []*workers.Worker) string {
|
||||
return base.WrapForHelpText(output)
|
||||
}
|
||||
|
||||
func printItemTable(result api.GenericResult) string {
|
||||
item := result.GetItem().(*workers.Worker)
|
||||
func printItemTable(item *workers.Worker, resp *api.Response) string {
|
||||
nonAttributeMap := map[string]interface{}{}
|
||||
if item.Id != "" {
|
||||
nonAttributeMap["ID"] = item.Id
|
||||
@@ -132,7 +131,7 @@ func printItemTable(result api.GenericResult) string {
|
||||
nonAttributeMap["Last Status Time"] = item.LastStatusTime
|
||||
}
|
||||
|
||||
resultMap := result.GetResponse().Map
|
||||
resultMap := resp.Map
|
||||
if count, ok := resultMap[globals.ActiveConnectionCountField]; ok {
|
||||
nonAttributeMap["Active Connection Count"] = count
|
||||
}
|
||||
|
||||
@@ -54,10 +54,11 @@ func extraWorkerLedFlagsFuncImpl(c *WorkerLedCommand, set *base.FlagSets, f *bas
|
||||
}
|
||||
}
|
||||
|
||||
func executeExtraWorkerLedActionsImpl(c *WorkerLedCommand, origResult api.GenericResult, origError error, workerClient *workers.Client, version uint32, opts []workers.Option) (api.GenericResult, error) {
|
||||
func executeExtraWorkerLedActionsImpl(c *WorkerLedCommand, origResp *api.Response, origItem *workers.Worker, origError error, workerClient *workers.Client, version uint32, opts []workers.Option) (*api.Response, *workers.Worker, error) {
|
||||
switch c.Func {
|
||||
case "create":
|
||||
return workerClient.CreateWorkerLed(c.Context, c.flagWorkerGeneratedAuthToken, c.FlagScopeId, opts...)
|
||||
result, err := workerClient.CreateWorkerLed(c.Context, c.flagWorkerGeneratedAuthToken, c.FlagScopeId, opts...)
|
||||
return result.GetResponse(), result.GetItem(), err
|
||||
}
|
||||
return origResult, origError
|
||||
return origResp, origItem, origError
|
||||
}
|
||||
@@ -187,13 +187,14 @@ func (c *WorkerLedCommand) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *workers.Worker
|
||||
|
||||
switch c.Func {
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraWorkerLedActions(c, result, err, workersClient, version, opts)
|
||||
resp, item, err = executeExtraWorkerLedActions(c, resp, item, err, workersClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -221,10 +222,10 @@ func (c *WorkerLedCommand) 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
|
||||
}
|
||||
}
|
||||
@@ -237,8 +238,8 @@ var (
|
||||
extraWorkerLedSynopsisFunc = func(*WorkerLedCommand) string { return "" }
|
||||
extraWorkerLedFlagsFunc = func(*WorkerLedCommand, *base.FlagSets, *base.FlagSet) {}
|
||||
extraWorkerLedFlagsHandlingFunc = func(*WorkerLedCommand, *base.FlagSets, *[]workers.Option) bool { return true }
|
||||
executeExtraWorkerLedActions = func(_ *WorkerLedCommand, inResult api.GenericResult, inErr error, _ *workers.Client, _ uint32, _ []workers.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraWorkerLedActions = func(_ *WorkerLedCommand, inResp *api.Response, inItem *workers.Worker, inErr error, _ *workers.Client, _ uint32, _ []workers.Option) (*api.Response, *workers.Worker, error) {
|
||||
return inResp, inItem, inErr
|
||||
}
|
||||
printCustomWorkerLedActionOutput = func(*WorkerLedCommand) (bool, error) { return false, nil }
|
||||
)
|
||||
@@ -214,27 +214,43 @@ func (c *Command) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *workers.Worker
|
||||
|
||||
var listResult api.GenericListResult
|
||||
var items []*workers.Worker
|
||||
|
||||
var readResult *workers.WorkerReadResult
|
||||
|
||||
var updateResult *workers.WorkerUpdateResult
|
||||
|
||||
var deleteResult *workers.WorkerDeleteResult
|
||||
|
||||
var listResult *workers.WorkerListResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "read":
|
||||
result, err = workersClient.Read(c.Context, c.FlagId, opts...)
|
||||
readResult, err = workersClient.Read(c.Context, c.FlagId, opts...)
|
||||
resp = readResult.GetResponse()
|
||||
item = readResult.GetItem()
|
||||
|
||||
case "update":
|
||||
result, err = workersClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
updateResult, err = workersClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
resp = updateResult.GetResponse()
|
||||
item = updateResult.GetItem()
|
||||
|
||||
case "delete":
|
||||
result, err = workersClient.Delete(c.Context, c.FlagId, opts...)
|
||||
deleteResult, err = workersClient.Delete(c.Context, c.FlagId, opts...)
|
||||
resp = deleteResult.GetResponse()
|
||||
|
||||
case "list":
|
||||
listResult, err = workersClient.List(c.Context, c.FlagScopeId, opts...)
|
||||
resp = listResult.GetResponse()
|
||||
items = listResult.GetItems()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraActions(c, result, err, workersClient, version, opts)
|
||||
resp, item, items, err = executeExtraActions(c, resp, item, items, err, workersClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -261,7 +277,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
|
||||
}
|
||||
|
||||
@@ -274,13 +290,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().([]*workers.Worker)
|
||||
c.UI.Output(c.printListTable(listedItems))
|
||||
c.UI.Output(c.printListTable(items))
|
||||
}
|
||||
|
||||
return base.CommandSuccess
|
||||
@@ -289,10 +304,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
|
||||
}
|
||||
}
|
||||
@@ -307,8 +322,8 @@ var (
|
||||
extraSynopsisFunc = func(*Command) string { return "" }
|
||||
extraFlagsFunc = func(*Command, *base.FlagSets, *base.FlagSet) {}
|
||||
extraFlagsHandlingFunc = func(*Command, *base.FlagSets, *[]workers.Option) bool { return true }
|
||||
executeExtraActions = func(_ *Command, inResult api.GenericResult, inErr error, _ *workers.Client, _ uint32, _ []workers.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraActions = func(_ *Command, inResp *api.Response, inItem *workers.Worker, inItems []*workers.Worker, inErr error, _ *workers.Client, _ uint32, _ []workers.Option) (*api.Response, *workers.Worker, []*workers.Worker, error) {
|
||||
return inResp, inItem, inItems, inErr
|
||||
}
|
||||
printCustomActionOutput = func(*Command) (bool, error) { return false, nil }
|
||||
)
|
||||
Reference in new issue
Block a user