This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
image_export.go
100 lines (86 loc) · 2.29 KB
/
image_export.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
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
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io"
"os"
"github.com/rkt/rkt/store/imagestore"
"github.com/spf13/cobra"
)
var (
cmdImageExport = &cobra.Command{
Use: "export IMAGE OUTPUT_ACI_FILE",
Short: "Export a stored image to an ACI file",
Long: `IMAGE should be a string referencing an image: either an ID or an image name.
Note that images must be fetched prior to running export and that this command
always returns uncompressed ACIs`,
Run: runWrapper(runImageExport),
}
flagOverwriteACI bool
)
func init() {
cmdImage.AddCommand(cmdImageExport)
cmdImageExport.Flags().BoolVar(&flagOverwriteACI, "overwrite", false, "overwrite output ACI")
}
func runImageExport(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 2 {
cmd.Usage()
return 254
}
s, err := imagestore.NewStore(storeDir())
if err != nil {
stderr.PrintE("cannot open store", err)
return 254
}
key, err := getStoreKeyFromAppOrHash(s, args[0])
if err != nil {
stderr.Error(err)
return 254
}
aci, err := s.ReadStream(key)
if err != nil {
stderr.PrintE("error reading image", err)
return 254
}
defer aci.Close()
mode := os.O_CREATE | os.O_WRONLY
if flagOverwriteACI {
mode |= os.O_TRUNC
} else {
mode |= os.O_EXCL
}
f, err := os.OpenFile(args[1], mode, 0644)
if err != nil {
if os.IsExist(err) {
stderr.Print("output ACI file exists (try --overwrite)")
} else {
stderr.PrintE(fmt.Sprintf("unable to open output ACI file %s", args[1]), err)
}
return 254
}
defer func() {
err := f.Close()
if err != nil {
stderr.PrintE("error closing output ACI file", err)
exit = 1
}
}()
_, err = io.Copy(f, aci)
if err != nil {
stderr.PrintE("error writing to output ACI file", err)
return 254
}
return 0
}