-
Notifications
You must be signed in to change notification settings - Fork 6
/
archive_merge.go
134 lines (119 loc) · 3.3 KB
/
archive_merge.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
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
package commands
import (
"fmt"
"os"
"strings"
"github.com/cnrancher/hangar/pkg/hangar/archive"
"github.com/cnrancher/hangar/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type archiveMergeOpts struct {
files []string
output string
autoYes bool
}
type archiveMergeCmd struct {
*baseCmd
*archiveMergeOpts
}
func newArchiveMergeCmd() *archiveMergeCmd {
cc := &archiveMergeCmd{
archiveMergeOpts: &archiveMergeOpts{},
}
cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "merge",
Short: "Merge multiple hangar archive files into one new archive file",
Long: "",
Example: `
# Merge multiple archive files
hangar archive merge \
--file ARCHIVE_1.zip \
--file ARCHIVE_2.zip \
--output MERGE_OUTPUT.zip`,
PreRun: func(cmd *cobra.Command, args []string) {
if cc.debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("Debug output enabled")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := cc.run(); err != nil {
return err
}
return nil
},
})
flags := cc.baseCmd.cmd.Flags()
flags.StringSliceVarP(&cc.files, "file", "f", nil, "archive file path")
flags.StringVarP(&cc.output, "output", "o", "", "output archive file")
flags.BoolVarP(&cc.autoYes, "auto-yes", "y", false, "answer yes automatically (used in shell script)")
return cc
}
func (cc *archiveMergeCmd) run() error {
if len(cc.files) == 0 {
return fmt.Errorf("archive files not provided, use '--file' option to specify at least 2 archive files")
}
if len(cc.files) < 2 {
return fmt.Errorf("use '--file' option to specify at least 2 archive files")
}
if cc.output == "" {
return fmt.Errorf("output file not provided, use '--output' to specify the output file")
}
if err := cc.merge(); err != nil {
return err
}
return nil
}
func (cc *archiveMergeCmd) merge() error {
var (
readers []*archive.Reader
writer *archive.Writer
err error
newIndex = archive.NewIndex()
)
for _, fn := range cc.files {
if _, err := os.Stat(fn); err != nil {
return fmt.Errorf("stat %q: %w", fn, err)
}
ar, err := archive.NewReader(fn)
if err != nil {
return fmt.Errorf("failed to create reader for %q: %w", fn, err)
}
readers = append(readers, ar)
defer ar.Close()
}
if len(readers) == 0 {
return fmt.Errorf("no archive files to merge")
}
if err := utils.CheckFileExistsPrompt(signalContext, cc.output, cc.autoYes); err != nil {
return err
}
writer, err = archive.NewWriter(cc.output)
if err != nil {
return fmt.Errorf("failed to create writer for %q: %w", cc.output, err)
}
defer writer.Close()
for _, reader := range readers {
ib, err := reader.Index()
if err != nil {
return fmt.Errorf("failed to load index: %w", err)
}
i := archive.NewIndex()
if err := i.Unmarshal(ib); err != nil {
return fmt.Errorf("failed to read index data: %w", err)
}
for _, img := range i.List {
if err := writer.CopyImage(img, reader); err != nil {
return fmt.Errorf("failed to copy image [%v:%v]: %w", img.Source, img.Tag, err)
}
newIndex.Append(img)
logrus.Infof("Copy [%s:%s]", img.Source, img.Tag)
}
}
if err := writer.WriteIndex(newIndex); err != nil {
return fmt.Errorf("failed to write index to %q: %w", cc.output, err)
}
logrus.Infof("Merged archive files [%v] to %q", strings.Join(cc.files, ","), cc.output)
return nil
}