github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/objects/call/calls.go (about) 1 package call 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "os" 9 "strings" 10 "time" 11 12 apps "github.com/fnproject/cli/objects/app" 13 fns "github.com/fnproject/cli/objects/fn" 14 fnclient "github.com/fnproject/fn_go/clientv2" 15 apicall "github.com/fnproject/fn_go/clientv2/call" 16 "github.com/fnproject/fn_go/modelsv2" 17 "github.com/go-openapi/strfmt" 18 "github.com/urfave/cli" 19 ) 20 21 type callsCmd struct { 22 client *fnclient.Fn 23 } 24 25 // getMarshalableCall returns a call struct that we can marshal to JSON and output 26 func getMarshalableCall(call *modelsv2.Call) interface{} { 27 if call.Error != "" { 28 return struct { 29 ID string `json:"id"` 30 AppID string `json:"appId"` 31 FnID string `json:"fnId"` 32 CreatedAt strfmt.DateTime `json:"createdAt"` 33 StartedAt strfmt.DateTime `json:"startedAt"` 34 CompletedAt strfmt.DateTime `json:"completedAt"` 35 Status string `json:"status"` 36 ErrorReason string `json:"errorReason"` 37 }{ 38 call.ID, 39 call.AppID, 40 call.FnID, 41 call.CreatedAt, 42 call.StartedAt, 43 call.CompletedAt, 44 call.Status, 45 call.Error, 46 } 47 } 48 49 return struct { 50 ID string `json:"id"` 51 AppID string `json:"appId"` 52 FnID string `json:"fnId"` 53 CreatedAt strfmt.DateTime `json:"createdAt"` 54 StartedAt strfmt.DateTime `json:"startedAt"` 55 CompletedAt strfmt.DateTime `json:"completedAt"` 56 Status string `json:"status"` 57 }{ 58 call.ID, 59 call.AppID, 60 call.FnID, 61 call.CreatedAt, 62 call.StartedAt, 63 call.CompletedAt, 64 call.Status, 65 } 66 } 67 68 func printCalls(c *cli.Context, calls []*modelsv2.Call) error { 69 outputFormat := strings.ToLower(c.String("output")) 70 if outputFormat == "json" { 71 var allCalls []interface{} 72 for _, call := range calls { 73 c := getMarshalableCall(call) 74 allCalls = append(allCalls, c) 75 } 76 b, err := json.MarshalIndent(allCalls, "", " ") 77 if err != nil { 78 return err 79 } 80 fmt.Fprint(os.Stdout, string(b)) 81 } else { 82 for _, call := range calls { 83 fmt.Println(fmt.Sprintf( 84 "ID: %v\n"+ 85 "App Id: %v\n"+ 86 "Fn Id: %v\n"+ 87 "Created At: %v\n"+ 88 "Started At: %v\n"+ 89 "Completed At: %v\n"+ 90 "Status: %v\n", 91 call.ID, call.AppID, call.FnID, call.CreatedAt, 92 call.StartedAt, call.CompletedAt, call.Status)) 93 if call.Error != "" { 94 fmt.Println(fmt.Sprintf("Error reason: %v\n", call.Error)) 95 } 96 } 97 } 98 return nil 99 } 100 101 func (c *callsCmd) get(ctx *cli.Context) error { 102 appName, fnName, callID := ctx.Args().Get(0), ctx.Args().Get(1), ctx.Args().Get(2) 103 104 app, err := apps.GetAppByName(c.client, appName) 105 if err != nil { 106 return err 107 } 108 fn, err := fns.GetFnByName(c.client, app.ID, fnName) 109 if err != nil { 110 return err 111 } 112 params := apicall.GetFnsFnIDCallsCallIDParams{ 113 CallID: callID, 114 FnID: fn.ID, 115 Context: context.Background(), 116 } 117 resp, err := c.client.Call.GetFnsFnIDCallsCallID(¶ms) 118 if err != nil { 119 switch e := err.(type) { 120 case *apicall.GetFnsFnIDCallsCallIDNotFound: 121 return errors.New(e.Payload.Message) 122 default: 123 return err 124 } 125 } 126 printCalls(ctx, []*modelsv2.Call{resp.Payload}) 127 return nil 128 } 129 130 func (c *callsCmd) list(ctx *cli.Context) error { 131 appName, fnName := ctx.Args().Get(0), ctx.Args().Get(1) 132 133 app, err := apps.GetAppByName(c.client, appName) 134 if err != nil { 135 return err 136 } 137 fn, err := fns.GetFnByName(c.client, app.ID, fnName) 138 if err != nil { 139 return err 140 } 141 params := apicall.GetFnsFnIDCallsParams{ 142 FnID: fn.ID, 143 Context: context.Background(), 144 } 145 if ctx.String("cursor") != "" { 146 cursor := ctx.String("cursor") 147 params.Cursor = &cursor 148 } 149 if ctx.String("from-time") != "" { 150 fromTime := ctx.String("from-time") 151 fromTimeInt64, err := time.Parse(time.RFC3339, fromTime) 152 if err != nil { 153 return err 154 } 155 res := fromTimeInt64.Unix() 156 params.FromTime = &res 157 158 } 159 160 if ctx.String("to-time") != "" { 161 toTime := ctx.String("to-time") 162 toTimeInt64, err := time.Parse(time.RFC3339, toTime) 163 if err != nil { 164 return err 165 } 166 res := toTimeInt64.Unix() 167 params.ToTime = &res 168 } 169 170 n := ctx.Int64("n") 171 if n < 0 { 172 return errors.New("Number of calls: negative value not allowed") 173 } 174 175 var resCalls []*modelsv2.Call 176 for { 177 resp, err := c.client.Call.GetFnsFnIDCalls(¶ms) 178 if err != nil { 179 switch e := err.(type) { 180 case *apicall.GetFnsFnIDCallsNotFound: 181 return errors.New(e.Payload.Message) 182 default: 183 return err 184 } 185 } 186 187 resCalls = append(resCalls, resp.Payload.Items...) 188 howManyMore := n - int64(len(resCalls)+len(resp.Payload.Items)) 189 if howManyMore <= 0 || resp.Payload.NextCursor == "" { 190 break 191 } 192 193 params.Cursor = &resp.Payload.NextCursor 194 } 195 196 printCalls(ctx, resCalls) 197 return nil 198 }