Repository: claudiodangelis/qrcp
File: util.go (Go)

Code before:
package main

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"math/rand"
	"net"
	"os"
	"strconv"
	"strings"
	"time"
)

var (
	words = []string{"Gerbil",
		"Hamster",
		"Ferret",
		"Parrot",
		"Cat",
		"Dog",
		"Bunny",
		"Mouse",
		"Rat",
		"Hedgehog",
		"Chinchilla"}
)

// 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
}

// getRandomSuffix returns a string of three concatenated
// random words from `words`.
func getRandomSuffix() string {
	rand.Seed(time.Now().UTC().UnixNano())
	suffix := ""

	for i := 0; i < 3; i++ {
		suffix += words[rand.Intn(len(words)-1)]
	}
	return suffix
}


Review comment (suggestion):
you can change to `var suffix string` and use the zero-values, it have the same value as empty string.

Code after:
	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
}

// getRandomURLPath returns a random string of 4 alphanumeric characters
func getRandomURLPath() string {
	timeNum := time.Now().UTC().UnixNano()
	alphaString := strconv.FormatInt(timeNum, 36)
	return alphaString[len(alphaString)-4:]
}