Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
renames Backpack{} to Pack{}
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Setale <[email protected]>
  • Loading branch information
koalalorenzo committed Nov 15, 2020
1 parent cedcf4a commit 9b8e6e0
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {

func createRun(cmd *cobra.Command, args []string) {
name := args[0]
b := pkg.Backpack{
b := pkg.Pack{
Name: name,
Version: "0.1.0",
Dependencies: map[string]string{"TODO": "http://backpack.qm64.com/example.backpack"},
Expand Down Expand Up @@ -111,7 +111,7 @@ Have fun! 😄
}
}

err = pkg.UnpackBackpackInDirectory(&b, directory)
err = pkg.UnpackInDirectory(&b, directory)
if err != nil {
log.Fatalf("Error unpacking: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func packRun(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

b, err := pkg.GetBackpackFromDirectory(args[0])
b, err := pkg.GetPackFromDirectory(args[0])
if err != nil {
log.Fatalf("Error generating the backpack from the directory: %s", err)
}
Expand All @@ -51,7 +51,7 @@ func packRun(cmd *cobra.Command, args []string) {
writeTo = fileFlag
}

err = pkg.WriteBackpackToFile(*b, writeTo)
err = pkg.WritePackToFile(*b, writeTo)
if err != nil {
log.Fatalf("Error writing to file: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {

// This is the actual command..
func planRun(cmd *cobra.Command, args []string) {
b := getBackpackFromCLIInput(cmd, args)
b := getPackFromCLIInput(cmd, args)
var err error

client, err := conn.NewClient()
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {

// This is the actual command..
func runRun(cmd *cobra.Command, args []string) {
b := getBackpackFromCLIInput(cmd, args)
b := getPackFromCLIInput(cmd, args)
var err error

client, err := conn.NewClient()
Expand Down
4 changes: 2 additions & 2 deletions cmd/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
func unpackRun(cmd *cobra.Command, args []string) {
// get a file from URL or Path
p := getAUsablePathOfFile(args[0])
b, err := pkg.GetBackpackFromFile(p)
b, err := pkg.GetPackFromFile(p)
if err != nil {
log.Fatalf("Error parsing the backpack: %s", err)
}
Expand All @@ -64,7 +64,7 @@ func unpackRun(cmd *cobra.Command, args []string) {
}
}

err = pkg.UnpackBackpackInDirectory(&b, directory)
err = pkg.UnpackInDirectory(&b, directory)
if err != nil {
log.Fatalf("Error unpacking: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/unpack_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func unpackDocsRun(cmd *cobra.Command, args []string) {
// get a file from URL or Path
p := getAUsablePathOfFile(args[0])

b, err := pkg.GetBackpackFromFile(p)
b, err := pkg.GetPackFromFile(p)
if err != nil {
log.Fatalf("Error parsing the backpack: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/unpack_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func unpackValuesRun(cmd *cobra.Command, args []string) {
// get a file from URL or Path
p := getAUsablePathOfFile(args[0])

b, err := pkg.GetBackpackFromFile(p)
b, err := pkg.GetPackFromFile(p)
if err != nil {
log.Fatalf("Error parsing the backpack: %s", err)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func getAUsablePathOfFile(v string) string {
}
}

func getBackpackFromCLIInput(cmd *cobra.Command, args []string) pkg.Backpack {
b := pkg.Backpack{}
func getPackFromCLIInput(cmd *cobra.Command, args []string) pkg.Pack {
b := pkg.Pack{}

readFromDir, err := cmd.Flags().GetBool("unpacked")
if err != nil {
Expand All @@ -87,15 +87,15 @@ func getBackpackFromCLIInput(cmd *cobra.Command, args []string) pkg.Backpack {
// get a file from URL or Path
p := getAUsablePathOfFile(args[0])

b, err = pkg.GetBackpackFromFile(p)
b, err = pkg.GetPackFromFile(p)
if err != nil {
log.Fatalf("Error parsing the backpack: %s", err)
log.Fatalf("Error parsing the pack: %s", err)
}
} else {
// If we have to read from directory instead args[0] is a path
d, err := pkg.GetBackpackFromDirectory(args[0])
d, err := pkg.GetPackFromDirectory(args[0])
if err != nil {
log.Fatalf("Error parsing the unpacked backpack: %s", err)
log.Fatalf("Error parsing the unpacked pack: %s", err)
}
b = *d
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/backpack.go → pkg/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/hashicorp/go-multierror"
)

// Backpack is the structure of the package/file that will use to export, share
// Pack is the structure of the package/file that will use to export, share
// exchange templates, docs and configuration
type Backpack struct {
type Pack struct {
Name string `yaml:"name"`
Version string `yaml:"version"` // Please use semver
Dependencies map[string]string `yaml:",omitempty"` // URLs for dependencies? TBD
Expand Down Expand Up @@ -39,7 +39,7 @@ type Backpack struct {
// SortTemplates is used to ensure that files names sorting is respected.
// This is useful to define an "order" to follow when applying resources if that
// is needed.
func (b *Backpack) SortTemplates() {
func (b *Pack) SortTemplates() {
nm := make(FilesMapType, len(b.Templates))
sk := make([]string, 0, len(b.Templates))

Expand All @@ -59,7 +59,7 @@ func (b *Backpack) SortTemplates() {
}

// FilesMapType is useful type to specify what kind of Mapping we are using to
// store files in the backpack.
// store files in the pack.
type FilesMapType map[string][]byte

func decodeB64FilesMap(ra FilesMapType) (x FilesMapType, err error) {
Expand Down
17 changes: 9 additions & 8 deletions pkg/backpack_file.go → pkg/pack_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,43 @@ import (
"github.com/vmihailenco/msgpack"
)

// GetBackpackFromFile will read and get a Backpack from a file path
func GetBackpackFromFile(path string) (b Backpack, err error) {
// GetPackFromFile will read and get a Pack from a file path and return as a
// Pack struct
func GetPackFromFile(path string) (b Pack, err error) {
bb, err := ioutil.ReadFile(path)
if err != nil {
return
}

err = msgpack.Unmarshal(bb, &b)
if err != nil {
return Backpack{}, err
return Pack{}, err
}

// Decode Templates to Base64
b.Templates, err = decodeB64FilesMap(b.Templates)
if err != nil {
return Backpack{}, err
return Pack{}, err
}
b.SortTemplates()

// Decode Documentation
b.Documentation, err = decodeB64FilesMap(b.Documentation)
if err != nil {
return Backpack{}, err
return Pack{}, err
}

// Decode Default Values
b.DefaultValues, err = base64.StdEncoding.DecodeString(string(b.DefaultValues))
if err != nil {
return Backpack{}, err
return Pack{}, err
}

return
}

// WriteBackpackToFile will write a Backpack to file
func WriteBackpackToFile(b Backpack, path string) (err error) {
// WritePackToFile will write a Pack to file
func WritePackToFile(b Pack, path string) (err error) {
// Encode to base64
b.Templates, _ = encodeB64FilesMap(b.Templates)
b.Documentation, _ = encodeB64FilesMap(b.Documentation)
Expand Down
6 changes: 3 additions & 3 deletions pkg/backpack_file_test.go → pkg/pack_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"testing"
)

func TestBackpackWrite(t *testing.T) {
func TestPackWrite(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "backpack-")
defer os.Remove(tmpFile.Name())
if err != nil {
t.Fatal(err)
}

b := Backpack{
b := Pack{
Name: "hello-world",
Version: "0.1.0",
DefaultValues: []byte(`datacenters: ["dc1", "dc2"]`),
}

err = WriteBackpackToFile(b, tmpFile.Name())
err = WritePackToFile(b, tmpFile.Name())
if err != nil {
t.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/backpack_pack.go → pkg/pack_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func getAllFileWithExtension(ext, basePath string) (FilesMapType, error) {
return tempMap, nil
}

// GetBackpackFromDirectory will look in a given path for backpack.yaml and .nomad
// files to Backpack them together.
func GetBackpackFromDirectory(dirPath string) (b *Backpack, err error) {
b = &Backpack{}
// GetPackFromDirectory will look in a given path for backpack.yaml and .nomad
// files to pack them together in a Pack struct.
func GetPackFromDirectory(dirPath string) (b *Pack, err error) {
b = &Pack{}

// Get the backpack.yaml file
bpBytes, err := ioutil.ReadFile(filepath.Join(dirPath, "backpack.yaml"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/backpack_pack_test.go → pkg/pack_pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"gopkg.in/yaml.v2"
)

func TestBackpackPackDirectory(t *testing.T) {
func TestPackPackDirectory(t *testing.T) {
// extract the test file name
_, filename, _, _ := runtime.Caller(0)
bundlePackageGoDir := filepath.Dir(filename)
bundleTestDirPath := filepath.Join(bundlePackageGoDir, "../test_files/backpack/")

b, err := GetBackpackFromDirectory(bundleTestDirPath)
b, err := GetPackFromDirectory(bundleTestDirPath)
assert.NoError(t, err)

assert.Equal(t, "redis", b.Name)
Expand Down
4 changes: 2 additions & 2 deletions pkg/backpack_unpack.go → pkg/pack_unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"gopkg.in/yaml.v2"
)

// UnpackBackpackInDirectory will write the Backpack's backpack into a directory.
func UnpackBackpackInDirectory(b *Backpack, dirPath string) (err error) {
// UnpackInDirectory will extract files of a pack into a directory.
func UnpackInDirectory(b *Pack, dirPath string) (err error) {
// Unpack Templates
for n, f := range b.Templates {
terr := ioutil.WriteFile(filepath.Join(dirPath, n), f, 0744)
Expand Down
6 changes: 3 additions & 3 deletions pkg/backpack_unpack_test.go → pkg/pack_unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBackpackUnpack(t *testing.T) {
func TestPackUnpack(t *testing.T) {
// extract the test file name
_, filename, _, _ := runtime.Caller(0)
bundlePackageGoDir := filepath.Dir(filename)
Expand All @@ -22,11 +22,11 @@ func TestBackpackUnpack(t *testing.T) {
assert.NoError(t, err)

// Get the bundle
b, err := GetBackpackFromFile(filepath.Join(bundleTestDirPath, "redis.backpack"))
b, err := GetPackFromFile(filepath.Join(bundleTestDirPath, "redis.backpack"))
assert.NoError(t, err)

t.Logf("Temp dir: %s", tempDir)
err = UnpackBackpackInDirectory(&b, tempDir)
err = UnpackInDirectory(&b, tempDir)
assert.NoError(t, err)

expectations := map[string]string{
Expand Down
6 changes: 3 additions & 3 deletions templating/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"gitlab.com/qm64/backpack/pkg"
)

// BuildHCL will gather the Backpack templates, the default values and
// custom values to generate proper HCL that can be sent to nomad
func BuildHCL(bpk *pkg.Backpack, cv pkg.ValuesType) (o map[string][]byte, err error) {
// BuildHCL will gather the pack templates, the default values and custom values
// to generate proper HCL that can be sent to nomad
func BuildHCL(bpk *pkg.Pack, cv pkg.ValuesType) (o map[string][]byte, err error) {
// Get the default map properly from the raw bytes that contains comments
dvm := map[string]interface{}{}
err = yaml.Unmarshal(bpk.DefaultValues, dvm)
Expand Down
2 changes: 1 addition & 1 deletion templating/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestBuildHCL(t *testing.T) {
bp := pkg.Backpack{
bp := pkg.Pack{
Name: "test-backpack",
Version: "0.1.0",
Templates: map[string][]byte{
Expand Down
2 changes: 1 addition & 1 deletion test_files/backpack/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Documentation

The documentation for this backpack is also part of the test!
The documentation for this pack is also part of the test!

0 comments on commit 9b8e6e0

Please sign in to comment.