-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect.go
More file actions
165 lines (150 loc) · 3.64 KB
/
Copy pathinspect.go
File metadata and controls
165 lines (150 loc) · 3.64 KB
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
package docker
import (
"context"
"fmt"
"io"
"strings"
"time"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
"github.com/crunchloop/devcontainer/runtime"
)
func (r *Runtime) InspectImage(ctx context.Context, ref string) (*runtime.ImageDetails, error) {
res, err := r.api.ImageInspect(ctx, ref)
if err != nil {
if isImageNotFound(err) {
return nil, &runtime.ImageNotFoundError{Ref: ref, Err: err}
}
return nil, fmt.Errorf("ImageInspect: %w", err)
}
out := &runtime.ImageDetails{
ID: res.ID,
Tags: res.RepoTags,
}
if res.Config != nil {
out.Labels = copyLabels(res.Config.Labels)
out.Env = append([]string(nil), res.Config.Env...)
out.User = res.Config.User
}
return out, nil
}
func (r *Runtime) InspectContainer(ctx context.Context, id string) (*runtime.ContainerDetails, error) {
res, err := r.api.ContainerInspect(ctx, id, client.ContainerInspectOptions{})
if err != nil {
if isContainerNotFound(err) {
return nil, &runtime.ContainerNotFoundError{ID: id, Err: err}
}
return nil, fmt.Errorf("ContainerInspect: %w", err)
}
c := res.Container
out := &runtime.ContainerDetails{
Container: runtime.Container{
ID: c.ID,
Name: trimLeadingSlash(c.Name),
Image: c.Image,
State: stateFrom(c.State),
},
Created: parseTime(c.Created),
StartedAt: stateStartedAt(c.State),
FinishedAt: stateFinishedAt(c.State),
ExitCode: stateExitCode(c.State),
Mounts: convertMounts(c.Mounts),
}
if c.Config != nil {
out.User = c.Config.User
out.Env = append([]string(nil), c.Config.Env...)
out.Labels = copyLabels(c.Config.Labels)
}
return out, nil
}
func (r *Runtime) ContainerLogs(ctx context.Context, id string, w io.Writer, follow bool) error {
res, err := r.api.ContainerLogs(ctx, id, client.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: follow,
})
if err != nil {
if isContainerNotFound(err) {
return &runtime.ContainerNotFoundError{ID: id, Err: err}
}
return fmt.Errorf("ContainerLogs: %w", err)
}
// Logs are framed when no TTY; demux into one writer.
rc, ok := res.(io.ReadCloser)
if !ok {
return fmt.Errorf("ContainerLogs: result is not io.ReadCloser")
}
if follow {
// Wrap in cancellable copy so ctx cancellation breaks the read.
pr, pw := io.Pipe()
go func() {
_ = runtime.CancellableCopy(ctx, pw, rc)
_ = pw.Close()
}()
return stdcopy(w, w, pr)
}
defer rc.Close()
return stdcopy(w, w, rc)
}
func trimLeadingSlash(s string) string {
return strings.TrimPrefix(s, "/")
}
func stateFrom(s *container.State) runtime.State {
if s == nil {
return ""
}
return runtime.State(s.Status)
}
func stateStartedAt(s *container.State) time.Time {
if s == nil {
return time.Time{}
}
return parseTime(s.StartedAt)
}
func stateFinishedAt(s *container.State) time.Time {
if s == nil {
return time.Time{}
}
return parseTime(s.FinishedAt)
}
func stateExitCode(s *container.State) int {
if s == nil {
return 0
}
return s.ExitCode
}
func parseTime(s string) time.Time {
if s == "" {
return time.Time{}
}
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
return time.Time{}
}
return t
}
func convertMounts(in []container.MountPoint) []runtime.MountInspect {
if len(in) == 0 {
return nil
}
out := make([]runtime.MountInspect, 0, len(in))
for _, m := range in {
out = append(out, runtime.MountInspect{
Type: string(m.Type),
Source: m.Source,
Target: m.Destination,
ReadOnly: !m.RW,
})
}
return out
}
func copyLabels(in map[string]string) map[string]string {
if len(in) == 0 {
return nil
}
out := make(map[string]string, len(in))
for k, v := range in {
out[k] = v
}
return out
}