Debian Codegraph @

buildkit/client/connhelper/podmancontainer/podmancontainer.go

raw
1 // Package podmancontainer provides connhelper for podman-container://<container>
2 package podmancontainer
3
4 import (
5 "context"
6 "net"
7 "net/url"
8
9 "github.com/docker/cli/cli/connhelper/commandconn"
10 "github.com/moby/buildkit/client/connhelper"
11 "github.com/pkg/errors"
12 )
13
14 func init() {
15 connhelper.Register("podman-container", Helper)
16 }
17
18 // Helper returns helper for connecting to a Podman container.
19 // Requires BuildKit v0.5.0 or later in the container.
20 func Helper(u *url.URL) (*connhelper.ConnectionHelper, error) {
21 sp, err := SpecFromURL(u)
22 if err != nil {
23 return nil, err
24 }
25 return &connhelper.ConnectionHelper{
26 ContextDialer: func(ctx context.Context, addr string) (net.Conn, error) {
27 // using background context because context remains active for the duration of the process, after dial has completed
28 return commandconn.New(context.Background(), "podman", "exec", "-i", sp.Container, "buildctl", "dial-stdio")
29 },
30 }, nil
31 }
32
33 // Spec
34 type Spec struct {
35 Container string
36 }
37
38 // SpecFromURL creates Spec from URL.
39 // URL is like podman-container://<container>
40 // The <container> part is mandatory.
41 func SpecFromURL(u *url.URL) (*Spec, error) {
42 sp := Spec{
43 Container: u.Hostname(),
44 }
45 if sp.Container == "" {
46 return nil, errors.New("url lacks container name")
47 }
48 return &sp, nil
49 }