This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunpack.go
72 lines (61 loc) · 1.97 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
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/pkg"
)
// unpackCmd represents the unpack command
var unpackCmd = &cobra.Command{
Use: "unpack [file.backpack]",
Aliases: []string{"pull"},
Args: cobra.ExactArgs(1),
Run: unpackRun,
Short: "Opens a pack file to explore content",
Long: `Explodes/Open the pack inside a directory. This is useful to edit a
pack, inspecting it or seeing default values... or if you are looking for
something inside it and you know it is at the bottom of the backpack!
This command accepts one argument that is the path of a pack to extract the data
from (path or URL). Unless specified via -d or --dir, the files will be
extracted in a new directory in the CWD, with the name and version of the
original pack.
A pack includes the following files:
- backpack.yaml (containing metadata)
- values.yaml (containing the default values for the templates)
- *.nomad (representing the various go templates of Nomad Jobs)
- *.md (useful documentation)
This command performs the opposite of "backpack pack [...]" command.
`,
}
func init() {
rootCmd.AddCommand(unpackCmd)
unpackCmd.Flags().StringP("dir", "d", "", "specifies the directory to write into")
}
func unpackRun(cmd *cobra.Command, args []string) {
// get a file from URL or Path
p := getAUsablePathOfFile(args[0])
b, err := pkg.GetPackFromFile(p)
if err != nil {
log.Fatalf("Error parsing the backpack: %s", err)
}
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// Checks if a custom directory has been specified, otherwise unpack in
// the backpack name.
directory, _ := cmd.Flags().GetString("dir")
if directory == "" {
directory = filepath.Join(cwd, fmt.Sprintf("%s-%s", b.Name, b.Version))
err = os.Mkdir(directory, 0744)
if err != nil {
log.Fatalf("Error creating directory: %s", err)
}
}
err = pkg.UnpackInDirectory(&b, directory)
if err != nil {
log.Fatalf("Error unpacking: %s", err)
}
}