| 10 | ) |
| 11 | |
| 12 | func cacheImportCmd() *cobra.Command { |
| 13 | var ( |
| 14 | inputFile string |
| 15 | ) |
| 16 | cmd := &cobra.Command{ |
| 17 | Use: "import", |
| 18 | Short: "import individual images to the linuxkit cache", |
| 19 | Long: `Import individual images from tar file to the linuxkit cache. |
| 20 | Can provide the file on the command-line or via stdin with filename '-'. |
| 21 | |
| 22 | Example: |
| 23 | linuxkit cache import myimage.tar |
| 24 | cat myimage.tar | linuxkit cache import - |
| 25 | |
| 26 | Tarfile format must be the OCI v1 file format, see https://github.com/opencontainers/image-spec/blob/main/image-layout.md |
| 27 | `, |
| 28 | Args: cobra.MinimumNArgs(1), |
| 29 | RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | paths := args |
| 31 | infile := paths[0] |
| 32 | |
| 33 | p, err := cachepkg.NewProvider(cacheDir) |
| 34 | if err != nil { |
| 35 | log.Fatalf("unable to read a local cache: %v", err) |
| 36 | } |
| 37 | |
| 38 | var reader io.ReadCloser |
| 39 | if inputFile == "-" { |
| 40 | reader = os.Stdin |
| 41 | } else { |
| 42 | f, err := os.Open(infile) |
| 43 | if err != nil { |
| 44 | log.Fatalf("unable to open %s: %v", infile, err) |
| 45 | } |
| 46 | defer func() { _ = f.Close() }() |
| 47 | reader = f |
| 48 | } |
| 49 | defer func() { _ = reader.Close() }() |
| 50 | |
| 51 | if _, err := p.ImageLoad(reader); err != nil { |
| 52 | log.Fatalf("unable to load image: %v", err) |
| 53 | } |
| 54 | |
| 55 | return err |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | return cmd |
| 60 | } |