From e7a817326601ef16f1adcd3cd5896e2a12d6e87f Mon Sep 17 00:00:00 2001 From: Owen Ou Date: Mon, 14 Feb 2022 14:57:00 -0800 Subject: [PATCH] Add `meroxa functions log` Add `meroxa functions log`. This is part of https://github.com/meroxa/cli/issues/244. --- cmd/meroxa/root/functions/functions.go | 1 + cmd/meroxa/root/functions/logs.go | 80 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- .../meroxa/meroxa-go/pkg/meroxa/log.go | 20 ++++- .../meroxa/meroxa-go/pkg/meroxa/meroxa.go | 1 + vendor/modules.txt | 2 +- 7 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 cmd/meroxa/root/functions/logs.go diff --git a/cmd/meroxa/root/functions/functions.go b/cmd/meroxa/root/functions/functions.go index 8caf985d5..36d7a52bf 100644 --- a/cmd/meroxa/root/functions/functions.go +++ b/cmd/meroxa/root/functions/functions.go @@ -45,5 +45,6 @@ func (*Functions) SubCommands() []*cobra.Command { builder.BuildCobraCommand(&List{}), builder.BuildCobraCommand(&Describe{}), builder.BuildCobraCommand(&Remove{}), + builder.BuildCobraCommand(&Logs{}), } } diff --git a/cmd/meroxa/root/functions/logs.go b/cmd/meroxa/root/functions/logs.go new file mode 100644 index 000000000..802bfaebd --- /dev/null +++ b/cmd/meroxa/root/functions/logs.go @@ -0,0 +1,80 @@ +package functions + +import ( + "bytes" + "context" + "errors" + "net/http" + + "github.com/meroxa/cli/cmd/meroxa/builder" + "github.com/meroxa/cli/log" + "github.com/meroxa/meroxa-go/pkg/meroxa" +) + +var ( + _ builder.CommandWithDocs = (*Logs)(nil) + _ builder.CommandWithArgs = (*Logs)(nil) + _ builder.CommandWithClient = (*Logs)(nil) + _ builder.CommandWithLogger = (*Logs)(nil) + _ builder.CommandWithExecute = (*Logs)(nil) +) + +type functionLogsClient interface { + GetFunctionLogs(ctx context.Context, nameOrID string) (*http.Response, error) +} + +type Logs struct { + client functionLogsClient + logger log.Logger + + args struct { + NameOrID string + } +} + +func (l *Logs) Usage() string { + return "logs NAME" +} + +func (l *Logs) Docs() builder.Docs { + return builder.Docs{ + Short: "Print logs for a function", + } +} + +func (l *Logs) Execute(ctx context.Context) error { + resp, err := l.client.GetFunctionLogs(ctx, l.args.NameOrID) + + if err != nil { + return err + } + defer resp.Body.Close() + + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(resp.Body) + + if err != nil { + return err + } + + l.logger.Info(ctx, buf.String()) + + return nil +} + +func (l *Logs) Logger(logger log.Logger) { + l.logger = logger +} + +func (l *Logs) Client(client meroxa.Client) { + l.client = client +} + +func (l *Logs) ParseArgs(args []string) error { + if len(args) < 1 { + return errors.New("requires function name") + } + + l.args.NameOrID = args[0] + return nil +} diff --git a/go.mod b/go.mod index a504f2c0a..f55c5dcd9 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/manifoldco/promptui v0.8.0 github.com/mattn/go-colorable v0.1.8 // indirect github.com/mattn/go-runewidth v0.0.10 // indirect - github.com/meroxa/meroxa-go v0.0.0-20220208195203-71ddc3133fab + github.com/meroxa/meroxa-go v0.0.0-20220214221023-2da2e3a247a7 github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 github.com/rivo/uniseg v0.2.0 // indirect diff --git a/go.sum b/go.sum index a972848da..2576d876d 100644 --- a/go.sum +++ b/go.sum @@ -225,8 +225,8 @@ github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRR github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= -github.com/meroxa/meroxa-go v0.0.0-20220208195203-71ddc3133fab h1:EzZzmvjyU+U/8ecHt0QmKbJQs4293wms3EeapctnBME= -github.com/meroxa/meroxa-go v0.0.0-20220208195203-71ddc3133fab/go.mod h1:HDFszURCM1cOpKE699o5Hs0T2tEIXqY+vFcsur3RiwY= +github.com/meroxa/meroxa-go v0.0.0-20220214221023-2da2e3a247a7 h1:o8QhC5o0bb6cUQN0NdYBT8AkitftZWsBZRF1Y9kXtgI= +github.com/meroxa/meroxa-go v0.0.0-20220214221023-2da2e3a247a7/go.mod h1:HDFszURCM1cOpKE699o5Hs0T2tEIXqY+vFcsur3RiwY= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= diff --git a/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/log.go b/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/log.go index ec7334ce9..6525df87c 100644 --- a/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/log.go +++ b/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/log.go @@ -6,7 +6,10 @@ import ( "net/http" ) -const connectorLogsBasePath = "/v1/connectors" +const ( + connectorLogsBasePath = "/v1/connectors" + functionLogsBasePath = "/v1/functions" +) func (c *client) GetConnectorLogs(ctx context.Context, nameOrID string) (*http.Response, error) { path := fmt.Sprintf("%s/%s/logs", connectorLogsBasePath, nameOrID) @@ -22,3 +25,18 @@ func (c *client) GetConnectorLogs(ctx context.Context, nameOrID string) (*http.R return c.httpClient.Do(req) } + +func (c *client) GetFunctionLogs(ctx context.Context, nameOrID string) (*http.Response, error) { + path := fmt.Sprintf("%s/%s/logs", functionLogsBasePath, nameOrID) + + req, err := c.newRequest(ctx, http.MethodGet, path, nil, nil) + if err != nil { + return nil, err + } + + // Override content-type and accept headers to text/palin + req.Header.Add("Content-Type", textContentType) + req.Header.Add("Accept", textContentType) + + return c.httpClient.Do(req) +} diff --git a/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/meroxa.go b/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/meroxa.go index 20a5be55d..e52d17636 100644 --- a/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/meroxa.go +++ b/vendor/github.com/meroxa/meroxa-go/pkg/meroxa/meroxa.go @@ -43,6 +43,7 @@ type Client interface { CreateFunction(ctx context.Context, input *CreateFunctionInput) (*Function, error) GetFunction(ctx context.Context, nameOrUUID string) (*Function, error) + GetFunctionLogs(ctx context.Context, nameOrID string) (*http.Response, error) ListFunctions(ctx context.Context) ([]*Function, error) DeleteFunction(ctx context.Context, nameOrUUID string) (*Function, error) diff --git a/vendor/modules.txt b/vendor/modules.txt index 0abb22f4b..53da5710e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -87,7 +87,7 @@ github.com/mattn/go-runewidth # github.com/mattn/go-shellwords v1.0.12 ## explicit; go 1.13 github.com/mattn/go-shellwords -# github.com/meroxa/meroxa-go v0.0.0-20220208195203-71ddc3133fab +# github.com/meroxa/meroxa-go v0.0.0-20220214221023-2da2e3a247a7 ## explicit; go 1.17 github.com/meroxa/meroxa-go/pkg/meroxa github.com/meroxa/meroxa-go/pkg/mock