#getting exec.Command() output before dagger finishes the resolving the graph

1 messages · Page 1 of 1 (latest)

forest talon
#

I am trying to run an exec.Command() and was hit by the synchronization issue that has been wrapped up here

So basically I want the current HEAD sha by running the following but the output is not ready when dagger has resolved graph. Was there a solution to this already?

func getCommandOutput(bin string, args ...string) string {
    cmd := exec.Command(bin, args...)
    out, err := cmd.Output()
    if err != nil {
        log.Fatal(err)
    }
    return string(out)
}

Also experimented with this

func getCommandOutput(bin string, args ...string) string {
    var stdoutBuf, stderrBuf bytes.Buffer

    cmd := exec.Command(bin, args...)
    cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
    cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
    err := cmd.Start()
    if err != nil {
        log.Fatal(err)
    }
    return stdoutBuf.String()
}

And the caller

sha := getCommandOutput("git", "rev-parse", "--short=8", "HEAD")
leaden coyote
#

So basically I want the current HEAD sha by running the following but the output is not ready when dagger has resolved graph. Was there a solution to this already?

no sure I get the full picture, when do you want to run that getCommandOutput?

forest talon
#

right, let me post some context

so I am pushing an image to ECR and I want to stitch the current git HEAD sha to the image, like so

    addr, err := client.Container().
        From(baseImage).
        WithWorkdir("/app").WithMountedCache("./node_modules", client.CacheVolume("node_modules")).
        WithDirectory("/app/dist", buildDir).
        WithRegistryAuth(ecrRegistry, "AWS", secret).
        Publish(ctx, fmt.Sprintf("%s/%s:%s", ecrRegistry, service, sha))

    if err != nil {
        log.Fatal(err)
    }
#

as long as the sha variable is static it works fine

leaden coyote
forest talon
#

the interesting part is that the first line in the dagger logs is the sha, so it's resolved before all the dagger pipelines are

forest talon
forest talon
#

I wasn't able to get this to work using exec.Command 😦

the https://github.com/go-cmd/cmd package worked though 🤷

func getCommandOutput(bin string, args ...string) string {
    c := cmd.NewCmd(bin, args...)
    status := <-c.Start()
    out := strings.Join(status.Stdout, " ")
    return out
}
#

wrapped all of this in a getSha function when pipelines are run locally and the user hasn't commited changes 😄

func getSha(name string) string {
    e := os.Getenv(name)
    if e == "" {
        currentUser, err := user.Current()
        if err != nil {
            log.Fatalf(err.Error())
        }
        sha := getCommandOutput("git", "rev-parse", "--short=8", "HEAD")
        currentTime := time.Now().Format("20060102150405")
        return fmt.Sprintf("%s-%s-%s", currentUser.Username, sha, currentTime)
    }
    return e
}
leaden coyote
#

🚀 . Do you still want to try with the exec.Command? I can give it a quick try if you want

forest talon
#

ahh, sry the late reply

yes, that would be really helpful, let me craft some minimal code to reproduce this

forest talon
#

but this is the same code I had earlier, which is weird

#

the only difference is this line

before

  log.Fatal(err)

current

  panic(err)
#

but anyway, not a Dagger thing