github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/objects/log/logs.go (about) 1 package log 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 8 apps "github.com/fnproject/cli/objects/app" 9 fns "github.com/fnproject/cli/objects/fn" 10 fnclient "github.com/fnproject/fn_go/clientv2" 11 ccall "github.com/fnproject/fn_go/clientv2/call" 12 apicall "github.com/fnproject/fn_go/clientv2/operations" 13 "github.com/urfave/cli" 14 ) 15 16 type logsCmd struct { 17 client *fnclient.Fn 18 } 19 20 func (l *logsCmd) get(ctx *cli.Context) error { 21 appName, fnName, callID := ctx.Args().Get(0), ctx.Args().Get(1), ctx.Args().Get(2) 22 23 app, err := apps.GetAppByName(l.client, appName) 24 if err != nil { 25 return err 26 } 27 fn, err := fns.GetFnByName(l.client, app.ID, fnName) 28 if err != nil { 29 return nil 30 } 31 32 if callID == "last" || callID == "l" { 33 params := ccall.GetFnsFnIDCallsParams{ 34 FnID: fn.ID, 35 Context: context.Background(), 36 } 37 resp, err := l.client.Call.GetFnsFnIDCalls(¶ms) 38 if err != nil { 39 switch e := err.(type) { 40 case *ccall.GetFnsFnIDCallsNotFound: 41 return errors.New(e.Payload.Message) 42 default: 43 return err 44 } 45 } 46 calls := resp.Payload.Items 47 if len(calls) > 0 { 48 callID = calls[0].ID 49 } else { 50 return errors.New("no previous calls found") 51 } 52 } 53 params := apicall.GetCallLogsParams{ 54 CallID: callID, 55 FnID: fn.ID, 56 Context: context.Background(), 57 } 58 resp, err := l.client.Operations.GetCallLogs(¶ms) 59 if err != nil { 60 switch e := err.(type) { 61 case *apicall.GetCallLogsNotFound: 62 return fmt.Errorf("%v", e.Payload.Message) 63 default: 64 return err 65 } 66 } 67 fmt.Print(resp.Payload.Log) 68 return nil 69 }