#Hi All,
1 messages ยท Page 1 of 1 (latest)
No problem! Let me guide you:
Imagine you want to add a function to the module Go: https://daggerverse.dev/mod/github.com/dagger/dagger/dev/go@133917c6f9ce36d8cfdc595d9b7bd2c14cbc2c20
Right now, this module has the following function:
- version()
- source()
- base()
- env()
- lint()
Let's say you would like to add the function test
What you would do is:
- Creating your own module
mkdir my-module && cd my-module
# You can actually use whatever language you want, I'll use go for the example. (faster to write)
dagger init --sdk=go --name=my-better-go-dagger-module
- Remove the placeholder functions inside it so you should have
package main
import (
"internal/dagger"
)
type MyBetterGoDaggerModule struct {}
- Install the go module you wanna extend
dagger install github.com/dagger/dagger/dev/go@v0.12.0
- Map the existing func of that module in your's, I see that this module has an entrypoint args so you can do it with a constructor function. (see: https://docs.dagger.io/manuals/developer/entrypoint-function/#constructor-only-arguments)
package main
import (
"context"
"internal/dagger"
)
type MyBetterGoDaggerModule struct {
Source *dagger.Directory
Version string
}
func New(
Source *dagger.Directory
Version string
) *MyBetterGoDaggerModule {
return &MyBetterGoDaggerModule{
Source,
Version,
}
}
// Map function from the existing module by calling your other module.
func (m *MyBetterGoDaggerModule) Version(ctx context.Context) (string, error) {
return dagger.Go(m.Source, m.Version).Version(ctx)
}
func (m *MyBetterGoDaggerModule) Source(ctx context.Context) (*dagger.Directory, error) {
return dagger.Go(m.Source, m.Version).Source(ctx)
}
// You add your extends function there
func (m *MyBetterGoDaggerModule) Test(ctx context.Context) (string, error) {
ctr, err := dagger.Go(m.Source, m.Version).Container(ctx)
return ctr.WithExec(...).Stdout(ctx)
}
Does this help you understand how you could do that?
I can write a small TypeScript Example if you prefer (just did it from head, was easier in Go for me sry)
First, I should appreciate your kind help..I am not GO person but let me read carefully everystep and search a bit
If it is easy, I d prefer to have an example for TS tho
Alright! Let me cook you one really quick ๐
import { dag, func, object, Directory } from "@dagger.io/dagger"
@object()
export class MyBetterGoDaggerModule {
// We keep the field private to not expose them directly since we got method for that
_source: Directory
_version: string
// The entrypoint function
constructor(
source: Directory
version: string
) {
this._source = source
this._version = version
}
// Some wrapper of the base module you wanna extend
@func()
async version(): Promise<string> {
return dag.go(this._source, this._version).version()
}
@func()
async source(): Promise<Directory> {
return dag.go(this._source, this._version).source()
}
// Your function that actually extends the module
@func()
async test(): Promise<string> {
const ctr = await dag.go(this._source, this._version).container()
return ctr.withExec([...]).stdout()
}
}
Let me know if you have any questions ๐
The basic idea is to map existing module func by just calling them from the imported module, and add your other functions that extends the module ๐
Is MyBetterGoDaggerModule the name of original module?
Really sorry for stupid questions but lets go with real example. If I would like to add another function of below module, do I have to exactly use the same module which is nodejs in this scenario?
https://github.com/RawkodeAcademy/Daggerverse/blob/9261a733e05982bd32239e56b5c4687b62c11996/nodejs/src/index.ts
Nope, it's the name of your module that wrap the module
You can name it the way you want, NodeJSwould work but you can name it whatever, you'll import the original module using dag.nodejs so you'll not hit any name conflict
Basically, what you will do is publish your own version of that module