diff --git a/README.md b/README.md index 910e2cb..2aa1f9a 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ Sample yaml file workflow: - run +logsWithTime: true + run: image: golang:alpine3.15 copyFiles: true @@ -88,6 +90,26 @@ If you want to see all files in the cmd folder you must set soloExecution to fal cd cmd && ls ``` +## logsWithTime + +default: false + +logsWithTime => true +```sh +⚉ 2022/05/08 11:36:30 Image is available +⚉ 2022/05/08 11:36:30 Start creating container +⚉ 2022/05/08 11:36:33 Starting the container +⚉ 2022/05/08 11:36:35 Execute command: ls -a +``` + +logsWithTime => false +```sh +⚉ Image is available +⚉ Start creating container +⚉ Starting the container +⚉ Execute command: ls -a +``` + # Tests ```sh diff --git a/pkg/apply.go b/pkg/apply.go index e33da1a..099f623 100644 --- a/pkg/apply.go +++ b/pkg/apply.go @@ -18,7 +18,7 @@ func Apply(name string, filepath string) error { return err } - workflow, err := parse() + pipeline, err := parse() if err != nil { fmt.Println(err) @@ -27,7 +27,7 @@ func Apply(name string, filepath string) error { currentRunner := Runner{} - if err := currentRunner.run(workflow); err != nil { + if err := currentRunner.run(pipeline); err != nil { fmt.Println(err) return err } diff --git a/pkg/parser.go b/pkg/parser.go index 603d23f..1b09186 100644 --- a/pkg/parser.go +++ b/pkg/parser.go @@ -7,10 +7,13 @@ import ( "github.com/spf13/viper" ) -type Workflow []Job +type Pipeline struct { + Workflow []Job + LogsWithTime bool +} -func parse() (Workflow, error) { - var workflow Workflow = Workflow{} +func parse() (Pipeline, error) { + var pipeline Pipeline = Pipeline{} flows := viper.GetStringSlice("workflow") @@ -20,15 +23,17 @@ func parse() (Workflow, error) { job, err := generateJob(configMap) if err != nil { - return nil, err + return Pipeline{}, err } job.Name = v - workflow = append(workflow, job) + pipeline.Workflow = append(pipeline.Workflow, job) } - return workflow, nil + pipeline.LogsWithTime = viper.GetBool("logsWithTime") + + return pipeline, nil } func generateJob(configMap map[string]interface{}) (Job, error) { @@ -58,10 +63,6 @@ func generateJob(configMap map[string]interface{}) (Job, error) { soloExecution := getBool(configMap["soloexecution"]) - if err != nil { - return Job{}, err - } - var job Job = Job{ Image: image, Script: script, diff --git a/pkg/runner.go b/pkg/runner.go index 28b78e7..f900b56 100644 --- a/pkg/runner.go +++ b/pkg/runner.go @@ -33,8 +33,12 @@ type Runner struct { container container.ContainerCreateCreatedBody } -func (r *Runner) run(workflow Workflow) error { - r.infoLog = log.New(os.Stdout, "INFO \t", log.Ldate|log.Ltime) +func (r *Runner) run(pipeline Pipeline) error { + if pipeline.LogsWithTime { + r.infoLog = log.New(os.Stdout, "⚉ ", log.Ldate|log.Ltime) + } else { + r.infoLog = log.New(os.Stdout, "⚉ ", 0) + } r.ctx = context.Background() cli, err := client.NewClientWithOpts() @@ -46,11 +50,11 @@ func (r *Runner) run(workflow Workflow) error { r.cli = cli r.imageManager = image_manager.NewImageManager(r.ctx, r.cli, r.infoLog) r.containerManager = container_manager.NewContainerManager(r.ctx, r.cli, r.infoLog) + r.shellCommander = shell_commander.NewShellCommander() - for _, job := range workflow { + for _, job := range pipeline.Workflow { r.currentJob = job r.workDir = job.WorkDir - r.shellCommander = shell_commander.NewShellCommander() if err := r.jobRunner(); err != nil { return err @@ -184,7 +188,6 @@ func (r *Runner) commandRunner(command string, name string) error { color.Set(color.FgRed) r.infoLog.Printf("Command execution failed") - r.infoLog.Println("=======================") r.infoLog.Println("Command Log:") if reader, _, err := r.cli.CopyFromContainer(r.ctx, r.container.ID, "/shell_command_output.log"); err == nil { @@ -193,7 +196,6 @@ func (r *Runner) commandRunner(command string, name string) error { b, _ := ioutil.ReadAll(tr) fmt.Println("\n" + string(b)) } - r.infoLog.Println("=======================") color.Unset() r.cli.ContainerKill(r.ctx, r.container.ID, "KILL") @@ -218,10 +220,8 @@ func (r *Runner) commandRunner(command string, name string) error { if len(b) != 0 { color.Set(color.FgGreen) - r.infoLog.Println("=======================") r.infoLog.Println("Command Log:") fmt.Println("\n" + string(b)) - r.infoLog.Println("=======================") color.Unset() } }