-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.go
More file actions
173 lines (156 loc) · 5.68 KB
/
image.go
File metadata and controls
173 lines (156 loc) · 5.68 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
166
167
168
169
170
171
172
173
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package hypeman
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/kernel/hypeman-go/internal/apijson"
"github.com/kernel/hypeman-go/internal/apiquery"
"github.com/kernel/hypeman-go/internal/requestconfig"
"github.com/kernel/hypeman-go/option"
"github.com/kernel/hypeman-go/packages/param"
"github.com/kernel/hypeman-go/packages/respjson"
)
// ImageService contains methods and other services that help with interacting with
// the hypeman API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewImageService] method instead.
type ImageService struct {
Options []option.RequestOption
}
// NewImageService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewImageService(opts ...option.RequestOption) (r ImageService) {
r = ImageService{}
r.Options = opts
return
}
// Pull and convert OCI image
func (r *ImageService) New(ctx context.Context, body ImageNewParams, opts ...option.RequestOption) (res *Image, err error) {
opts = slices.Concat(r.Options, opts)
path := "images"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// List images
func (r *ImageService) List(ctx context.Context, query ImageListParams, opts ...option.RequestOption) (res *[]Image, err error) {
opts = slices.Concat(r.Options, opts)
path := "images"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Delete image
func (r *ImageService) Delete(ctx context.Context, name string, opts ...option.RequestOption) (err error) {
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithHeader("Accept", "*/*")}, opts...)
if name == "" {
err = errors.New("missing required name parameter")
return err
}
path := fmt.Sprintf("images/%s", name)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...)
return err
}
// Get image details
func (r *ImageService) Get(ctx context.Context, name string, opts ...option.RequestOption) (res *Image, err error) {
opts = slices.Concat(r.Options, opts)
if name == "" {
err = errors.New("missing required name parameter")
return nil, err
}
path := fmt.Sprintf("images/%s", name)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
type Image struct {
// Creation timestamp (RFC3339)
CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
// Resolved manifest digest
Digest string `json:"digest" api:"required"`
// Normalized OCI image reference (tag or digest)
Name string `json:"name" api:"required"`
// Build status
//
// Any of "pending", "pulling", "converting", "ready", "failed".
Status ImageStatus `json:"status" api:"required"`
// CMD from container metadata
Cmd []string `json:"cmd" api:"nullable"`
// Entrypoint from container metadata
Entrypoint []string `json:"entrypoint" api:"nullable"`
// Environment variables from container metadata
Env map[string]string `json:"env"`
// Error message if status is failed
Error string `json:"error" api:"nullable"`
// Position in build queue (null if not queued)
QueuePosition int64 `json:"queue_position" api:"nullable"`
// Disk size in bytes (null until ready)
SizeBytes int64 `json:"size_bytes" api:"nullable"`
// User-defined key-value tags.
Tags map[string]string `json:"tags"`
// Working directory from container metadata
WorkingDir string `json:"working_dir" api:"nullable"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CreatedAt respjson.Field
Digest respjson.Field
Name respjson.Field
Status respjson.Field
Cmd respjson.Field
Entrypoint respjson.Field
Env respjson.Field
Error respjson.Field
QueuePosition respjson.Field
SizeBytes respjson.Field
Tags respjson.Field
WorkingDir respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Image) RawJSON() string { return r.JSON.raw }
func (r *Image) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Build status
type ImageStatus string
const (
ImageStatusPending ImageStatus = "pending"
ImageStatusPulling ImageStatus = "pulling"
ImageStatusConverting ImageStatus = "converting"
ImageStatusReady ImageStatus = "ready"
ImageStatusFailed ImageStatus = "failed"
)
type ImageNewParams struct {
// OCI image reference (e.g., docker.io/library/nginx:latest)
Name string `json:"name" api:"required"`
// User-defined key-value tags.
Tags map[string]string `json:"tags,omitzero"`
paramObj
}
func (r ImageNewParams) MarshalJSON() (data []byte, err error) {
type shadow ImageNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *ImageNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ImageListParams struct {
// Filter images by tag key-value pairs.
Tags map[string]string `query:"tags,omitzero" json:"-"`
paramObj
}
// URLQuery serializes [ImageListParams]'s query parameters as `url.Values`.
func (r ImageListParams) URLQuery() (v url.Values, err error) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}