#Hi All,

1 messages ยท Page 1 of 1 (latest)

red echo
#

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:

  1. 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
  1. Remove the placeholder functions inside it so you should have
package main

import (
   "internal/dagger"
)

type MyBetterGoDaggerModule struct {}
  1. Install the go module you wanna extend
dagger install github.com/dagger/dagger/dev/go@v0.12.0
  1. 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?

Every Dagger module has an entrypoint function. The default one is generated automatically and has no arguments.

#

I can write a small TypeScript Example if you prefer (just did it from head, was easier in Go for me sry)

weary needle
#

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

red echo
# weary needle 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 ๐Ÿ˜„

weary needle
#

Is MyBetterGoDaggerModule the name of original module?

red echo
#

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