/
podmancontainer.go
49 lines (43 loc) · 1.28 KB
/
podmancontainer.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
// Package podmancontainer provides connhelper for podman-container://<container>
package podmancontainer
import (
"context"
"net"
"net/url"
"github.com/docker/cli/cli/connhelper/commandconn"
"github.com/moby/buildkit/client/connhelper"
"github.com/pkg/errors"
)
func init() {
connhelper.Register("podman-container", Helper)
}
// Helper returns helper for connecting to a Podman container.
// Requires BuildKit v0.5.0 or later in the container.
func Helper(u *url.URL) (*connhelper.ConnectionHelper, error) {
sp, err := SpecFromURL(u)
if err != nil {
return nil, err
}
return &connhelper.ConnectionHelper{
ContextDialer: func(ctx context.Context, addr string) (net.Conn, error) {
// using background context because context remains active for the duration of the process, after dial has completed
return commandconn.New(context.Background(), "podman", "exec", "-i", sp.Container, "buildctl", "dial-stdio")
},
}, nil
}
// Spec
type Spec struct {
Container string
}
// SpecFromURL creates Spec from URL.
// URL is like podman-container://<container>
// The <container> part is mandatory.
func SpecFromURL(u *url.URL) (*Spec, error) {
sp := Spec{
Container: u.Hostname(),
}
if sp.Container == "" {
return nil, errors.New("url lacks container name")
}
return &sp, nil
}