forked from claudiodangelis/qrcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
118 lines (113 loc) · 2.64 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
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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
)
// debug prints its argument if the -debug flag is passed
func debug(args ...string) {
if *debugFlag == true {
log.Println(args)
}
}
// findIP returns the IPv4 address of the passed interface, and an error
func findIP(iface net.Interface) (string, error) {
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
return ipnet.IP.String(), nil
}
}
return "", errors.New("Unable to find an IP for this interface")
}
// getAddress returns the address of the network interface to
// bind the server to. The first time is run it prompts a
// dialog to choose which network interface should be used
// for the transfer
func getAddress(config *Config) (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
var candidateInterface *net.Interface
for _, iface := range ifaces {
if iface.Name == config.Iface {
candidateInterface = &iface
break
}
}
if candidateInterface != nil {
ip, err := findIP(*candidateInterface)
if err != nil {
return "", err
}
return ip, nil
}
fmt.Println("Choose the network interface to use (type the number):")
var filteredIfaces []net.Interface
for _, iface := range ifaces {
// TODO: Replace the following with a regexp
if strings.HasPrefix(iface.Name, "veth") {
continue
}
if strings.HasPrefix(iface.Name, "br-") {
continue
}
if strings.HasPrefix(iface.Name, "docker") {
continue
}
if iface.Name == "lo" {
continue
}
filteredIfaces = append(filteredIfaces, iface)
}
for n, iface := range filteredIfaces {
fmt.Printf("[%d] %s\n", n, iface.Name)
}
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
index, err := strconv.Atoi(strings.Trim(text, "\n\r"))
if err != nil {
return "", err
}
if index+1 > len(filteredIfaces) {
return "", errors.New("Wrong number")
}
candidateInterface = &filteredIfaces[index]
ip, err := findIP(*candidateInterface)
if err != nil {
return "", err
}
config.Iface = candidateInterface.Name
return ip, nil
}
// shouldBeZipped returns a boolean value indicating if the
// content should be zipped or not, and an error.
// The content should be zipped if:
// 1. the user passed the `-zip` flag
// 2. there are more than one file
// 3. the file is a directory
func shouldBeZipped(args []string) (bool, error) {
if *zipFlag == true {
return true, nil
}
if len(args) > 1 {
return true, nil
}
file, err := os.Stat(args[0])
if err != nil {
return false, err
}
if file.IsDir() {
return true, nil
}
return false, nil
}