1- // Package main provides a generated module for Hello functions .
1+ // A Dagger reference module demonstrating basic module concepts and return types .
22//
3- // This module has been generated via dagger init and serves as a reference to
4- // basic module structure as you get started with Dagger.
5- //
6- // Two functions have been pre-created. You can modify, delete, or add to them,
7- // as needed. They demonstrate usage of arguments and return types using simple
8- // echo and grep commands. The functions can be called from the dagger CLI or
9- // from one of the SDKs.
10- //
11- // The first line in this comment block is a short description line and the
12- // rest is a long description with more detail on the module's purpose or usage,
13- // if appropriate. All modules should have a short description.
3+ // This module serves as a learning template showing how to return strings,
4+ // chain containers, generate files, and use default or optional arguments.
145package main
156
167import (
8+ "context"
179 "dagger/hello/internal/dagger"
10+ "fmt"
1811)
1912
2013const (
21- defaultImageRepository = "busybox:uclibc "
14+ defaultImageRepository = "cgr.dev/chainguard/wolfi-base:latest "
2215)
2316
24- // Hello is a struct that provides echo functions.
17+ // Hello provides greeting and demonstration functions.
2518type Hello struct {
2619 // +private
2720 Image string
2821 // +private
22+ Greeting string
23+ // +private
2924 Ctr * dagger.Container
3025}
3126
32- // New creates a new instance of the Hello struct.
33- // If the ctr parameter is nil, it will create a new dagger.Container using the "busybox:uclibc" image.
34- // The stringArg parameter is an optional argument that specifies a string value.
35- // If not provided, it will default to "Hello, Daggerverse!".
36- // The function returns a pointer to the created Hello struct.
27+ // New creates a new instance of the Hello module.
3728func New (
38- // Custom image reference in "repository:tag" format to use as a base container .
29+ // Custom image reference in "repository:tag" format.
3930 // +optional
4031 image string ,
32+ // Default greeting word to use (e.g. "Hello", "Hola", "Szia").
33+ // +default="Hello"
34+ // +optional
35+ greeting string ,
4136) * Hello {
4237 return & Hello {
43- Image : image ,
38+ Image : image ,
39+ Greeting : greeting ,
4440 }
4541}
4642
47- // Container returns the underlying Dagger container
43+ // container returns the underlying Dagger container, lazily initialized.
4844func (m * Hello ) container () * dagger.Container {
4945 if m .Ctr != nil {
5046 return m .Ctr
@@ -59,21 +55,51 @@ func (m *Hello) container() *dagger.Container {
5955 return m .Ctr
6056}
6157
62- // HelloString returns the string value provided to the Hello struct.
63- func (m * Hello ) HelloString (
64- // StringArg is the string value to be printed.
65- StringArg string ,
66- ) * dagger.Container {
58+ // Message returns a greeting string using pure Go (runs instantly without containers).
59+ func (m * Hello ) Message (
60+ // Name of the person or entity to greet.
61+ // +default="Daggerverse"
62+ // +optional
63+ name string ,
64+ ) string {
65+ return fmt .Sprintf ("%s, %s!" , m .Greeting , name )
66+ }
67+
68+ // Say executes an echo command inside the container and returns stdout.
69+ func (m * Hello ) Say (
70+ ctx context.Context ,
71+ // Name to greet inside the container.
72+ // +default="Daggerverse"
73+ // +optional
74+ name string ,
75+ ) (string , error ) {
76+ msg := fmt .Sprintf ("%s, %s!" , m .Greeting , name )
6777 return m .container ().
68- WithExec ([]string {"echo" , StringArg })
78+ WithExec ([]string {"echo" , msg }).
79+ Stdout (ctx )
6980}
7081
71- // HelloContainer executes a container with the provided string value.
72- func (m * Hello ) HelloContainer (
73- // StringArg is the string value to be printed.
74- StringArg string ,
82+ // Terminal returns the container prepared with the message, ready for chaining or interactive shell.
83+ func (m * Hello ) Terminal (
84+ // Message to echo when container starts.
85+ // +default="Welcome to Dagger!"
86+ // +optional
87+ message string ,
7588) * dagger.Container {
7689 return m .container ().
77- From ("cgr.dev/chainguard/wolfi-base:latest" ).
78- WithExec ([]string {"echo" , StringArg })
90+ WithExec ([]string {"echo" , message })
91+ }
92+
93+ // Write generates a greeting text file inside Dagger and returns it as an artifact.
94+ func (m * Hello ) Write (
95+ // Name to greet in the generated file.
96+ // +default="Daggerverse"
97+ // +optional
98+ name string ,
99+ ) * dagger.File {
100+ content := fmt .Sprintf ("%s, %s!\n Generated by Dagger.\n " , m .Greeting , name )
101+
102+ return dag .Directory ().
103+ WithNewFile ("greeting.txt" , content ).
104+ File ("greeting.txt" )
79105}
0 commit comments