-
Notifications
You must be signed in to change notification settings - Fork 559
/
sdk_go.go
179 lines (154 loc) · 4.5 KB
/
sdk_go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"encoding/base64"
"fmt"
"strings"
"github.com/moby/buildkit/identity"
"golang.org/x/sync/errgroup"
"github.com/dagger/dagger/ci/consts"
"github.com/dagger/dagger/ci/util"
)
type GoSDK struct {
Dagger *Dagger // +private
}
// Lint the Go SDK
func (t GoSDK) Lint(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return lintGoModule(ctx, false, t.Dagger.Source, []string{"sdk/go"})
})
eg.Go(func() error {
return util.DiffDirectoryF(ctx, util.GoDirectory(t.Dagger.Source), t.Generate, "sdk/go")
})
return eg.Wait()
}
// Test the Go SDK
func (t GoSDK) Test(ctx context.Context) error {
installer, err := t.Dagger.installer(ctx, "sdk-go-test")
if err != nil {
return err
}
output, err := util.GoBase(t.Dagger.Source).
With(installer).
WithWorkdir("sdk/go").
WithExec([]string{"go", "test", "-v", "./..."}).
Stdout(ctx)
if err != nil {
err = fmt.Errorf("test failed: %w\n%s", err, output)
}
return err
}
// Regenerate the Go SDK API
func (t GoSDK) Generate(ctx context.Context) (*Directory, error) {
installer, err := t.Dagger.installer(ctx, "sdk-go-generate")
if err != nil {
return nil, err
}
generated := util.GoBase(t.Dagger.Source).
With(installer).
WithWorkdir("sdk/go").
WithExec([]string{"go", "generate", "-v", "./..."}).
WithExec([]string{"go", "mod", "tidy"}).
Directory(".")
return dag.Directory().WithDirectory("sdk/go", generated), nil
}
// Publish the Go SDK
func (t GoSDK) Publish(
ctx context.Context,
tag string,
// +optional
dryRun bool,
// +optional
// +default="https://github.com/dagger/dagger-go-sdk.git"
gitRepo string,
// +optional
// +default="dagger-ci"
gitUserName string,
// +optional
// +default="hello@dagger.io"
gitUserEmail string,
// +optional
githubToken *Secret,
) error {
return gitPublish(ctx, gitPublishOpts{
source: "https://github.com/dagger/dagger.git",
alpineBase: util.GoBase(t.Dagger.Source),
sourceTag: tag,
sourcePath: "sdk/go/",
sourceFilter: "if [ -f go.mod ]; then go mod edit -dropreplace github.com/dagger/dagger; fi",
dest: gitRepo,
destTag: strings.TrimPrefix(tag, "sdk/go/"),
username: gitUserName,
email: gitUserEmail,
githubToken: githubToken,
dryRun: dryRun,
})
}
// Bump the Go SDK's Engine dependency
func (t GoSDK) Bump(version string) (*Directory, error) {
// trim leading v from version
version = strings.TrimPrefix(version, "v")
versionFile := fmt.Sprintf(`// Code generated by dagger. DO NOT EDIT.
package engineconn
const CLIVersion = %q
`, version)
// NOTE: if you change this path, be sure to update .github/workflows/publish.yml so that
// provision tests run whenever this file changes.
dir := dag.Directory().WithNewFile("sdk/go/internal/engineconn/version.gen.go", versionFile)
return dir, nil
}
type gitPublishOpts struct {
source, dest string
sourceTag, destTag string
sourcePath string
sourceFilter string
username string
email string
githubToken *Secret
alpineBase *Container
dryRun bool
}
func gitPublish(ctx context.Context, opts gitPublishOpts) error {
base := opts.alpineBase
if base == nil {
base = dag.Container().From(consts.AlpineImage)
}
git := base.
WithExec([]string{"apk", "add", "-U", "--no-cache", "git"}).
WithExec([]string{"git", "config", "--global", "user.name", opts.username}).
WithExec([]string{"git", "config", "--global", "user.email", opts.email})
if !opts.dryRun {
githubTokenRaw, err := opts.githubToken.Plaintext(ctx)
if err != nil {
return err
}
encodedPAT := base64.URLEncoding.EncodeToString([]byte("pat:" + githubTokenRaw))
git = git.
WithEnvVariable("GIT_CONFIG_COUNT", "1").
WithEnvVariable("GIT_CONFIG_KEY_0", "http.https://github.com/.extraheader").
WithSecretVariable("GIT_CONFIG_VALUE_0", dag.SetSecret("GITHUB_HEADER", fmt.Sprintf("AUTHORIZATION: Basic %s", encodedPAT)))
}
result := git.
WithEnvVariable("CACHEBUSTER", identity.NewID()).
WithExec([]string{"git", "clone", opts.source, "/src/dagger"}).
WithWorkdir("/src/dagger").
WithEnvVariable("FILTER_BRANCH_SQUELCH_WARNING", "1").
WithExec([]string{
"git", "filter-branch", "-f", "--prune-empty",
"--subdirectory-filter", opts.sourcePath,
"--tree-filter", opts.sourceFilter,
"--", opts.sourceTag,
})
if !opts.dryRun {
result = result.WithExec([]string{
"git",
"push",
"-f",
opts.dest,
fmt.Sprintf("%s:%s", opts.sourceTag, opts.destTag),
})
}
_, err := result.Sync(ctx)
return err
}