-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
62 lines (52 loc) · 1.19 KB
/
base.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
package services
import (
"errors"
"fmt"
"net"
"strconv"
"github.com/AlecAivazis/survey/v2"
)
type Service interface {
GetName() string
GetDefaultPort() int
GetOrganization() string
GetImageName() string
GetDefaults() map[string]string
Prompt() (map[string]string, error)
GetDockerCommandArgs(map[string]string) []string
}
func DefaultOptions() map[string]string {
return map[string]string{
"tag": "latest",
}
}
func DefaultPrompts(port int) []*survey.Question {
return []*survey.Question{
{
Name: "port",
Prompt: &survey.Input{Message: "Which port would you like to use?", Default: strconv.Itoa(port)},
Validate: func(val interface{}) error {
port, err := strconv.ParseUint(val.(string), 10, 16)
if err != nil {
return errors.New("invalid port")
}
if PortIsInUse(uint16(port)) {
return errors.New("port is already in use")
}
return nil
},
},
{
Name: "tag",
Prompt: &survey.Input{Message: "Which tag (version) would you like to use?", Default: "latest"},
},
}
}
func PortIsInUse(port uint16) bool {
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return false
}
conn.Close()
return true
}