forked from anz-bank/sysl
/
sysl.go
213 lines (186 loc) · 5.2 KB
/
sysl.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package command
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Joshcarp/sysl/pkg/parse"
sysl "github.com/Joshcarp/sysl/pkg/sysl"
"github.com/Joshcarp/sysl/pkg/syslutil"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"gopkg.in/alecthomas/kingpin.v2"
)
// Version - Binary version
// GitCommit - Commit SHA of the source code
// BuildDate - Binary build date
// BuildOS - Operating System used to build binary
//nolint:gochecknoglobals
var (
Version = "unspecified"
GitCommit = "unspecified"
BuildDate = "unspecified"
BuildOS = "unspecified"
)
const debug string = "debug"
type projectConfiguration struct {
module, root string
rootIsFound bool
fs afero.Fs
}
func LoadSyslModule(root, filename string, fs afero.Fs, logger *logrus.Logger) (*sysl.Module, string, error) {
logger.Debugf("Attempting to load module:%s (root:%s)", filename, root)
projectConfig := newProjectConfiguration()
if err := projectConfig.configureProject(root, filename, fs, logger); err != nil {
return nil, "", err
}
modelParser := parse.NewParser()
if !projectConfig.rootIsFound {
modelParser.RestrictToLocalImport()
}
return parse.LoadAndGetDefaultApp(projectConfig.module, projectConfig.fs, modelParser)
}
func newProjectConfiguration() *projectConfiguration {
return &projectConfiguration{
root: "",
module: "",
rootIsFound: false,
fs: nil,
}
}
func (pc *projectConfiguration) configureProject(root, module string, fs afero.Fs, logger *logrus.Logger) error {
rootIsDefined := root != ""
modulePath := module
if rootIsDefined {
modulePath = filepath.Join(root, module)
}
syslRootPath, err := findRootFromSyslModule(modulePath, fs)
if err != nil {
return err
}
rootMarkerExists := syslRootPath != ""
if rootIsDefined {
pc.rootIsFound = true
pc.root = root
pc.module = module
if rootMarkerExists {
logger.Warningf("%s found in %s but will use %s instead",
syslRootMarker, syslRootPath, pc.root)
} else {
logger.Warningf("%s is not defined but root flag is defined in %s",
syslRootMarker, pc.root)
}
} else {
if rootMarkerExists {
pc.root = syslRootPath
// module has to be relative to the root
absModulePath, err := filepath.Abs(module)
if err != nil {
return err
}
pc.module, err = filepath.Rel(pc.root, absModulePath)
if err != nil {
return err
}
pc.rootIsFound = true
} else {
// uses the module directory as the root, changing the module to be relative to the root
pc.root = filepath.Dir(module)
pc.module = filepath.Base(module)
pc.rootIsFound = false
logger.Warningf("root and %s are undefined, %s will be used instead",
syslRootMarker, pc.root)
}
}
pc.fs = syslutil.NewChrootFs(fs, pc.root)
return nil
}
func findRootFromSyslModule(modulePath string, fs afero.Fs) (string, error) {
currentPath, err := filepath.Abs(modulePath)
if err != nil {
return "", err
}
systemRoot, err := filepath.Abs(string(os.PathSeparator))
if err != nil {
return "", err
}
// Keep walking up the directories to find nearest root marker
for {
currentPath = filepath.Dir(currentPath)
exists, err := afero.Exists(fs, filepath.Join(currentPath, syslRootMarker))
reachedRoot := currentPath == systemRoot || (err != nil && os.IsPermission(err))
switch {
case exists:
return currentPath, nil
case reachedRoot:
return "", nil
case err != nil:
return "", err
}
}
}
// Main3 is the real main function. It takes its output streams and command-line
// arguments as parameters to support testability.
func Main3(args []string, fs afero.Fs, logger *logrus.Logger) error {
syslCmd := kingpin.New("sysl", "System Modelling Language Toolkit")
syslCmd.Version(Version)
syslCmd.UsageTemplate(kingpin.SeparateOptionalFlagsUsageTemplate)
(&debugTypeData{}).add(syslCmd)
runner := cmdRunner{}
if err := runner.Configure(syslCmd); err != nil {
return err
}
selectedCommand, err := syslCmd.Parse(args[1:])
if err != nil {
return err
}
return runner.Run(selectedCommand, fs, logger)
}
type debugTypeData struct {
loglevel string
verbose bool
}
//nolint:unparam
func (d *debugTypeData) do(_ *kingpin.ParseContext) error {
if d.verbose {
d.loglevel = debug
}
// Default info
if level, has := syslutil.LogLevels[d.loglevel]; has {
logrus.SetLevel(level)
}
logrus.Debugf("Logging: %+v", *d)
return nil
}
func (d *debugTypeData) add(app *kingpin.Application) {
var levels []string
for l := range syslutil.LogLevels {
if l != "" {
levels = append(levels, l)
}
}
app.Flag("log", fmt.Sprintf("log level: [%s]", strings.Join(levels, ","))).
HintOptions(levels...).
Default("warn").
StringVar(&d.loglevel)
app.Flag("verbose", "enable verbose logging").Short('v').BoolVar(&d.verbose)
app.PreAction(d.do)
}
// Main2 calls Main3 and handles any errors it returns. It takes its output
// streams and command-line arguments and even Main3 as parameters to support
// testability.
func Main2(
args []string,
fs afero.Fs,
logger *logrus.Logger,
main3 func(args []string, fs afero.Fs, logger *logrus.Logger) error,
) int {
if err := main3(args, fs, logger); err != nil {
logger.Errorln(err.Error())
if err, ok := err.(parse.Exit); ok {
return err.Code
}
return 1
}
return 0
}