#Container assignment isn't working as expected

1 messages · Page 1 of 1 (latest)

jolly minnow
#

This is a strange one. I am not sure why this works this way.

this works

package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "dagger.io/dagger"
)

func main() {
    var test *dagger.Container

    ctx := context.Background()
    c, _ := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))

    mycontainer := c.Container().From("golang:1.20-alpine")

    if true {
        test = mycontainer.Pipeline("my pipeline").
            WithEnvVariable("cb", time.Now().String()).
            WithExec([]string{"echo", "HELLO_WORLD"})
    } else {
        test = mycontainer.Pipeline("my pipeline 2")
    }

    // test.
    //     WithEnvVariable("cb", time.Now().String()).
    //     WithExec([]string{"echo", "HELLO_WORLD"})

    fmt.Println(test.Stdout(ctx))

}

this doesnt

package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "dagger.io/dagger"
)

func main() {
    var test *dagger.Container

    ctx := context.Background()
    c, _ := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))

    mycontainer := c.Container().From("golang:1.20-alpine")

    if true {
        test = mycontainer.Pipeline("my pipeline")
        // WithEnvVariable("cb", time.Now().String()).
        // WithExec([]string{"echo", "HELLO_WORLD"})
    } else {
        test = mycontainer.Pipeline("my pipeline 2")
    }

    test.
        WithEnvVariable("cb", time.Now().String()).
        WithExec([]string{"echo", "HELLO_WORLD"})

    fmt.Println(test.Stdout(ctx))

}

I am fairly new to Go so I'm probably doing something dumb. But I can't figure it out. Is there a way to write this so I can add additional stages to the container outside of the if block?

gilded jungle
#

Hey @jolly minnow 👋
In the second snippet, try

test = test.
        WithEnvVariable("cb", time.Now().String()).
        WithExec([]string{"echo", "HELLO_WORLD"})
jolly minnow
#

omg! that did it! Thank you so much! I knew it had to be something silly like that. But it was unusually tough to troubleshoot. I've written a bunch of dagger code so far but beats me how I missed this.

gilded jungle
#

Heh no worries, its an easy one to miss 🙂