Browse Source

Improved flags validation

main
Unbewohnte 3 years ago
parent
commit
4f5165fa3a
  1. 36
      main.go
  2. 4
      sender/sender.go

36
main.go

@ -13,45 +13,43 @@ import (
// flags
var PORT *int = flag.Int("port", 8080, "Specifies a port for a sender|port to connect to")
var SENDERADDR *string = flag.String("addr", "", "Specifies an address to connect to")
var DOWNLOADSFOLDER *string = flag.String("downloadto", "", "Specifies where the receiver will store downloaded file")
var DOWNLOADSFOLDER *string = flag.String("downloadto", ".", "Specifies where the receiver will store downloaded file")
var SHAREDFILE *string = flag.String("sharefile", "", "Specifies what file sender will send")
var SENDING bool
// Input-validation
func processFlags() {
func validateFlags() {
// port validation
if *PORT < 0 {
fmt.Println("Invalid port !")
os.Exit(-1)
}
// going to send file -> sending
// sending or receiving
if strings.TrimSpace(*SHAREDFILE) != "" {
SENDING = true
}
// specifying address to connect to -> receiving
if strings.TrimSpace(*SENDERADDR) != "" {
if SENDING {
fmt.Println("Cannot specify an address when sharing !")
os.Exit(-1)
}
SENDING = false
}
// specifying path to download to -> receiving
if strings.TrimSpace(*DOWNLOADSFOLDER) != "" {
if SENDING {
fmt.Println("Cannot specify a downloads directory when sharing !")
os.Exit(-1)
}
} else if strings.TrimSpace(*SENDERADDR) != "" {
SENDING = false
}
// check for default values in vital flags in case they were not provided
if strings.TrimSpace(*SENDERADDR) == "" && strings.TrimSpace(*SHAREDFILE) == "" {
fmt.Println("--help to see available flags")
os.Exit(-1)
} else if !SENDING && strings.TrimSpace(*SENDERADDR) == "" {
fmt.Println("No specified sender`s address")
os.Exit(-1)
} else if SENDING && strings.TrimSpace(*SHAREDFILE) == "" {
fmt.Println("No specified file")
os.Exit(-1)
}
}
// parse flags, validate given values
func init() {
flag.Parse()
processFlags()
validateFlags()
}
func main() {

4
sender/sender.go

@ -52,7 +52,7 @@ func NewSender(port int, filepath string) *Sender {
key := encryption.Generate32AESkey()
fmt.Printf("Generated an encryption key: %s\n", key)
fmt.Printf("Created a new sender at %s:%d (remote)\n%s:%d (local)\n", remoteIP, port, localIP, port)
fmt.Printf("Created a new sender at %s:%d (remote)\n%s:%d (local)\n\n", remoteIP, port, localIP, port)
return &Sender{
Port: port,
FileToTransfer: fileToTransfer,
@ -265,7 +265,7 @@ func (s *Sender) ReceivePackets() {
// Current structure allows the sender to receive any type of packet
// in any order and react correspondingly
func (s *Sender) MainLoop() {
// receive and print in separate goroutines
go s.ReceivePackets()
go s.PrintTransferInfo(time.Second * 3)

Loading…
Cancel
Save