-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
util.go
49 lines (46 loc) · 1.11 KB
/
util.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
package config
import (
"errors"
"fmt"
"github.com/claudiodangelis/qrcp/application"
"github.com/claudiodangelis/qrcp/util"
"github.com/manifoldco/promptui"
)
func chooseInterface(flags application.Flags) (string, error) {
interfaces, err := util.Interfaces(flags.ListAllInterfaces)
if err != nil {
return "", err
}
if len(interfaces) == 0 {
return "", errors.New("no interfaces found")
}
if len(interfaces) == 1 && !interactive {
for name := range interfaces {
fmt.Printf("only one interface found: %s, using this one\n", name)
return name, nil
}
}
// Map for pretty printing
m := make(map[string]string)
items := []string{}
for name, ip := range interfaces {
label := fmt.Sprintf("%s (%s)", name, ip)
m[label] = name
items = append(items, label)
}
// Add the "any" interface
anyIP := "0.0.0.0"
anyName := "any"
anyLabel := fmt.Sprintf("%s (%s)", anyName, anyIP)
m[anyLabel] = anyName
items = append(items, anyLabel)
prompt := promptui.Select{
Items: items,
Label: "Choose interface",
}
_, result, err := prompt.Run()
if err != nil {
return "", err
}
return m[result], nil
}