forked from ksonnet/ksonnet
/
manager.go
151 lines (121 loc) · 4.36 KB
/
manager.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package component
import (
"path"
"path/filepath"
"strings"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/prototype"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
func ResolvePath(ksApp app.App, path string) (Module, Component, error) {
isDir, err := isComponentDir2(ksApp, path)
if err != nil {
return nil, nil, errors.Wrap(err, "check for namespace directory")
}
if isDir {
ns, err := GetModule(ksApp, path)
if err != nil {
return nil, nil, err
}
return ns, nil, nil
}
module, cName, err := checkComponent(ksApp, path)
if err != nil {
return nil, nil, err
}
ns, err := GetModule(ksApp, module)
if err != nil {
return nil, nil, err
}
c, err := LocateComponent(ksApp, module, cName)
if err != nil {
return nil, nil, err
}
return ns, c, nil
}
var (
// DefaultManager is the default manager for components.
DefaultManager = &defaultManager{}
)
// Manager is an interface for interating with components.
type Manager interface {
Components(ns Module) ([]Component, error)
Component(ksApp app.App, module, componentName string) (Component, error)
CreateComponent(ksApp app.App, name, text string, params param.Params, templateType prototype.TemplateType) (string, error)
CreateModule(ksApp app.App, name string) error
Module(ksApp app.App, moduleName string) (Module, error)
Modules(ksApp app.App, envName string) ([]Module, error)
NSResolveParams(ns Module) (string, error)
}
type defaultManager struct{}
var _ Manager = (*defaultManager)(nil)
func (dm *defaultManager) Modules(ksApp app.App, envName string) ([]Module, error) {
return ModulesFromEnv(ksApp, envName)
}
func (dm *defaultManager) Module(ksApp app.App, module string) (Module, error) {
return GetModule(ksApp, module)
}
func (dm *defaultManager) NSResolveParams(ns Module) (string, error) {
return ns.ResolvedParams()
}
func (dm *defaultManager) Components(ns Module) ([]Component, error) {
return ns.Components()
}
func (dm *defaultManager) Component(ksApp app.App, module, componentName string) (Component, error) {
return LocateComponent(ksApp, module, componentName)
}
func (dm *defaultManager) CreateComponent(ksApp app.App, name, text string, params param.Params, templateType prototype.TemplateType) (string, error) {
return Create(ksApp, name, text, params, templateType)
}
func (dm *defaultManager) CreateModule(ksApp app.App, name string) error {
parts := strings.Split(name, "/")
dir := filepath.Join(append([]string{ksApp.Root(), "components"}, parts...)...)
if err := ksApp.Fs().MkdirAll(dir, app.DefaultFolderPermissions); err != nil {
return err
}
paramsDir := filepath.Join(dir, "params.libsonnet")
return afero.WriteFile(ksApp.Fs(), paramsDir, GenParamsContent(), app.DefaultFilePermissions)
}
func isComponentDir2(ksApp app.App, path string) (bool, error) {
parts := strings.Split(path, "/")
dir := filepath.Join(append([]string{ksApp.Root(), componentsRoot}, parts...)...)
dir = filepath.Clean(dir)
return afero.DirExists(ksApp.Fs(), dir)
}
func checkComponent(ksApp app.App, name string) (string, string, error) {
parts := strings.Split(name, "/")
base := filepath.Join(append([]string{ksApp.Root(), componentsRoot}, parts...)...)
base = filepath.Clean(base)
exts := []string{".yaml", ".jsonnet", ".json"}
for _, ext := range exts {
exists, err := afero.Exists(ksApp.Fs(), base+ext)
if err != nil {
return "", "", errors.Wrap(err, "check for component")
}
if exists {
dir, file := path.Split(base)
module := strings.TrimPrefix(dir, path.Join(ksApp.Root(), componentsRoot))
if len(module) > 0 {
module = strings.TrimSuffix(module, "/")
}
return module, file, nil
}
}
return "", "", errors.Errorf("%q is not a component or a module", name)
}