Skip to content

Commit f5197cb

Browse files
refactor: hello
1 parent 3d61bf9 commit f5197cb

2 files changed

Lines changed: 112 additions & 75 deletions

File tree

‎hello/main.go‎

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
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.
145
package main
156

167
import (
8+
"context"
179
"dagger/hello/internal/dagger"
10+
"fmt"
1811
)
1912

2013
const (
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.
2518
type 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.
3728
func 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.
4844
func (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!\nGenerated by Dagger.\n", m.Greeting, name)
101+
102+
return dag.Directory().
103+
WithNewFile("greeting.txt", content).
104+
File("greeting.txt")
79105
}

‎hello/test/main.go‎

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,92 @@
1-
// A generated module for Test functions
2-
//
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.
1+
// Package main provides tests for the Hello reference module.
142
package main
153

164
import (
175
"context"
186
"dagger/hello/test/internal/dagger"
7+
"errors"
8+
"strings"
199

2010
"github.com/sourcegraph/conc/pool"
2111
)
2212

23-
// Test is a module for running tests.
13+
// Test provides tests for the Hello module.
2414
type Test struct{}
2515

26-
// All runs all tests.
16+
// All runs all tests concurrently.
2717
func (m *Test) All(ctx context.Context) error {
28-
p := pool.New().WithContext(ctx)
18+
p := pool.New().WithErrors().WithContext(ctx)
2919

30-
p.Go(m.HelloContainer)
31-
p.Go(m.HelloStringContainer)
32-
p.Go(m.HelloString)
20+
p.Go(m.TestMessage)
21+
p.Go(m.TestSay)
22+
p.Go(m.TestTerminal)
23+
p.Go(m.TestWrite)
24+
p.Go(m.TestCustomGreeting)
3325

3426
return p.Wait()
3527
}
3628

37-
// HelloContainer tests the HelloContainer function.
38-
func (m *Test) HelloContainer(ctx context.Context) error {
39-
40-
argString := "Hello, Daggerverse!"
41-
_, err := dag.Hello().HelloContainer(argString).Sync(ctx)
42-
29+
// TestMessage asserts that the pure Go Message function returns the expected string.
30+
func (m *Test) TestMessage(ctx context.Context) error {
31+
msg, err := dag.Hello().Message(ctx)
4332
if err != nil {
4433
return err
4534
}
4635

36+
if msg != "Hello, Daggerverse!" {
37+
return errors.New("unexpected message output: " + msg)
38+
}
4739
return nil
48-
4940
}
5041

51-
// HelloStringContainer tests the HelloString function.
52-
func (m *Test) HelloStringContainer(ctx context.Context) error {
53-
54-
argString := "Hello, Daggerverse!"
55-
_, err := dag.Hello(
56-
dagger.HelloOpts{
57-
Image: "cgr.dev/chainguard/wolfi-base:latest",
58-
},
59-
).HelloString(argString).Sync(ctx)
60-
42+
// TestSay asserts that the container execution produces the correct stdout.
43+
func (m *Test) TestSay(ctx context.Context) error {
44+
stdout, err := dag.Hello().Say(ctx, dagger.HelloSayOpts{
45+
Name: "Gopher",
46+
})
6147
if err != nil {
6248
return err
6349
}
6450

51+
if !strings.Contains(stdout, "Hello, Gopher!") {
52+
return errors.New("expected stdout to contain 'Hello, Gopher!', got: " + stdout)
53+
}
6554
return nil
55+
}
6656

57+
// TestTerminal asserts that the returned container executes successfully.
58+
func (m *Test) TestTerminal(ctx context.Context) error {
59+
_, err := dag.Hello().Terminal().Sync(ctx)
60+
return err
6761
}
6862

69-
// HelloString tests the HelloString function.
70-
func (m *Test) HelloString(ctx context.Context) error {
63+
// TestWrite asserts that a valid file artifact is generated with expected contents.
64+
func (m *Test) TestWrite(ctx context.Context) error {
65+
file := dag.Hello().Write()
66+
content, err := file.Contents(ctx)
67+
if err != nil {
68+
return err
69+
}
7170

72-
argString := "Hello, Daggerverse!"
73-
_, err := dag.Hello().HelloString(argString).Sync(ctx)
71+
if !strings.Contains(content, "Generated by Dagger.") {
72+
return errors.New("file content does not match expected output")
73+
}
74+
return nil
75+
}
7476

77+
// TestCustomGreeting tests the constructor options.
78+
func (m *Test) TestCustomGreeting(ctx context.Context) error {
79+
msg, err := dag.Hello(dagger.HelloOpts{
80+
Greeting: "Hello",
81+
}).Message(ctx, dagger.HelloMessageOpts{
82+
Name: "World",
83+
})
7584
if err != nil {
7685
return err
7786
}
7887

88+
if msg != "Hello, World!" {
89+
return errors.New("expected 'Hello, World!', got: " + msg)
90+
}
7991
return nil
80-
8192
}

0 commit comments

Comments
 (0)