Skip to content

Commit

Permalink
Implement ListVersions in local inventory. References #59
Browse files Browse the repository at this point in the history
  • Loading branch information
bspaans committed Oct 1, 2018
1 parent a140a1f commit 34c2d0b
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions model/inventory/local/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package local

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
Expand All @@ -25,6 +26,7 @@ import (

core "github.com/ankyra/escape-core"
"github.com/ankyra/escape/model/inventory/types"
"github.com/ankyra/escape/util"
)

type LocalInventory struct {
Expand Down Expand Up @@ -77,34 +79,53 @@ func (r *LocalInventory) ListProjects() ([]string, error) {

func (r *LocalInventory) ListApplications(project string) ([]string, error) {
path := filepath.Join(r.BaseDir, project)
if !util.PathExists(path) {
return nil, fmt.Errorf("The project '%s' could not be found in the local inventory at %s.", project, r.BaseDir)
}
result := []string{}
files, err := ioutil.ReadDir(path)
if err != nil {
return result, err
}
appMap := map[string]bool{}
for _, file := range files {
if !file.IsDir() {
if file.IsDir() {
name := file.Name()
if strings.HasSuffix(name, ".json") {
metadata, err := core.NewReleaseMetadataFromFile(filepath.Join(path, name))
if err != nil {
fmt.Printf("WARN: Could not read release metadata from file %s: %s\n", name, err.Error())
continue
}
appMap[metadata.Name] = true
indexPath := filepath.Join(r.BaseDir, project, name, "index.json")
if util.PathExists(indexPath) {
result = append(result, name)
}
}
}
for app, _ := range appMap {
result = append(result, app)
}
sort.Strings(result)
return result, nil
}

type VersionIndex struct {
Name string
EscapeVersion string
CoreVersion string
Versions map[string]*core.ReleaseMetadata
}

func (r *LocalInventory) ListVersions(project, app string) ([]string, error) {
return []string{}, nil
path := filepath.Join(r.BaseDir, project, app, "index.json")
if !util.PathExists(path) {
return nil, fmt.Errorf("The application '%s/%s' could not be found in the local inventory at %s.", project, app, r.BaseDir)
}
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
index := VersionIndex{}
if err := json.Unmarshal(content, &index); err != nil {
return nil, fmt.Errorf("Could not read local version index at '%s': %s", path, err.Error())
}

result := []string{}
for version := range index.Versions {
result = append(result, version)
}
return result, nil
}

// Not required.
Expand Down

0 comments on commit 34c2d0b

Please sign in to comment.