| 1 |
package core |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"runtime" |
| 8 |
"slices" |
| 9 |
|
| 10 |
"github.com/cqroot/prompt" |
| 11 |
"github.com/cqroot/prompt/multichoose" |
| 12 |
|
| 13 |
tea "github.com/charmbracelet/bubbletea" |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
errInvalidKind = errors.New("invalid kind") |
| 18 |
errInvalidComponent = errors.New("invalid component") |
| 19 |
errKindDoesNotAcceptBody = errors.New("kind does not accept a body") |
| 20 |
errKindProvidedWhenNotConfigured = errors.New("kind provided but not supported") |
| 21 |
errComponentProvidedWhenNotConfigured = errors.New("component provided but not supported") |
| 22 |
errCustomProvidedNotConfigured = errors.New("custom value provided but not configured") |
| 23 |
errProjectNotFound = errors.New("project not found") |
| 24 |
errProjectRequired = errors.New("project missing but required") |
| 25 |
|
| 26 |
// prompt disabled |
| 27 |
errProjectMissingPromptDisabled = errors.New("project missing and prompt is disabled") |
| 28 |
errComponentMissingPromptDisabled = errors.New("component missing and prompt is disabled") |
| 29 |
errKindMissingPromptDisabled = errors.New("kind missing and prompt is disabled") |
| 30 |
errBodyMissingPromptDisabled = errors.New("body missing and prompt is disabled") |
| 31 |
errCustomMissingPromptDisabled = errors.New("custom missing and prompt is disabled") |
| 32 |
) |
| 33 |
|
| 34 |
type Prompts struct { |
| 35 |
Config *Config |
| 36 |
StdinReader io.Reader |
| 37 |
KindConfig *KindConfig |
| 38 |
BodyEditor bool |
| 39 |
EditorCmdBuilder func(string) (EditorRunner, error) |
| 40 |
TimeNow TimeNow |
| 41 |
|
| 42 |
// Enabled checks to make sure our terminal supports prompts |
| 43 |
Enabled bool |
| 44 |
|
| 45 |
// Values can be submitted from the environment or shell arguments. |
| 46 |
Projects []string |
| 47 |
Component string |
| 48 |
Kind string |
| 49 |
Body string |
| 50 |
Customs map[string]string |
| 51 |
} |
| 52 |
|
| 53 |
// BuildChanges will ask the user prompts based on the configuration |
| 54 |
// returning all changes as prompts are answered. |
| 55 |
// A change relates to a single project and single change, so if the change |
| 56 |
// affects multiple projects we will return multiple changes. |
| 57 |
func (p *Prompts) BuildChanges() ([]*Change, error) { |
| 58 |
err := p.validateArguments() |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
err = p.projects() |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
|
| 68 |
err = p.component() |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
|
| 73 |
err = p.kind() |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
err = p.body() |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
err = p.userChoices() |
| 84 |
if err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
|
| 88 |
envs := p.Config.EnvVars() |
| 89 |
|
| 90 |
// If we don't have projects enabled, just create a single change. |
| 91 |
if len(p.Projects) == 0 { |
| 92 |
change := &Change{ |
| 93 |
Component: p.Component, |
| 94 |
Kind: p.Kind, |
| 95 |
Body: p.Body, |
| 96 |
Time: p.TimeNow(), |
| 97 |
Custom: p.Customs, |
| 98 |
Env: envs, |
| 99 |
} |
| 100 |
|
| 101 |
err = change.PostProcess(p.Config, p.KindConfig) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
|
| 106 |
return []*Change{change}, nil |
| 107 |
} |
| 108 |
|
| 109 |
changes := make([]*Change, len(p.Projects)) |
| 110 |
for i := range changes { |
| 111 |
// Err is already validated when getting the project above. |
| 112 |
projConfig, _ := p.Config.Project(p.Projects[i]) |
| 113 |
changes[i] = &Change{ |
| 114 |
Project: projConfig.Key, |
| 115 |
Component: p.Component, |
| 116 |
Kind: p.Kind, |
| 117 |
Body: p.Body, |
| 118 |
Time: p.TimeNow(), |
| 119 |
Custom: p.Customs, |
| 120 |
Env: envs, |
| 121 |
} |
| 122 |
|
| 123 |
err = changes[i].PostProcess(p.Config, p.KindConfig) |
| 124 |
if err != nil { |
| 125 |
return nil, err |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return changes, nil |
| 130 |
} |
| 131 |
|
| 132 |
// validateArguments will check the initial state of a change against the config |
| 133 |
// and return an error if anything is invalid |
| 134 |
func (p *Prompts) validateArguments() error { |
| 135 |
if len(p.Config.Components) == 0 && len(p.Component) > 0 { |
| 136 |
return errComponentProvidedWhenNotConfigured |
| 137 |
} |
| 138 |
|
| 139 |
if len(p.Config.Kinds) == 0 && len(p.Kind) > 0 { |
| 140 |
return errKindProvidedWhenNotConfigured |
| 141 |
} |
| 142 |
|
| 143 |
configuredCustoms := make([]Custom, 0) |
| 144 |
|
| 145 |
if len(p.Config.Kinds) > 0 && len(p.Kind) > 0 { |
| 146 |
kc := p.Config.KindFromKeyOrLabel(p.Kind) |
| 147 |
if kc == nil { |
| 148 |
return fmt.Errorf("%w: %s", errInvalidKind, p.Kind) |
| 149 |
} |
| 150 |
|
| 151 |
configuredCustoms = append(configuredCustoms, kc.AdditionalChoices...) |
| 152 |
if !kc.SkipGlobalChoices { |
| 153 |
configuredCustoms = append(configuredCustoms, p.Config.CustomChoices...) |
| 154 |
} |
| 155 |
} else { |
| 156 |
configuredCustoms = append(configuredCustoms, p.Config.CustomChoices...) |
| 157 |
} |
| 158 |
|
| 159 |
// make sure no custom values are assigned that do not exist |
| 160 |
foundCustoms := map[string]struct{}{} |
| 161 |
|
| 162 |
for key, value := range p.Customs { |
| 163 |
for _, choice := range configuredCustoms { |
| 164 |
if choice.Key == key { |
| 165 |
foundCustoms[key] = struct{}{} |
| 166 |
|
| 167 |
err := choice.Validate(value) |
| 168 |
if err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
|
| 172 |
break |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
for key := range p.Customs { |
| 178 |
_, ok := foundCustoms[key] |
| 179 |
if !ok { |
| 180 |
return fmt.Errorf("%w: %s", errCustomProvidedNotConfigured, key) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return nil |
| 185 |
} |
| 186 |
|
| 187 |
func (p *Prompts) projects() error { |
| 188 |
if len(p.Config.Projects) == 0 { |
| 189 |
return nil |
| 190 |
} |
| 191 |
|
| 192 |
if len(p.Projects) == 0 { |
| 193 |
if !p.Enabled { |
| 194 |
return errProjectMissingPromptDisabled |
| 195 |
} |
| 196 |
|
| 197 |
var err error |
| 198 |
|
| 199 |
projs, err := prompt.New().Ask("Projects"). |
| 200 |
MultiChoose( |
| 201 |
p.Config.ProjectLabels(), |
| 202 |
multichoose.WithHelp(true), |
| 203 |
multichoose.WithTeaProgramOpts(tea.WithInput(p.StdinReader)), |
| 204 |
) |
| 205 |
if err != nil { |
| 206 |
return err |
| 207 |
} |
| 208 |
|
| 209 |
if len(projs) == 0 { |
| 210 |
return errProjectRequired |
| 211 |
} |
| 212 |
|
| 213 |
p.Projects = projs |
| 214 |
} |
| 215 |
|
| 216 |
// Quickly validate the project exists and lines up before moving on. |
| 217 |
for _, proj := range p.Projects { |
| 218 |
_, err := p.Config.Project(proj) |
| 219 |
if err != nil { |
| 220 |
return fmt.Errorf("%w: %s", err, proj) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return nil |
| 225 |
} |
| 226 |
|
| 227 |
func (p *Prompts) component() error { |
| 228 |
if len(p.Config.Components) == 0 { |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
if len(p.Component) == 0 { |
| 233 |
if !p.Enabled { |
| 234 |
return errComponentMissingPromptDisabled |
| 235 |
} |
| 236 |
|
| 237 |
var err error |
| 238 |
|
| 239 |
comp, err := Custom{ |
| 240 |
Type: CustomEnum, |
| 241 |
Label: "Component", |
| 242 |
EnumOptions: p.Config.Components, |
| 243 |
}.AskPrompt(p.StdinReader) |
| 244 |
if err != nil { |
| 245 |
return err |
| 246 |
} |
| 247 |
|
| 248 |
p.Component = comp |
| 249 |
} |
| 250 |
|
| 251 |
if !slices.Contains(p.Config.Components, p.Component) { |
| 252 |
return fmt.Errorf("%w: %s", errInvalidComponent, p.Component) |
| 253 |
} |
| 254 |
|
| 255 |
return nil |
| 256 |
} |
| 257 |
|
| 258 |
func (p *Prompts) kind() error { |
| 259 |
if len(p.Config.Kinds) == 0 { |
| 260 |
return nil |
| 261 |
} |
| 262 |
|
| 263 |
if len(p.Kind) == 0 { |
| 264 |
if !p.Enabled { |
| 265 |
return errKindMissingPromptDisabled |
| 266 |
} |
| 267 |
|
| 268 |
kindLabels := make([]string, len(p.Config.Kinds)) |
| 269 |
for i, kc := range p.Config.Kinds { |
| 270 |
kindLabels[i] = kc.Label |
| 271 |
} |
| 272 |
|
| 273 |
kind, err := Custom{ |
| 274 |
Type: CustomEnum, |
| 275 |
Label: "Kind", |
| 276 |
EnumOptions: kindLabels, |
| 277 |
}.AskPrompt(p.StdinReader) |
| 278 |
if err != nil { |
| 279 |
return err |
| 280 |
} |
| 281 |
|
| 282 |
p.Kind = kind |
| 283 |
} |
| 284 |
|
| 285 |
for i := range p.Config.Kinds { |
| 286 |
kindConfig := &p.Config.Kinds[i] |
| 287 |
|
| 288 |
if kindConfig.Label == p.Kind || kindConfig.Key == p.Kind { |
| 289 |
p.KindConfig = kindConfig |
| 290 |
p.Kind = kindConfig.KeyOrLabel() |
| 291 |
|
| 292 |
return nil |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return fmt.Errorf("%w: %s", errInvalidKind, p.Kind) |
| 297 |
} |
| 298 |
|
| 299 |
func (p *Prompts) body() error { |
| 300 |
if p.expectsNoBody() && len(p.Body) > 0 { |
| 301 |
return fmt.Errorf("%w: %s", errKindDoesNotAcceptBody, p.Kind) |
| 302 |
} |
| 303 |
|
| 304 |
if p.expectsBody() && len(p.Body) == 0 { |
| 305 |
if !p.Enabled { |
| 306 |
return errBodyMissingPromptDisabled |
| 307 |
} |
| 308 |
|
| 309 |
if p.BodyEditor { |
| 310 |
file, err := createTempFile(runtime.GOOS, p.Config.VersionExt) |
| 311 |
if err != nil { |
| 312 |
return err |
| 313 |
} |
| 314 |
|
| 315 |
runner, err := p.EditorCmdBuilder(file) |
| 316 |
if err != nil { |
| 317 |
return err |
| 318 |
} |
| 319 |
|
| 320 |
p.Body, err = getBodyTextWithEditor(runner, file) |
| 321 |
|
| 322 |
return err |
| 323 |
} else { |
| 324 |
bodyCustom := p.Config.Body.CreateCustom() |
| 325 |
|
| 326 |
body, err := bodyCustom.AskPrompt(p.StdinReader) |
| 327 |
if err != nil { |
| 328 |
return err |
| 329 |
} |
| 330 |
|
| 331 |
p.Body = body |
| 332 |
|
| 333 |
return err |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if p.expectsBody() && len(p.Body) > 0 { |
| 338 |
return p.Config.Body.Validate(p.Body) |
| 339 |
} |
| 340 |
|
| 341 |
return nil |
| 342 |
} |
| 343 |
|
| 344 |
func (p *Prompts) expectsNoBody() bool { |
| 345 |
return p.KindConfig != nil && p.KindConfig.SkipBody |
| 346 |
} |
| 347 |
|
| 348 |
func (p *Prompts) expectsBody() bool { |
| 349 |
return p.KindConfig == nil || !p.KindConfig.SkipBody |
| 350 |
} |
| 351 |
|
| 352 |
func (p *Prompts) userChoices() error { |
| 353 |
userChoices := make([]Custom, 0) |
| 354 |
|
| 355 |
if p.KindConfig == nil || !p.KindConfig.SkipGlobalChoices { |
| 356 |
userChoices = append(userChoices, p.Config.CustomChoices...) |
| 357 |
} |
| 358 |
|
| 359 |
if p.KindConfig != nil { |
| 360 |
userChoices = append(userChoices, p.KindConfig.AdditionalChoices...) |
| 361 |
} |
| 362 |
|
| 363 |
// custom map may be nil, which is fine if we have no choices |
| 364 |
// otherwise we need to initialize it |
| 365 |
if len(userChoices) > 0 && p.Customs == nil { |
| 366 |
p.Customs = make(map[string]string) |
| 367 |
} |
| 368 |
|
| 369 |
for _, custom := range userChoices { |
| 370 |
// skip already provided values |
| 371 |
if p.Customs[custom.Key] != "" { |
| 372 |
continue |
| 373 |
} |
| 374 |
|
| 375 |
if !p.Enabled { |
| 376 |
if custom.Optional { |
| 377 |
continue |
| 378 |
} |
| 379 |
|
| 380 |
return fmt.Errorf("%w: custom key '%s'", errCustomMissingPromptDisabled, custom.Key) |
| 381 |
} |
| 382 |
|
| 383 |
var err error |
| 384 |
|
| 385 |
p.Customs[custom.Key], err = custom.AskPrompt(p.StdinReader) |
| 386 |
if err != nil { |
| 387 |
return err |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
return nil |
| 392 |
} |