From f084d8d93213776108cc15ab243c8bfa9fec3f8b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Apr 2015 16:37:46 -0700 Subject: [PATCH 1/4] config/module: store the path with the module --- config/module/tree.go | 17 ++++++++++++++++- config/module/tree_gob.go | 3 +++ config/module/tree_test.go | 18 +++++++++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/config/module/tree.go b/config/module/tree.go index fbc4673176..08f73b0e48 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -23,6 +23,7 @@ type Tree struct { name string config *config.Config children map[string]*Tree + path []string lock sync.RWMutex } @@ -187,6 +188,11 @@ func (t *Tree) Load(s Storage, mode GetMode) error { return fmt.Errorf( "module %s: %s", m.Name, err) } + + // Set the path of this child + path := make([]string, len(t.path), len(t.path)+1) + copy(path, t.path) + children[m.Name].path = append(path, m.Name) } // Go through all the children and load them. @@ -202,10 +208,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 { diff --git a/config/module/tree_gob.go b/config/module/tree_gob.go index cbf8a25ed0..fcd37f4e71 100644 --- a/config/module/tree_gob.go +++ b/config/module/tree_gob.go @@ -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 } @@ -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 @@ -51,4 +53,5 @@ type treeGob struct { Config *config.Config Children map[string]*Tree Name string + Path []string } diff --git a/config/module/tree_test.go b/config/module/tree_test.go index 9ac0c582da..9667c0157c 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -18,6 +18,8 @@ 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 @@ -25,6 +27,8 @@ 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 foo child @@ -32,6 +36,8 @@ func TestTreeChild(t *testing.T) { 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 @@ -39,6 +45,8 @@ func TestTreeChild(t *testing.T) { 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()) } } @@ -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) ` From 23781608039111f7c51f3c4c327f33c47116c73b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Apr 2015 16:44:24 -0700 Subject: [PATCH 2/4] config/module: determine storage folder by path --- config/module/folder_storage.go | 4 ++-- config/module/folder_storage_test.go | 6 ++++-- config/module/storage.go | 8 ++++---- config/module/tree.go | 13 +++++++++---- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/config/module/folder_storage.go b/config/module/folder_storage.go index dfb79748af..4c47ba294a 100644 --- a/config/module/folder_storage.go +++ b/config/module/folder_storage.go @@ -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 diff --git a/config/module/folder_storage_test.go b/config/module/folder_storage_test.go index 4ffaac2bb1..7fda6b21a4 100644 --- a/config/module/folder_storage_test.go +++ b/config/module/folder_storage_test.go @@ -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) } diff --git a/config/module/storage.go b/config/module/storage.go index dcb0cc57c8..9c752f6309 100644 --- a/config/module/storage.go +++ b/config/module/storage.go @@ -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) } diff --git a/config/module/tree.go b/config/module/tree.go index 08f73b0e48..d7b3ac9661 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -153,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) @@ -168,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 } @@ -190,9 +197,7 @@ func (t *Tree) Load(s Storage, mode GetMode) error { } // Set the path of this child - path := make([]string, len(t.path), len(t.path)+1) - copy(path, t.path) - children[m.Name].path = append(path, m.Name) + children[m.Name].path = path } // Go through all the children and load them. From 650a9f372b60a0d68855db8dcfd9f78751424569 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Apr 2015 16:46:56 -0700 Subject: [PATCH 3/4] command: implement proper module.Storage interface --- command/module_storage.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/command/module_storage.go b/command/module_storage.go index 846942aaaf..e17786a807 100644 --- a/command/module_storage.go +++ b/command/module_storage.go @@ -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) } From aab7bc0da53ac83330f6ebb08cc9a68266134bf7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Apr 2015 17:07:50 -0700 Subject: [PATCH 4/4] config/module: rename parameters --- config/module/folder_storage.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/module/folder_storage.go b/config/module/folder_storage.go index 4c47ba294a..81c9a2ac19 100644 --- a/config/module/folder_storage.go +++ b/config/module/folder_storage.go @@ -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 @@ -59,7 +59,7 @@ func (s *FolderStorage) Get(key string, 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[:])) }