-
Notifications
You must be signed in to change notification settings - Fork 762
/
create.go
70 lines (61 loc) · 1.68 KB
/
create.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
package source
import (
"context"
"fmt"
"os"
"time"
spec "github.com/opencontainers/image-spec/specs-go"
specV1 "github.com/opencontainers/image-spec/specs-go/v1"
)
// CreateOptions includes data to alter certain knobs when creating a source
// image.
type CreateOptions struct {
// Author is the author of the source image.
Author string
// TimeStamp controls whether a "created" timestamp is set or not.
TimeStamp bool
}
// createdTime returns `time.Now()` if the options are configured to include a
// time stamp.
func (o *CreateOptions) createdTime() *time.Time {
if !o.TimeStamp {
return nil
}
now := time.Now()
return &now
}
// Create creates an empty source image at the specified `sourcePath`. Note
// that `sourcePath` must not exist.
func Create(ctx context.Context, sourcePath string, options CreateOptions) error {
if _, err := os.Stat(sourcePath); err == nil {
return fmt.Errorf("creating source image: %q already exists", sourcePath)
}
ociDest, err := openOrCreateSourceImage(ctx, sourcePath)
if err != nil {
return err
}
defer ociDest.Close()
// Create and add a config.
config := ImageConfig{
Author: options.Author,
Created: options.createdTime(),
}
configBlob, err := addConfig(ctx, &config, ociDest)
if err != nil {
return err
}
// Create and write the manifest.
manifest := specV1.Manifest{
Versioned: spec.Versioned{SchemaVersion: 2},
MediaType: specV1.MediaTypeImageManifest,
Config: specV1.Descriptor{
MediaType: MediaTypeSourceImageConfig,
Digest: configBlob.Digest,
Size: configBlob.Size,
},
}
if _, _, err := writeManifest(ctx, &manifest, ociDest); err != nil {
return err
}
return ociDest.Commit(ctx, nil)
}