From 8bebeb65a028be829cc922633e3949f05ffec700 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Wed, 23 Nov 2022 18:43:00 +0100 Subject: [PATCH] Add --last to tkn pac logs And make all the help messages uses it instead of tkn pr directly Signed-off-by: Chmouel Boudjnah --- docs/content/docs/guide/running.md | 2 +- pkg/cmd/tknpac/logs/logs.go | 23 +++++++++++++++++------ pkg/cmd/tknpac/logs/logs_test.go | 17 +++++++++++++++++ pkg/pipelineascode/pipelineascode.go | 2 +- pkg/reconciler/reconciler.go | 2 +- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/docs/content/docs/guide/running.md b/docs/content/docs/guide/running.md index c1c4f2c83..cfbf4935a 100644 --- a/docs/content/docs/guide/running.md +++ b/docs/content/docs/guide/running.md @@ -43,7 +43,7 @@ You can follow the execution of your pipeline with the [tkn](https://github.com/tektoncd/cli) cli : ```console -tkn pr logs -n my-pipeline-ci -Lf +tkn pac logs -n my-pipeline-ci -L ``` If you need to show another pipelinerun than the last one you diff --git a/pkg/cmd/tknpac/logs/logs.go b/pkg/cmd/tknpac/logs/logs.go index 5ab554c7d..bd028135a 100644 --- a/pkg/cmd/tknpac/logs/logs.go +++ b/pkg/cmd/tknpac/logs/logs.go @@ -35,11 +35,12 @@ tkn pac logs will get the logs of a PipelineRun belonging to a Repository. the PipelineRun needs to exist on the kubernetes cluster to be able to display the logs.` const ( - namespaceFlag = "namespace" - limitFlag = "limit" - tknPathFlag = "tkn-path" - defaultLimit = -1 - openWebBrowserFlag = "web" + namespaceFlag = "namespace" + limitFlag = "limit" + tknPathFlag = "tkn-path" + defaultLimit = -1 + openWebBrowserFlag = "web" + useLastPipelineRunFlag = "last" ) type logOption struct { @@ -51,6 +52,7 @@ type logOption struct { tknPath string limit int webBrowser bool + useLastPR bool } func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command { @@ -92,6 +94,11 @@ func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command { return err } + useLastPR, err := cmd.Flags().GetBool(useLastPipelineRunFlag) + if err != nil { + return err + } + tknPath, err := cmd.Flags().GetString(tknPathFlag) if err != nil { return err @@ -115,6 +122,7 @@ func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command { limit: limit, webBrowser: webBrowser, tknPath: tknPath, + useLastPR: useLastPR, } return log(ctx, lopts) }, @@ -134,6 +142,9 @@ func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command { cmd.Flags().BoolP( openWebBrowserFlag, "w", false, "Open Web browser to detected console instead of using tkn") + cmd.Flags().BoolP( + useLastPipelineRunFlag, "L", false, "show logs of the last PipelineRun") + cmd.Flags().IntP( limitFlag, "", defaultLimit, "Limit the number of PipelineRun to show (-1 is unlimited)") @@ -213,7 +224,7 @@ func log(ctx context.Context, lo *logOption) error { return fmt.Errorf("cannot detect pipelineruns belonging to repository: %s", repository.GetName()) } var replyString string - if len(allprs) == 1 { + if lo.useLastPR || len(allprs) == 1 { replyString = allprs[0] } else { if err := prompt.SurveyAskOne(&survey.Select{ diff --git a/pkg/cmd/tknpac/logs/logs_test.go b/pkg/cmd/tknpac/logs/logs_test.go index 805369151..0aa08007e 100644 --- a/pkg/cmd/tknpac/logs/logs_test.go +++ b/pkg/cmd/tknpac/logs/logs_test.go @@ -35,6 +35,7 @@ func TestLogs(t *testing.T) { currentNamespace string shift int pruns []*tektonv1beta1.PipelineRun + useLastPR bool }{ { name: "good/show logs", @@ -48,6 +49,21 @@ func TestLogs(t *testing.T) { }, 30), }, }, + { + name: "good/show logs", + wantErr: false, + repoName: "test", + currentNamespace: ns, + useLastPR: true, + pruns: []*tektonv1beta1.PipelineRun{ + tektontest.MakePRCompletion(cw, "test-pipeline", ns, completed, map[string]string{ + keys.Repository: "test", + }, 30), + tektontest.MakePRCompletion(cw, "test-pipeline2", ns, completed, map[string]string{ + keys.Repository: "test", + }, 30), + }, + }, { name: "bad/shift", wantErr: true, @@ -116,6 +132,7 @@ func TestLogs(t *testing.T) { limit: 1, tknPath: tknPath, ioStreams: io, + useLastPR: tt.useLastPR, } err = log(ctx, lopts) diff --git a/pkg/pipelineascode/pipelineascode.go b/pkg/pipelineascode/pipelineascode.go index 0e2047a26..450aceff6 100644 --- a/pkg/pipelineascode/pipelineascode.go +++ b/pkg/pipelineascode/pipelineascode.go @@ -26,7 +26,7 @@ const ( startingPipelineRunText = `Starting Pipelinerun %s in namespace %s

You can follow the execution on the [OpenShift console](%s) pipelinerun viewer or via the command line with : -
tkn pr logs -f -n %s %s` +
tkn pac logs -L -n %s %s` queuingPipelineRunText = `PipelineRun %s has been queued Queuing in namespace %s

` ) diff --git a/pkg/reconciler/reconciler.go b/pkg/reconciler/reconciler.go index 95f66a184..e18dbc74a 100644 --- a/pkg/reconciler/reconciler.go +++ b/pkg/reconciler/reconciler.go @@ -31,7 +31,7 @@ import ( const startingPipelineRunText = `Starting Pipelinerun %s in namespace %s

You can follow the execution on the [OpenShift console](%s) pipelinerun viewer or via the command line with : -
tkn pr logs -f -n %s %s` +
tkn pac logs -L -n %s %s` type Reconciler struct { run *params.Run