#What is the best way to use dagger functions as a container cli wrapper?

1 messages · Page 1 of 1 (latest)

low umbra
#

I would like to create a gh module that basically runs github cli (gh) with the string as the arguments. However if i create a function that just receives an string it won't work because it would use it as if it was argv[1] :

package main

import "context"

type Gh struct{}

// Returns a container that runs the `gh` command with the given arguments.
func (m *Gh) Cli(
    ctx context.Context,

    // The arguments to pass to the `gh` command.
    // +required
    command string,

    // The GitHub token to use for authentication.
    // +required
    gh_token *Secret,
) *Container {

    gh := dag.Container().From("v17v3/gh")

    // Set the GitHub token as an environment variable.
    token, err := gh_token.Plaintext(ctx)

    if err != nil {
        panic(err)
    }

    gh.WithEnvVariable("GH_TOKEN", token)

    return gh.WithExec([]string{command})
}

Splitting the arg by spaces does not sound cool because they may could be set like arg=foo and reinventing the wheel by creating a function for each subparameter sounds like a bad idea.

Maybe this is not a desirable approach for dagger?

scenic cloud
#

Hey @low umbra!

What you probably want is to do sh -c and then send the entire command (the shell form, instead of the default exec form).

Quick question, have you looked into https://daggerverse.dev for existing gh modules? In the past I used this one: https://daggerverse.dev/mod/github.com/aweris/daggerverse/gh@99a1336f8091ff43bf833778a324de1cadcf25ac and it worked great for me! Maybe it can work for you too!

low umbra
#

Oh yeah, that sounds like the way to go, I'll check that one out too