Files
fn-serverless/fn/calls.go
Denis Makogon 52137e3b61 Update fn tool
2017-06-08 11:46:56 -07:00

97 lines
2.2 KiB
Go

package main
import (
"fmt"
"context"
"github.com/funcy/functions_go/models"
fnclient "github.com/funcy/functions_go/client"
apicall "github.com/funcy/functions_go/client/call"
"github.com/urfave/cli"
)
type callsCmd struct {
client *fnclient.Functions
}
func calls() cli.Command {
c := callsCmd{client: apiClient()}
return cli.Command{
Name: "calls",
Usage: "manage function calls",
Subcommands: []cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: "get function call info",
ArgsUsage: "<call-id>",
Action: c.get,
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all calls for specific route",
ArgsUsage: "<app> <route>",
Action: c.list,
},
},
}
}
func printCalls(calls []*models.Call) {
for _, call := range calls{
fmt.Println(fmt.Sprintf(
"ID: %v\n" +
"App: %v\n" +
"Route: %v\n" +
"Created At: %v\n" +
"Started At: %v\n" +
"Completed At: %v\n" +
"Status: %v\n",
call.ID, call.AppName, call.Path, call.CreatedAt,
call.StartedAt, call.CompletedAt, call.Status))
}
}
func (call *callsCmd) get(ctx *cli.Context) error {
call_id := ctx.Args().Get(0)
params := apicall.GetCallsCallParams{
Call: call_id,
Context: context.Background(),
}
resp, err := call.client.Call.GetCallsCall(&params)
if err != nil {
switch err.(type) {
case *apicall.GetCallsCallNotFound:
return fmt.Errorf("error: %v", err.(
*apicall.GetCallsCallNotFound).Payload.Error.Message)
}
return fmt.Errorf("unexpected error: %v", err)
}
printCalls([]*models.Call{resp.Payload.Call})
return nil
}
func (call *callsCmd) list(ctx *cli.Context) error {
app, route := ctx.Args().Get(0), ctx.Args().Get(1)
params := apicall.GetAppsAppCallsRouteParams{
App: app,
Route: route,
Context: context.Background(),
}
resp, err := call.client.Call.GetAppsAppCallsRoute(&params)
if err != nil {
switch err.(type) {
case *apicall.GetCallsCallNotFound:
return fmt.Errorf("error: %v", err.(
*apicall.GetCallsCallNotFound).Payload.Error.Message)
}
return fmt.Errorf("unexpected error: %v", err)
}
printCalls(resp.Payload.Calls)
return nil
}