-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (140 loc) · 3.59 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/go-resty/resty/v2"
"github.com/kozmod/progen/internal/exec"
"github.com/kozmod/progen/internal/factory"
"golang.org/x/xerrors"
"github.com/kozmod/progen/internal"
"github.com/kozmod/progen/internal/config"
"github.com/kozmod/progen/internal/entity"
"github.com/kozmod/progen/internal/flag"
)
func main() {
flags := flag.Parse()
if flags.Version {
fmt.Println(internal.GetVersion())
return
}
logFatalSuffixFn := entity.NewAppendVPlusOrV(flags.PrintErrorStackTrace)
logger, err := factory.NewLogger(flags.Verbose)
if err != nil {
log.Fatalf(logFatalSuffixFn("create logger: "), err)
}
defer func() {
_ = logger.Sync()
}()
{
if err = os.Chdir(flags.AWD); err != nil {
logger.Errorf(logFatalSuffixFn("changes the application working directory: "), xerrors.Errorf("%w", err))
return
}
var awd string
awd, err = os.Getwd()
if err != nil {
logger.Errorf(logFatalSuffixFn("get the application working directory: "), xerrors.Errorf("%w", err))
return
}
logger.Infof("application working directory: %s", awd)
}
defer func(start time.Time) {
logger.Infof("execution time: %v", time.Since(start))
}(time.Now())
logger.Infof("configuration file: %s", flags.FileLocationMessage())
data, err := config.NewConfigReader(flags).Read()
if err != nil {
logger.Errorf(logFatalSuffixFn("read config: "), err)
return
}
rawConfig, templateData, err := config.NewRawPreprocessor(
flags.ConfigPath,
flags.TemplateVars.Vars,
entity.TemplateFnsMap,
[]string{flags.MissingKey.String()},
).Process(data)
if err != nil {
logger.Errorf(logFatalSuffixFn("preprocess raw config: "), err)
return
}
if flags.PrintProcessedConfig {
logger.ForceInfof("preprocessed config:\n%s", string(rawConfig))
}
var (
conf config.Config
)
conf, err = config.NewYamlConfigUnmarshaler().Unmarshal(rawConfig)
if err != nil {
logger.Errorf(logFatalSuffixFn("unmarshal config: "), err)
return
}
if err = conf.Validate(); err != nil {
logger.Errorf(logFatalSuffixFn("validate config: "), err)
return
}
var (
actionFilter = factory.NewActionFilter(
flags.Skip,
flags.Group,
conf.Settings.Groups.GroupByAction(),
conf.Settings.Groups.ManualActions(),
logger,
)
templateOptions = []string{flags.MissingKey.String()}
preprocessors = &exec.Preprocessors{}
)
procChain, err := factory.NewExecutorChainFactory(
logger,
flags.DryRun,
func(executors []entity.Executor) entity.Executor {
return exec.NewPreprocessingChain(preprocessors, executors)
},
factory.NewExecutorBuilderFactory(
conf.DirActions(),
factory.NewMkdirExecutor,
actionFilter,
),
factory.NewExecutorBuilderFactory(
conf.RmActions(),
factory.NewRmExecutor,
actionFilter,
),
factory.NewExecutorBuilderFactory(
conf.CommandActions(),
factory.NewRunCommandExecutor,
actionFilter,
),
factory.NewExecutorBuilderFactory(
conf.FilesActions(),
factory.NewPreprocessorsFileExecutorFactory(
templateData,
templateOptions,
flags.PreprocessFiles,
preprocessors,
func(logger entity.Logger) *resty.Client {
return factory.NewHTTPClient(conf.Settings.HTTP, logger)
},
).Create,
actionFilter,
),
factory.NewExecutorBuilderFactory(
conf.FsActions(),
factory.NewFsModifyExecFactory(
templateData,
templateOptions,
).Create,
actionFilter,
),
).Create()
if err != nil {
logger.Errorf(logFatalSuffixFn("create processors chain: "), err)
return
}
err = procChain.Exec()
if err != nil {
logger.Errorf(logFatalSuffixFn("execute chain: "), err)
return
}
}