-
Notifications
You must be signed in to change notification settings - Fork 39
/
layout.go
181 lines (160 loc) · 4.86 KB
/
layout.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2016 Palantir Technologies, Inc.
//
// 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 layout
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
s "github.com/palantir/pkg/specdir"
)
// AllPaths returns a map that contains all of the paths in the provided directory. The paths are relative to the
// directory. The boolean key is true if the path is a directory, false otherwise.
func AllPaths(dir string) (map[string]bool, error) {
m := make(map[string]bool)
return m, allPaths(m, nil, dir)
}
func allPaths(paths map[string]bool, pathStack []string, dir string) error {
fis, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range fis {
p := filepath.Join(dir, fi.Name())
key := filepath.Join(strings.Join(pathStack, "/"), fi.Name())
// record current path
paths[key] = fi.IsDir()
if fi.IsDir() {
// if path is directory, recursively record paths
if err := allPaths(paths, append(pathStack, fi.Name()), p); err != nil {
return err
}
}
}
return nil
}
func GodelDistLayout(version string, mode s.Mode) (s.SpecDir, error) {
rootDir, err := GodelHomePath()
if err != nil {
return nil, err
}
values := s.TemplateValues{
godelHomeTemplate: filepath.Base(rootDir),
versionTemplate: version,
}
for key, value := range AppSpecTemplate(version) {
values[key] = value
}
return s.New(rootDir, godelHomeSpec([]s.FileNodeProvider{AppSpec()}), values, mode)
}
const (
godelHomeTemplate = "godel-home"
godelHomeEnvVar = "GODEL_HOME"
defaultGodelHome = ".godel"
AssetsDir = "assets"
CacheDir = "cache"
DistsDir = "dists"
ConfigsDir = "configs"
DownloadsDir = "downloads"
PluginsDir = "plugins"
)
// GodelHomePath returns the path to the gödel home directory. If $GODEL_HOME is set as an environment variable, that
// value is used. Otherwise, the value is "$HOME/{{defaultGodelHome}}"
func GodelHomePath() (string, error) {
// check the environment variable
if godelHomeDir := os.Getenv(godelHomeEnvVar); godelHomeDir != "" {
return godelHomeDir, nil
}
// if not present, create from home directory
if userHomeDir := os.Getenv("HOME"); userHomeDir != "" {
return filepath.Join(userHomeDir, defaultGodelHome), nil
}
return "", fmt.Errorf("failed to get %s home directory", AppName)
}
func GodelHomeSpecDir(mode s.Mode) (s.SpecDir, error) {
rootDir, err := GodelHomePath()
if err != nil {
return nil, err
}
values := s.TemplateValues{
godelHomeTemplate: filepath.Base(rootDir),
}
return s.New(rootDir, GodelHomeSpec(), values, mode)
}
func GodelHomeSpec() s.LayoutSpec {
return godelHomeSpec(nil)
}
func godelHomeSpec(providers []s.FileNodeProvider) s.LayoutSpec {
return s.NewLayoutSpec(
s.Dir(s.TemplateName(godelHomeTemplate), "",
s.Dir(s.LiteralName("assets"), AssetsDir),
s.Dir(s.LiteralName("cache"), CacheDir),
s.Dir(s.LiteralName("configs"), ConfigsDir),
s.Dir(s.LiteralName("dists"), DistsDir, providers...),
s.Dir(s.LiteralName("downloads"), DownloadsDir),
s.Dir(s.LiteralName("plugins"), PluginsDir),
),
true,
)
}
const (
AppName = "godel"
AppDir = "gödel-app"
AppExecutable = "app-executable"
osTemplate = "os"
archTemplate = "arch"
versionTemplate = "version"
)
func AppSpecDir(rootDir, version string) (s.SpecDir, error) {
return s.New(rootDir, AppSpec(), AppSpecTemplate(version), s.Validate)
}
func AppSpec() s.LayoutSpec {
return s.NewLayoutSpec(
s.Dir(s.CompositeName(s.LiteralName(AppName+"-"), s.TemplateName(versionTemplate)), AppDir,
s.Dir(s.LiteralName("bin"), "",
s.Dir(s.CompositeName(s.TemplateName(osTemplate), s.LiteralName("-"), s.TemplateName(archTemplate)), "",
s.File(s.LiteralName(AppName), AppExecutable),
),
),
WrapperSpec(),
),
true,
)
}
func AppSpecTemplate(version string) s.TemplateValues {
return s.TemplateValues{
versionTemplate: version,
osTemplate: runtime.GOOS,
archTemplate: runtime.GOARCH,
}
}
const (
WrapperDir = "wrapper-dir"
WrapperScriptFile = "wrapper-script"
WrapperAppDir = "wrapper-app"
WrapperConfigDir = "config"
WrapperName = "godelw"
)
func WrapperSpec() s.LayoutSpec {
return s.NewLayoutSpec(
s.Dir(s.LiteralName("wrapper"), WrapperDir,
s.File(s.LiteralName(WrapperName), WrapperScriptFile),
s.Dir(s.LiteralName(AppName), WrapperAppDir,
s.Dir(s.LiteralName(WrapperConfigDir), WrapperConfigDir),
),
),
false,
)
}