/
utils.go
125 lines (105 loc) · 3.22 KB
/
utils.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
package link
import (
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
msg "github.com/aziontech/azion-cli/messages/init"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/vulcan"
vul "github.com/aziontech/azion-cli/pkg/vulcan"
helpers "github.com/aziontech/azion-cli/utils"
"go.uber.org/zap"
)
func shouldConfigure(info *LinkInfo) bool {
if info.Auto {
return true
}
msg := fmt.Sprintf("Do you want to link %s to Azion? (y/N)", info.PathWorkingDir)
return helpers.Confirm(info.GlobalFlagAll, msg, false)
}
func shouldDevDeploy(info *LinkInfo, msg string, defaultYes bool) bool {
return helpers.Confirm(info.GlobalFlagAll, msg, defaultYes)
}
func shouldFetch(cmd *LinkCmd, info *LinkInfo) (bool, error) {
var err error
var shouldFetchTemplates bool
if empty, _ := cmd.IsDirEmpty("./azion"); !empty {
if info.GlobalFlagAll || info.Auto {
shouldFetchTemplates = true
} else {
return helpers.Confirm(info.GlobalFlagAll, "This project was already configured. Do you want to override the previous configuration? (y/N)", false), nil
}
if shouldFetchTemplates {
err = cmd.CleanDir("./azion")
if err != nil {
logger.Debug("Error while trying to clean azion directory", zap.Error(err))
return false, err
}
}
return shouldFetchTemplates, nil
}
return true, nil
}
func askForInput(msg string, defaultIn string) (string, error) {
var userInput string
prompt := &survey.Input{
Message: msg,
Default: defaultIn,
}
// Prompt the user for input
err := survey.AskOne(prompt, &userInput, survey.WithKeepFilter(true))
if err != nil {
return "", err
}
return userInput, nil
}
func (cmd *LinkCmd) selectVulcanMode(info *LinkInfo) error {
if info.Preset == "nextjs" {
return nil
}
// checking is vulcan major is correct
vulcanVer, err := cmd.CommandRunner(cmd.F, "npm show edge-functions version", []string{})
if err != nil {
return err
}
err = vulcan.CheckVulcanMajor(vulcanVer, cmd.F)
if err != nil {
return err
}
logger.FInfo(cmd.Io.Out, msg.InitGettingTemplates)
command := vul.Command("--loglevel=error --no-update-notifier", "presets ls")
output, err := cmd.CommandRunner(cmd.F, command, []string{"CLEAN_OUTPUT_MODE=true"})
if err != nil {
return err
}
// The list that comes from Vulcan comes with a blank line that we should remove.
outputInline := strings.Split(output, "\n")
noLastItem := len(outputInline) - 1
listPresets := make([]string, noLastItem)
copy(listPresets, outputInline[:noLastItem])
prompt := &survey.Select{
Message: "Choose a preset and mode:",
Options: listPresets,
}
var answer, template, mode string
err = survey.AskOne(prompt, &answer)
if err != nil {
return err
}
modeSplit := strings.Split(answer, " ")
template = modeSplit[0]
mode = strings.Replace(strings.Replace(modeSplit[1], "(", "", -1), ")", "", -1)
info.Preset = template
info.Mode = mode
return nil
}
func depsInstall(cmd *LinkCmd, packageManager string) error {
logger.FInfo(cmd.Io.Out, msg.InitInstallDeps)
command := fmt.Sprintf("%s install", packageManager)
err := cmd.CommandRunInteractive(cmd.F, command)
if err != nil {
logger.Debug("Error while running command with simultaneous output", zap.Error(err))
return msg.ErrorDeps
}
return nil
}