-
Notifications
You must be signed in to change notification settings - Fork 231
/
unpack.go
87 lines (67 loc) · 1.94 KB
/
unpack.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
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"os"
"path/filepath"
"github.com/containerd/containerd/namespaces"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
const unpackUsageShortHelp = `Unpack an image to a rootfs directory.`
const unpackUsageLongHelp = `Unpack an image to a rootfs directory.`
func newUnpackCommand() *cobra.Command {
unpack := &unpackCommand{}
cmd := &cobra.Command{
Use: "unpack [OPTIONS] IMAGE",
DisableFlagsInUseLine: true,
SilenceUsage: true,
Short: unpackUsageShortHelp,
Long: unpackUsageLongHelp,
Args: validateUnpackImageArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return unpack.Run(args)
},
}
fs := cmd.Flags()
fs.StringVarP(&unpack.output, "output", "o", "", "Directory to unpack the rootfs to. (defaults to rootfs/ in the current working directory)")
return cmd
}
func validateUnpackImageArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must pass an image to unpack as a rootfs")
}
return nil
}
type unpackCommand struct {
image string
output string
}
func (cmd *unpackCommand) Run(args []string) (err error) {
reexec()
cmd.image = args[0]
if len(cmd.output) < 1 {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("getting current working directory failed: %v", err)
}
cmd.output = filepath.Join(wd, "rootfs")
}
// Create the context.
id := identity.NewID()
ctx := session.NewContext(context.Background(), id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
if err := c.Unpack(ctx, cmd.image, cmd.output); err != nil {
return err
}
fmt.Printf("Successfully unpacked rootfs for %s to: %s\n", cmd.image, cmd.output)
return nil
}