-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
93 lines (84 loc) · 1.76 KB
/
main.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
package main
import (
"context"
"crypto/sha256"
"dagger/mikael-elkiaer/internal/dagger"
"encoding/hex"
"fmt"
)
type MikaelElkiaer struct {
// +private
AdditionalCAs []*dagger.File
// +private
Creds []*Cred
}
type Cred struct {
Name string
Url string
UserId string
UserSecret *dagger.Secret
}
// Add an additional CA certificate
func (m *MikaelElkiaer) WithCA(
// File containing the CA
file *dagger.File,
) *MikaelElkiaer {
m.AdditionalCAs = append(m.AdditionalCAs, file)
return m
}
func (m *MikaelElkiaer) WithDownloadedCA(
ctx context.Context,
uri string,
) (*MikaelElkiaer, error) {
cert := downloadAsFile(ctx, uri)
m = m.WithCA(cert)
return m, nil
}
// Add additional creds
func (m *MikaelElkiaer) WithCred(
// Used as identifier in configs
// Defaults to userId
// GitHub: Used as organisation name if set
// +optional
name string,
// URL to the service
// +default="ghcr.io"
url string,
// User name, email, or similar
userId string,
// Password, token, or similar
userSecret *dagger.Secret,
) (*MikaelElkiaer, error) {
id := name
if id == "" {
id = userId
}
cred := &Cred{
Name: id,
Url: url,
UserId: userId,
UserSecret: userSecret,
}
m.Creds = append(m.Creds, cred)
return m, nil
}
func downloadAsFile(
ctx context.Context,
uri string,
) *dagger.File {
h := sha256.New()
h.Write([]byte(uri))
hashed := h.Sum(nil)
name := hex.EncodeToString(hashed)
return dag.Container().
From("docker.io/library/alpine:3.20.3@sha256:1e42bbe2508154c9126d48c2b8a75420c3544343bf86fd041fb7527e017a4b4a").
WithWorkdir("/tmp").
WithExec([]string{"wget", "--output-document", name, uri}).
File(name)
}
func inSh(
cmd string,
a ...any,
) []string {
return []string{"sh", "-c", fmt.Sprintf(cmd, a...)}
}