-
Notifications
You must be signed in to change notification settings - Fork 7
/
files_overwrite.go
73 lines (63 loc) · 1.6 KB
/
files_overwrite.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
package question
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
"github.com/platformsh/platformify/internal/colors"
"github.com/platformsh/platformify/internal/question/models"
"github.com/platformsh/platformify/internal/questionnaire"
"github.com/platformsh/platformify/vendorization"
)
type FilesOverwrite struct{}
func (q *FilesOverwrite) Ask(ctx context.Context) error {
answers, ok := models.FromContext(ctx)
if !ok {
return nil
}
_, stderr, ok := colors.FromContext(ctx)
if !ok {
return nil
}
assets, _ := vendorization.FromContext(ctx)
existingFiles := make([]string, 0, len(assets.ProprietaryFiles()))
for _, p := range assets.ProprietaryFiles() {
if st, err := os.Stat(filepath.Join(answers.WorkingDirectory, p)); err == nil && !st.IsDir() {
existingFiles = append(existingFiles, p)
}
}
if len(existingFiles) > 0 {
fmt.Fprintln(
stderr,
colors.Colorize(
colors.WarningCode,
fmt.Sprintf("You are reconfiguring the project at %s.", answers.WorkingDirectory),
),
)
fmt.Fprintln(
stderr,
colors.Colorize(
colors.WarningCode,
fmt.Sprintf(
"The following %s files already exist in this directory:",
assets.ServiceName,
),
),
)
for _, p := range existingFiles {
fmt.Fprintln(stderr, colors.Colorize(colors.WarningCode, fmt.Sprintf(" - %s", p)))
}
proceed := false
if err := survey.AskOne(&survey.Confirm{
Message: "Do you want to overwrite them?",
Default: proceed,
}, &proceed); err != nil {
return err
}
if !proceed {
return questionnaire.ErrUserAborted
}
}
return nil
}