This is an automated email from the ASF dual-hosted git repository.

ricardozanini pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-tools.git


The following commit(s) were added to refs/heads/main by this push:
     new 01e01b2137a kie-issues#1335: kn workflow build output log should be 
saved in the target dir (#2474)
01e01b2137a is described below

commit 01e01b2137a16108c48787a37acaa277b2dc375b
Author: Dmitrii Tikhomirov <[email protected]>
AuthorDate: Mon Aug 12 08:01:12 2024 -0700

    kie-issues#1335: kn workflow build output log should be saved in the target 
dir (#2474)
---
 .../pkg/command/quarkus/build.go                   | 59 +++++++++++++++++-----
 1 file changed, 47 insertions(+), 12 deletions(-)

diff --git a/packages/kn-plugin-workflow/pkg/command/quarkus/build.go 
b/packages/kn-plugin-workflow/pkg/command/quarkus/build.go
index afcbab66f90..6f0085d8ae2 100644
--- a/packages/kn-plugin-workflow/pkg/command/quarkus/build.go
+++ b/packages/kn-plugin-workflow/pkg/command/quarkus/build.go
@@ -21,6 +21,10 @@ package quarkus
 
 import (
        "fmt"
+       "io"
+       "log"
+       "os"
+       "path/filepath"
        "regexp"
        "strconv"
        "strings"
@@ -224,8 +228,32 @@ func runAddExtension(cfg BuildCmdConfig) error {
 }
 
 func runBuildImage(cfg BuildCmdConfig) (out string, err error) {
+       var flags = log.Flags()
+       defer log.SetFlags(flags)
+       log.SetFlags(0)
+
+       dir := "target"
+       filePath := filepath.Join(dir, "build.log")
+
+       if _, err := os.Stat(dir); os.IsNotExist(err) {
+               err = os.MkdirAll(dir, 0755)
+               if err != nil {
+                       log.Fatalf("Unable to create a target folder: %v", err)
+               }
+       }
+
+       file, err := os.Create(filePath)
+       if err != nil {
+               log.Fatalf("Unable to create a log file  : %v", err)
+       }
+       log.SetOutput(file)
+       defer file.Close()
+
+       multiWriter := io.MultiWriter(os.Stdout, file)
+
        registry, repository, name, tag := getImageConfig(cfg)
        if err = checkImageName(name); err != nil {
+               log.Print(err)
                return
        }
 
@@ -247,25 +275,32 @@ func runBuildImage(cfg BuildCmdConfig) (out string, err 
error) {
                executableName,
        )
 
-       if err = common.RunCommand(
-               build,
-               "build",
-       ); err != nil {
+       build.Stdout = multiWriter
+       build.Stderr = multiWriter
+
+       if err := build.Start(); err != nil {
+               // write to the log as well
+               log.Println("ERROR: starting command \"build\" failed")
                if cfg.Push {
-                       fmt.Println("ERROR: Image build failed.")
-                       fmt.Println("If you're using a private registry, check 
if you're authenticated")
+                       log.Println("ERROR: Image build failed.")
+                       log.Println("If you're using a private registry, check 
if you're authenticated")
                }
-               return
+               return "", err
+       }
+
+       if err := build.Wait(); err != nil {
+               log.Println("ERROR: something went wrong during command 
\"build\"")
+               return "", err
        }
 
        out = getImage(registry, repository, name, tag)
        if cfg.Push {
-               fmt.Printf("Created and pushed an image to registry: %s\n", out)
+               log.Printf("Created and pushed an image to registry: %s\n", out)
        } else {
-               fmt.Printf("Created a local image: %s\n", out)
+               log.Printf("Created a local image: %s\n", out)
        }
 
-       fmt.Println("✅ Build success")
+       log.Println("✅ Build success")
        return
 }
 
@@ -274,11 +309,11 @@ func checkImageName(name string) (err error) {
        if !matched {
                fmt.Println(`
 ERROR: Image name should match [a-z]([-a-z0-9]*[a-z0-9])?
-The name needs to start with a lower case letter and then it can be composed 
exclusvely of lower case letters, numbers or dashes ('-')
+The name needs to start with a lower case letter and then it can be composed 
exclusively of lower case letters, numbers or dashes ('-')
 Example of valid names: "test-0-0-1", "test", "t1"
 Example of invalid names: "1-test", "test.1", "test/1"
                `)
-               err = fmt.Errorf("invalid image name")
+               err = fmt.Errorf("invalid image name: %s", name)
        }
        return
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to