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 pathpack.go
59 lines (49 loc) · 1.42 KB
/
pack.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
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/pkg"
)
// packCmd represents the pack command
var packCmd = &cobra.Command{
Use: "pack [path]",
Aliases: []string{"package"},
Short: "Build a Backpack file (pack) from a directory/template",
Long: `Generate a Backpack file (pack) from a directory containing the various
jobs, metadata and documentation.
The directory should have these 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 unpack [...]" command
`,
Args: cobra.ExactArgs(1),
Run: packRun,
}
func init() {
rootCmd.AddCommand(packCmd)
packCmd.Flags().StringP("file", "f", "", "path of the file to create and write into")
}
func packRun(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
b, err := pkg.GetPackFromDirectory(args[0])
if err != nil {
log.Fatalf("Error generating the backpack from the directory: %s", err)
}
writeTo := filepath.Join(cwd, fmt.Sprintf("%s-%s.backpack", b.Name, b.Version))
fileFlag, _ := cmd.Flags().GetString("file")
if fileFlag != "" {
writeTo = fileFlag
}
err = pkg.WritePackToFile(*b, writeTo)
if err != nil {
log.Fatalf("Error writing to file: %s", err)
}
}