This repository was archived by the owner on Dec 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappstore.go
More file actions
75 lines (63 loc) · 1.39 KB
/
appstore.go
File metadata and controls
75 lines (63 loc) · 1.39 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"io/ioutil"
"log"
"path"
"github.com/docker/libcompose/project"
"github.com/docker/libcompose/project/options"
"golang.org/x/net/context"
)
type AppStore struct {
}
type Installer interface {
Install(appName string)
Uninstall(appName string)
IsInstalled(appName string) bool
}
func NewAppStore() AppStore {
return AppStore{}
}
func (store *AppStore) Install(appName string) {
app := store.FindApp(appName)
if app != nil {
app.Project.Up(context.Background(), options.Up{})
}
}
func (store *AppStore) IsInstalled(appName string) bool {
app := store.FindApp(appName)
if app == nil {
return false
}
containers, _ := app.Project.Containers(context.Background(), project.Filter{})
return len(containers) > 0
}
func (store *AppStore) Uninstall(appName string) {
app := store.FindApp(appName)
if app != nil {
app.Project.Down(context.Background(), options.Down{})
}
}
func (store *AppStore) FindApp(appName string) *App {
for _, app := range store.Apps() {
if app.Name == appName {
return &app
}
}
return nil
}
func (store *AppStore) Apps() []App {
packageDir := "./apps"
files, _ := ioutil.ReadDir(packageDir)
apps := make([]App, 0, len(files))
for _, f := range files {
if f.IsDir() {
app, err := ParseApp(path.Join(packageDir, f.Name()))
if err == nil {
apps = append(apps, app)
} else {
log.Println(err)
}
}
}
return apps
}