/
template.go
218 lines (201 loc) · 6.48 KB
/
template.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
214
215
216
217
218
package replication
import (
"encoding/json"
"fmt"
"os"
"github.com/c-bata/go-prompt"
"github.com/jfrog/jfrog-cli-core/artifactory/commands/utils"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)
const (
// Strings for prompt questions
SelectConfigKeyMsg = "Select the next configuration key" + utils.PressTabMsg
// Template types
TemplateType = "templateType"
JobType = "jobType"
Create = "create"
Pull = "pull"
Push = "push"
// Common replication configuration JSON keys
ServerId = "serverId"
CronExp = "cronExp"
RepoKey = "repoKey"
TargetRepoKey = "targetRepoKey"
EnableEventReplication = "enableEventReplication"
Enabled = "enabled"
SyncDeletes = "syncDeletes"
SyncProperties = "syncProperties"
SyncStatistics = "syncStatistics"
PathPrefix = "pathPrefix"
SocketTimeoutMillis = "socketTimeoutMillis"
)
type ReplicationTemplateCommand struct {
path string
}
func NewReplicationTemplateCommand() *ReplicationTemplateCommand {
return &ReplicationTemplateCommand{}
}
func (rtc *ReplicationTemplateCommand) SetTemplatePath(path string) *ReplicationTemplateCommand {
rtc.path = path
return rtc
}
func (rtc *ReplicationTemplateCommand) CommandName() string {
return "rt_replication_template"
}
func (rtc *ReplicationTemplateCommand) ServerDetails() (*config.ServerDetails, error) {
// Since it's a local command, usage won't be reported.
return nil, nil
}
func getArtifactoryServerIds() []prompt.Suggest {
suggest := make([]prompt.Suggest, 0)
if configurations, _ := config.GetAllServersConfigs(); configurations != nil {
for _, conf := range configurations {
suggest = append(suggest, prompt.Suggest{Text: conf.ServerId})
}
}
return suggest
}
func (rtc *ReplicationTemplateCommand) Run() (err error) {
replicationTemplateQuestionnaire := &utils.InteractiveQuestionnaire{
MandatoryQuestionsKeys: []string{JobType, RepoKey},
QuestionsMap: questionMap,
}
err = replicationTemplateQuestionnaire.Perform()
if err != nil {
return err
}
resBytes, err := json.Marshal(replicationTemplateQuestionnaire.AnswersMap)
if err != nil {
return errorutils.CheckError(err)
}
if err = os.WriteFile(rtc.path, resBytes, 0644); err != nil {
return errorutils.CheckError(err)
}
log.Info(fmt.Sprintf("Replication creation config template successfully created at %s.", rtc.path))
return nil
}
var questionMap = map[string]utils.QuestionInfo{
utils.OptionalKey: {
Msg: "",
PromptPrefix: "Select the next property >",
AllowVars: false,
Writer: nil,
MapKey: "",
Callback: utils.OptionalKeyCallback,
},
TemplateType: {
Options: []prompt.Suggest{
{Text: Create, Description: "Template for creating a new replication"},
},
Msg: "",
PromptPrefix: "Select the template type >",
AllowVars: false,
Writer: nil,
MapKey: "",
Callback: nil,
},
JobType: {
Options: []prompt.Suggest{
{Text: Pull, Description: "Pull replication"},
{Text: Push, Description: "Push replication"},
},
Msg: "",
PromptPrefix: "Select replication job type" + utils.PressTabMsg,
AllowVars: false,
Writer: nil,
MapKey: "",
Callback: jobTypeCallback,
},
RepoKey: {
Msg: "",
PromptPrefix: "Enter source repo key >",
AllowVars: true,
Writer: utils.WriteStringAnswer,
MapKey: RepoKey,
Callback: nil,
},
TargetRepoKey: {
Msg: "",
PromptPrefix: "Enter target repo key >",
AllowVars: true,
Writer: utils.WriteStringAnswer,
MapKey: TargetRepoKey,
Callback: nil,
},
ServerId: {
Options: getArtifactoryServerIds(),
Msg: "",
PromptPrefix: "Enter target server id" + utils.PressTabMsg,
AllowVars: true,
Writer: utils.WriteStringAnswer,
MapKey: ServerId,
Callback: nil,
},
CronExp: {
Msg: "",
PromptPrefix: "Enter cron expression for frequency (for example, 0 0 12 * * ? will replicate daily) >",
AllowVars: true,
Writer: utils.WriteStringAnswer,
MapKey: CronExp,
Callback: nil,
},
EnableEventReplication: BoolToStringQuestionInfo,
Enabled: BoolToStringQuestionInfo,
SyncDeletes: BoolToStringQuestionInfo,
SyncProperties: BoolToStringQuestionInfo,
SyncStatistics: BoolToStringQuestionInfo,
PathPrefix: {
Msg: "",
PromptPrefix: "Enter path prefix >",
AllowVars: true,
Writer: utils.WriteStringAnswer,
MapKey: PathPrefix,
Callback: nil,
},
SocketTimeoutMillis: {
Msg: "",
PromptPrefix: "Enter socket timeout millis >",
AllowVars: true,
Writer: utils.WriteStringAnswer,
MapKey: SocketTimeoutMillis,
Callback: nil,
},
}
func jobTypeCallback(iq *utils.InteractiveQuestionnaire, jobType string) (string, error) {
if jobType == Pull {
iq.MandatoryQuestionsKeys = append(iq.MandatoryQuestionsKeys, CronExp)
} else {
iq.MandatoryQuestionsKeys = append(iq.MandatoryQuestionsKeys, TargetRepoKey, ServerId, CronExp)
}
iq.OptionalKeysSuggests = getAllPossibleOptionalRepoConfKeys()
return "", nil
}
func getAllPossibleOptionalRepoConfKeys(values ...string) []prompt.Suggest {
optionalKeys := []string{utils.SaveAndExit, Enabled, SyncDeletes, SyncProperties, SyncStatistics, PathPrefix, EnableEventReplication, SocketTimeoutMillis}
if len(values) > 0 {
optionalKeys = append(optionalKeys, values...)
}
return utils.GetSuggestsFromKeys(optionalKeys, suggestionMap)
}
// Specific writers for repo templates, since all the values in the templates should be written as string.
var BoolToStringQuestionInfo = utils.QuestionInfo{
Options: utils.GetBoolSuggests(),
AllowVars: true,
Writer: utils.WriteStringAnswer,
}
var suggestionMap = map[string]prompt.Suggest{
utils.SaveAndExit: {Text: utils.SaveAndExit},
ServerId: {Text: ServerId},
RepoKey: {Text: RepoKey},
TargetRepoKey: {Text: TargetRepoKey},
CronExp: {Text: CronExp},
EnableEventReplication: {Text: EnableEventReplication},
Enabled: {Text: Enabled},
SyncDeletes: {Text: SyncDeletes},
SyncProperties: {Text: SyncProperties},
SyncStatistics: {Text: SyncStatistics},
PathPrefix: {Text: PathPrefix},
SocketTimeoutMillis: {Text: SocketTimeoutMillis},
}