Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config/module: storage path should be independent of system #1418

Merged
merged 4 commits into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions command/module_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type uiModuleStorage struct {
Ui cli.Ui
}

func (s *uiModuleStorage) Dir(source string) (string, bool, error) {
return s.Storage.Dir(source)
func (s *uiModuleStorage) Dir(key string) (string, bool, error) {
return s.Storage.Dir(key)
}

func (s *uiModuleStorage) Get(source string, update bool) error {
func (s *uiModuleStorage) Get(key string, source string, update bool) error {
updateStr := ""
if update {
updateStr = " (update)"
}

s.Ui.Output(fmt.Sprintf("Get: %s%s", source, updateStr))
return s.Storage.Get(source, update)
return s.Storage.Get(key, source, update)
}
12 changes: 6 additions & 6 deletions config/module/folder_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type FolderStorage struct {
}

// Dir implements Storage.Dir
func (s *FolderStorage) Dir(source string) (d string, e bool, err error) {
d = s.dir(source)
func (s *FolderStorage) Dir(key string) (d string, e bool, err error) {
d = s.dir(key)
_, err = os.Stat(d)
if err == nil {
// Directory exists
Expand All @@ -39,8 +39,8 @@ func (s *FolderStorage) Dir(source string) (d string, e bool, err error) {
}

// Get implements Storage.Get
func (s *FolderStorage) Get(source string, update bool) error {
dir := s.dir(source)
func (s *FolderStorage) Get(key string, source string, update bool) error {
dir := s.dir(key)
if !update {
if _, err := os.Stat(dir); err == nil {
// If the directory already exists, then we're done since
Expand All @@ -59,7 +59,7 @@ func (s *FolderStorage) Get(source string, update bool) error {

// dir returns the directory name internally that we'll use to map to
// internally.
func (s *FolderStorage) dir(source string) string {
sum := md5.Sum([]byte(source))
func (s *FolderStorage) dir(key string) string {
sum := md5.Sum([]byte(key))
return filepath.Join(s.StorageDir, hex.EncodeToString(sum[:]))
}
6 changes: 4 additions & 2 deletions config/module/folder_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ func TestFolderStorage(t *testing.T) {
t.Fatal("should not exist")
}

key := "foo"

// We can get it
err = s.Get(module, false)
err = s.Get(key, module, false)
if err != nil {
t.Fatalf("err: %s", err)
}

// Now the module exists
dir, ok, err := s.Dir(module)
dir, ok, err := s.Dir(key)
if err != nil {
t.Fatalf("err: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions config/module/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ type Storage interface {
Dir(string) (string, bool, error)

// Get will download and optionally update the given module.
Get(string, bool) error
Get(string, string, bool) error
}

func getStorage(s Storage, src string, mode GetMode) (string, bool, error) {
func getStorage(s Storage, key string, src string, mode GetMode) (string, bool, error) {
// Get the module with the level specified if we were told to.
if mode > GetModeNone {
if err := s.Get(src, mode == GetModeUpdate); err != nil {
if err := s.Get(key, src, mode == GetModeUpdate); err != nil {
return "", false, err
}
}

// Get the directory where the module is.
return s.Dir(src)
return s.Dir(key)
}
24 changes: 22 additions & 2 deletions config/module/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Tree struct {
name string
config *config.Config
children map[string]*Tree
path []string
lock sync.RWMutex
}

Expand Down Expand Up @@ -152,6 +153,11 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
"module %s: duplicated. module names must be unique", m.Name)
}

// Determine the path to this child
path := make([]string, len(t.path), len(t.path)+1)
copy(path, t.path)
path = append(path, m.Name)

// Split out the subdir if we have one
source, subDir := getDirSubdir(m.Source)

Expand All @@ -167,7 +173,9 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
}

// Get the directory where this module is so we can load it
dir, ok, err := getStorage(s, source, mode)
key := strings.Join(path, ".")
key = "root." + key
dir, ok, err := getStorage(s, key, source, mode)
if err != nil {
return err
}
Expand All @@ -187,6 +195,9 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
return fmt.Errorf(
"module %s: %s", m.Name, err)
}

// Set the path of this child
children[m.Name].path = path
}

// Go through all the children and load them.
Expand All @@ -202,10 +213,19 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
return nil
}

// Path is the full path to this tree.
func (t *Tree) Path() []string {
return t.path
}

// String gives a nice output to describe the tree.
func (t *Tree) String() string {
var result bytes.Buffer
result.WriteString(t.Name() + "\n")
path := strings.Join(t.path, ", ")
if path != "" {
path = fmt.Sprintf(" (path: %s)", path)
}
result.WriteString(t.Name() + path + "\n")

cs := t.Children()
if cs == nil {
Expand Down
3 changes: 3 additions & 0 deletions config/module/tree_gob.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (t *Tree) GobDecode(bs []byte) error {
t.name = data.Name
t.config = data.Config
t.children = data.Children
t.path = data.Path

return nil
}
Expand All @@ -31,6 +32,7 @@ func (t *Tree) GobEncode() ([]byte, error) {
Config: t.config,
Children: t.children,
Name: t.name,
Path: t.path,
}

var buf bytes.Buffer
Expand All @@ -51,4 +53,5 @@ type treeGob struct {
Config *config.Config
Children map[string]*Tree
Name string
Path []string
}
18 changes: 13 additions & 5 deletions config/module/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,35 @@ func TestTreeChild(t *testing.T) {
t.Fatal("should not be nil")
} else if c.Name() != "root" {
t.Fatalf("bad: %#v", c.Name())
} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
t.Fatalf("bad: %#v", c.Path())
}

// Should be able to get the root child
if c := tree.Child(nil); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "root" {
t.Fatalf("bad: %#v", c.Name())
} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
t.Fatalf("bad: %#v", c.Path())
}

// Should be able to get the foo child
if c := tree.Child([]string{"foo"}); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "foo" {
t.Fatalf("bad: %#v", c.Name())
} else if !reflect.DeepEqual(c.Path(), []string{"foo"}) {
t.Fatalf("bad: %#v", c.Path())
}

// Should be able to get the nested child
if c := tree.Child([]string{"foo", "bar"}); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "bar" {
t.Fatalf("bad: %#v", c.Name())
} else if !reflect.DeepEqual(c.Path(), []string{"foo", "bar"}) {
t.Fatalf("bad: %#v", c.Path())
}
}

Expand Down Expand Up @@ -274,16 +282,16 @@ func TestTreeValidate_requiredChildVar(t *testing.T) {

const treeLoadStr = `
root
foo
foo (path: foo)
`

const treeLoadParentStr = `
root
a
b
a (path: a)
b (path: a, b)
`
const treeLoadSubdirStr = `
root
foo
bar
foo (path: foo)
bar (path: foo, bar)
`