From 5b44f532050de34b6edc3d7a4cc0469c2682e05c Mon Sep 17 00:00:00 2001 From: 16yuki0702 Date: Mon, 17 Feb 2020 10:48:31 +0900 Subject: [PATCH] Refactoring log writer. related issue is https://github.com/tektoncd/cli/issues/708 taskrun's logwriter and pipelinerun's logwriter are almost same code. It is useful and readable moving these code into same package. --- pkg/cmd/pipelinerun/log_reader.go | 23 +++----- pkg/cmd/pipelinerun/logs.go | 3 +- pkg/cmd/taskrun/log_reader.go | 24 +++----- pkg/cmd/taskrun/log_writer.go | 59 ------------------- pkg/cmd/taskrun/logs.go | 3 +- pkg/helper/log/log.go | 28 +++++++++ .../log_writer.go => helper/log/writer.go} | 22 +++++-- 7 files changed, 65 insertions(+), 97 deletions(-) delete mode 100644 pkg/cmd/taskrun/log_writer.go create mode 100644 pkg/helper/log/log.go rename pkg/{cmd/pipelinerun/log_writer.go => helper/log/writer.go} (70%) diff --git a/pkg/cmd/pipelinerun/log_reader.go b/pkg/cmd/pipelinerun/log_reader.go index d3a7293ca..c16dacf37 100644 --- a/pkg/cmd/pipelinerun/log_reader.go +++ b/pkg/cmd/pipelinerun/log_reader.go @@ -22,6 +22,7 @@ import ( "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/cmd/taskrun" + "github.com/tektoncd/cli/pkg/helper/log" "github.com/tektoncd/cli/pkg/helper/pipelinerun" "github.com/tektoncd/cli/pkg/helper/pods/stream" trh "github.com/tektoncd/cli/pkg/helper/taskrun" @@ -42,15 +43,7 @@ type LogReader struct { Tasks []string } -// Log is the data gets written to the log channel -type Log struct { - Pipeline string - Task string - Step string - Log string -} - -func (lr *LogReader) Read() (<-chan Log, <-chan error, error) { +func (lr *LogReader) Read() (<-chan log.Log, <-chan error, error) { tkn := lr.Clients.Tekton pr, err := tkn.TektonV1alpha1().PipelineRuns(lr.Ns).Get(lr.Run, metav1.GetOptions{}) if err != nil { @@ -64,8 +57,8 @@ func (lr *LogReader) Read() (<-chan Log, <-chan error, error) { } -func (lr *LogReader) readLiveLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <-chan error, error) { - logC := make(chan Log) +func (lr *LogReader) readLiveLogs(pr *v1alpha1.PipelineRun) (<-chan log.Log, <-chan error, error) { + logC := make(chan log.Log) errC := make(chan error) go func() { @@ -103,7 +96,7 @@ func (lr *LogReader) readLiveLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <-chan return logC, errC, nil } -func (lr *LogReader) readAvailableLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <-chan error, error) { +func (lr *LogReader) readAvailableLogs(pr *v1alpha1.PipelineRun) (<-chan log.Log, <-chan error, error) { if err := lr.waitUntilAvailable(10); err != nil { return nil, nil, err } @@ -118,7 +111,7 @@ func (lr *LogReader) readAvailableLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <- ordered := trh.SortTasksBySpecOrder(pl.Spec.Tasks, pr.Status.TaskRuns) taskRuns := trh.Filter(ordered, lr.Tasks) - logC := make(chan Log) + logC := make(chan log.Log) errC := make(chan error) go func() { @@ -185,7 +178,7 @@ func (lr *LogReader) waitUntilAvailable(timeout time.Duration) error { } } -func pipeLogs(logC chan<- Log, errC chan<- error, tlr *taskrun.LogReader) { +func pipeLogs(logC chan<- log.Log, errC chan<- error, tlr *taskrun.LogReader) { tlogC, terrC, err := tlr.Read() if err != nil { errC <- err @@ -199,7 +192,7 @@ func pipeLogs(logC chan<- Log, errC chan<- error, tlr *taskrun.LogReader) { tlogC = nil continue } - logC <- Log{Task: l.Task, Step: l.Step, Log: l.Log} + logC <- log.Log{Task: l.Task, Step: l.Step, Log: l.Log} case e, ok := <-terrC: if !ok { diff --git a/pkg/cmd/pipelinerun/logs.go b/pkg/cmd/pipelinerun/logs.go index 377b32b5b..cf0cea594 100644 --- a/pkg/cmd/pipelinerun/logs.go +++ b/pkg/cmd/pipelinerun/logs.go @@ -20,6 +20,7 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" + "github.com/tektoncd/cli/pkg/helper/log" "github.com/tektoncd/cli/pkg/helper/options" prhelper "github.com/tektoncd/cli/pkg/helper/pipelinerun" "github.com/tektoncd/cli/pkg/helper/pods" @@ -108,7 +109,7 @@ func Run(opts *options.LogOptions) error { return err } - NewLogWriter().Write(opts.Stream, logC, errC) + log.NewLogWriter(log.LogTypePipeline).Write(opts.Stream, logC, errC) return nil } diff --git a/pkg/cmd/taskrun/log_reader.go b/pkg/cmd/taskrun/log_reader.go index b1deacfb9..d4f6bbcfd 100644 --- a/pkg/cmd/taskrun/log_reader.go +++ b/pkg/cmd/taskrun/log_reader.go @@ -21,6 +21,7 @@ import ( "github.com/pkg/errors" "github.com/tektoncd/cli/pkg/cli" + "github.com/tektoncd/cli/pkg/helper/log" "github.com/tektoncd/cli/pkg/helper/pods" "github.com/tektoncd/cli/pkg/helper/pods/stream" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" @@ -40,13 +41,6 @@ func (s *step) hasStarted() bool { return s.state.Waiting == nil } -//Log data to write on log channel -type Log struct { - Task string - Step string - Log string -} - type LogReader struct { Task string Run string @@ -60,7 +54,7 @@ type LogReader struct { Steps []string } -func (lr *LogReader) Read() (<-chan Log, <-chan error, error) { +func (lr *LogReader) Read() (<-chan log.Log, <-chan error, error) { tkn := lr.Clients.Tekton tr, err := tkn.TektonV1alpha1().TaskRuns(lr.Ns).Get(lr.Run, metav1.GetOptions{}) if err != nil { @@ -72,7 +66,7 @@ func (lr *LogReader) Read() (<-chan Log, <-chan error, error) { return lr.readLogs(tr) } -func (lr *LogReader) readLogs(tr *v1alpha1.TaskRun) (<-chan Log, <-chan error, error) { +func (lr *LogReader) readLogs(tr *v1alpha1.TaskRun) (<-chan log.Log, <-chan error, error) { if lr.Follow { return lr.readLiveLogs() } @@ -97,7 +91,7 @@ func (lr *LogReader) formTaskName(tr *v1alpha1.TaskRun) { lr.Task = fmt.Sprintf("Task %d", lr.Number) } -func (lr *LogReader) readLiveLogs() (<-chan Log, <-chan error, error) { +func (lr *LogReader) readLiveLogs() (<-chan log.Log, <-chan error, error) { tr, err := lr.waitUntilPodNameAvailable(10) if err != nil { return nil, nil, err @@ -119,7 +113,7 @@ func (lr *LogReader) readLiveLogs() (<-chan Log, <-chan error, error) { return logC, errC, err } -func (lr *LogReader) readAvailableLogs(tr *v1alpha1.TaskRun) (<-chan Log, <-chan error, error) { +func (lr *LogReader) readAvailableLogs(tr *v1alpha1.TaskRun) (<-chan log.Log, <-chan error, error) { if !tr.HasStarted() { return nil, nil, fmt.Errorf("task %s has not started yet", lr.Task) } @@ -149,8 +143,8 @@ func (lr *LogReader) readAvailableLogs(tr *v1alpha1.TaskRun) (<-chan Log, <-chan return logC, errC, nil } -func (lr *LogReader) readStepsLogs(steps []*step, pod *pods.Pod, follow bool) (<-chan Log, <-chan error) { - logC := make(chan Log) +func (lr *LogReader) readStepsLogs(steps []*step, pod *pods.Pod, follow bool) (<-chan log.Log, <-chan error) { + logC := make(chan log.Log) errC := make(chan error) go func() { @@ -174,10 +168,10 @@ func (lr *LogReader) readStepsLogs(steps []*step, pod *pods.Pod, follow bool) (< case l, ok := <-podC: if !ok { podC = nil - logC <- Log{Task: lr.Task, Step: step.name, Log: "EOFLOG"} + logC <- log.Log{Task: lr.Task, Step: step.name, Log: "EOFLOG"} continue } - logC <- Log{Task: lr.Task, Step: step.name, Log: l.Log} + logC <- log.Log{Task: lr.Task, Step: step.name, Log: l.Log} case e, ok := <-perrC: if !ok { diff --git a/pkg/cmd/taskrun/log_writer.go b/pkg/cmd/taskrun/log_writer.go deleted file mode 100644 index 518a70e8c..000000000 --- a/pkg/cmd/taskrun/log_writer.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright © 2019 The Tekton Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package taskrun - -import ( - "fmt" - - "github.com/tektoncd/cli/pkg/cli" - "github.com/tektoncd/cli/pkg/formatted" -) - -type LogWriter struct { - fmt *formatted.Color -} - -//NewLogWriter returns the new instance of LogWriter -func NewLogWriter() *LogWriter { - return &LogWriter{ - fmt: formatted.NewColor(), - } -} - -func (lw *LogWriter) Write(s *cli.Stream, logC <-chan Log, errC <-chan error) { - for logC != nil || errC != nil { - select { - case l, ok := <-logC: - if !ok { - logC = nil - continue - } - - if l.Log == "EOFLOG" { - fmt.Fprintf(s.Out, "\n") - continue - } - - lw.fmt.Rainbow.Fprintf(l.Step, s.Out, "[%s] ", l.Step) - fmt.Fprintf(s.Out, "%s\n", l.Log) - case e, ok := <-errC: - if !ok { - errC = nil - continue - } - lw.fmt.Error(s.Err, "%s\n", e) - } - } -} diff --git a/pkg/cmd/taskrun/logs.go b/pkg/cmd/taskrun/logs.go index a4d03637f..d26799386 100644 --- a/pkg/cmd/taskrun/logs.go +++ b/pkg/cmd/taskrun/logs.go @@ -20,6 +20,7 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" + "github.com/tektoncd/cli/pkg/helper/log" "github.com/tektoncd/cli/pkg/helper/options" "github.com/tektoncd/cli/pkg/helper/pods" trlist "github.com/tektoncd/cli/pkg/helper/taskrun/list" @@ -117,7 +118,7 @@ func Run(opts *options.LogOptions) error { return err } - NewLogWriter().Write(opts.Stream, logC, errC) + log.NewLogWriter(log.LogTypeTask).Write(opts.Stream, logC, errC) return nil } diff --git a/pkg/helper/log/log.go b/pkg/helper/log/log.go new file mode 100644 index 000000000..6c118c46f --- /dev/null +++ b/pkg/helper/log/log.go @@ -0,0 +1,28 @@ +// Copyright © 2019 The Tekton Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package log + +const ( + LogTypePipeline = "pipeline" + LogTypeTask = "task" +) + +// Log represents data to write on log channel +type Log struct { + Pipeline string + Task string + Step string + Log string +} diff --git a/pkg/cmd/pipelinerun/log_writer.go b/pkg/helper/log/writer.go similarity index 70% rename from pkg/cmd/pipelinerun/log_writer.go rename to pkg/helper/log/writer.go index d79962f62..97ab20c80 100644 --- a/pkg/cmd/pipelinerun/log_writer.go +++ b/pkg/helper/log/writer.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package pipelinerun +package log import ( "fmt" @@ -21,17 +21,21 @@ import ( "github.com/tektoncd/cli/pkg/formatted" ) +// LogWriter helps logging pod"s log type LogWriter struct { - fmt *formatted.Color + fmt *formatted.Color + logType string } -//NewLogWriter returns the new instance of LogWriter -func NewLogWriter() *LogWriter { +// NewLogWriter returns the new instance of LogWriter +func NewLogWriter(logType string) *LogWriter { return &LogWriter{ - fmt: formatted.NewColor(), + fmt: formatted.NewColor(), + logType: logType, } } +// Write formatted pod's logs func (lw *LogWriter) Write(s *cli.Stream, logC <-chan Log, errC <-chan error) { for logC != nil || errC != nil { select { @@ -46,7 +50,13 @@ func (lw *LogWriter) Write(s *cli.Stream, logC <-chan Log, errC <-chan error) { continue } - lw.fmt.Rainbow.Fprintf(l.Step, s.Out, "[%s : %s] ", l.Task, l.Step) + switch lw.logType { + case LogTypePipeline: + lw.fmt.Rainbow.Fprintf(l.Step, s.Out, "[%s : %s] ", l.Task, l.Step) + case LogTypeTask: + lw.fmt.Rainbow.Fprintf(l.Step, s.Out, "[%s] ", l.Step) + } + fmt.Fprintf(s.Out, "%s\n", l.Log) case e, ok := <-errC: if !ok {