Skip to content

Commit

Permalink
move to cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychase committed Jan 24, 2022
1 parent d5cea8e commit 7f1e5ab
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 106 deletions.
7 changes: 3 additions & 4 deletions handlers.go → cmd/handlers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand All @@ -18,14 +18,13 @@ func v1(w http.ResponseWriter, req *http.Request) {
Type: comment,
}

// SwName are SwVers are are concatenated in the 'comment' field and then
// SwName are SwVers are concatenated in the 'comment' field and then
// immediately followed by the Wx.Type. This is performed in the upstream
// aprs library and looks like:
//
// fmt.Sprintf("%s%s%s", aprs.SwName, aprs.SwVers, wx.Type)
//
// On aprs.fi a lowercase 'v%d' is get dropped, so that is why this
// is 'V'.
// On aprs.fi a lowercase 'v%d' gets dropped, so that is why this is 'V'.
aprs.SwName = "wxigate-V"
aprs.SwVers = Version

Expand Down
105 changes: 105 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cmd

import (
"flag"
"fmt"
"net"
"os"
"strings"
)

const DEFAULT_PORT uint = 8080 // BUG(high) move
const DEFAULT_ADDRESS_IPV4 string = "0.0.0.0"

var Version = "-dev"

// Program options
var inputAddress string
var address net.IP
var port uint
var callsign string
var comment string
var ssid string
var longitude float64
var latitude float64
var showVersion bool

func Body() int {
parseArgs()

if showVersion {
fmt.Printf("v%s\n", Version)
return 0
}

err := validate()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}

err = server(address, port)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}

return 0
}

func parseArgs() {
flag.BoolVar(&showVersion, "version", false, "show version")
flag.Float64Var(&longitude, "longitude", 0.0, "longitude (decimal)")
flag.Float64Var(&latitude, "latitude", 0.0, "latitude (decimal)")
flag.StringVar(&callsign, "callsign", "", "callsign")
flag.StringVar(&comment, "comment", "", "comment")
flag.StringVar(&ssid, "ssid", "15", "ssid")
flag.UintVar(&port, "port", DEFAULT_PORT, "tcp port (automatic 0)")
flag.StringVar(&inputAddress, "address", DEFAULT_ADDRESS_IPV4, "IP address")

flag.Parse()
}

func validate() error {
// longitude and latitude validation
if longitude == 0.0 {
return fmt.Errorf("invalid longitude")
}
if latitude == 0.0 {
return fmt.Errorf("invalid latitude")
}

// address validation
address = net.ParseIP(inputAddress)
if address == nil {
return fmt.Errorf("invalid address")
}

// port validation
const max_port = 65535
if port > max_port {
return fmt.Errorf("max port (%d)", max_port)
}

// ssid validation
if len(ssid) > 2 {
return fmt.Errorf("ssid too long")
} else if len(ssid) < 1 {
return fmt.Errorf("ssid empty")
}

// callsign validation
if len(callsign) == 0 {
return fmt.Errorf("missing callsign")
}
if len(callsign) > 8 { // BUG(medium) fix
return fmt.Errorf("callsign too long")
} else if len(callsign) < 3 { // BUG(medium) fix
return fmt.Errorf("callsign too short")
}

callsign = strings.Trim(callsign, "-")
ssid = strings.Trim(ssid, "-")

return nil
}
2 changes: 1 addition & 1 deletion server.go → cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down
103 changes: 2 additions & 101 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,10 @@
package main

import (
"flag"
"fmt"
"net"
"os"
"strings"
"wxigate/cmd"
)

const DEFAULT_PORT uint = 8080 // BUG(high) move
const DEFAULT_ADDRESS_IPV4 string = "0.0.0.0"

var Version = "-dev"

// Program options
var inputAddress string
var address net.IP
var port uint
var callsign string
var comment string
var ssid string
var longitude float64
var latitude float64
var showVersion bool

func main() {
os.Exit(body())
}

func body() int {
parseArgs()

if showVersion {
fmt.Printf("v%s\n", Version)
return 0
}

err := validate()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}

err = server(address, port)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}

return 0
}

func parseArgs() {
flag.BoolVar(&showVersion, "version", false, "show version")
flag.Float64Var(&longitude, "longitude", 0.0, "longitude (decimal)")
flag.Float64Var(&latitude, "latitude", 0.0, "latitude (decimal)")
flag.StringVar(&callsign, "callsign", "", "callsign")
flag.StringVar(&comment, "comment", "", "comment")
flag.StringVar(&ssid, "ssid", "15", "ssid")
flag.UintVar(&port, "port", DEFAULT_PORT, "tcp port (automatic 0)")
flag.StringVar(&inputAddress, "address", DEFAULT_ADDRESS_IPV4, "IP address")

flag.Parse()
}

func validate() error {
// longitude and latitude validation
if longitude == 0.0 {
return fmt.Errorf("invalid longitude")
}
if latitude == 0.0 {
return fmt.Errorf("invalid latitude")
}

// address validation
address = net.ParseIP(inputAddress)
if address == nil {
return fmt.Errorf("invalid address")
}

// port validation
const max_port = 65535
if port > max_port {
return fmt.Errorf("max port (%d)", max_port)
}

// ssid validation
if len(ssid) > 2 {
return fmt.Errorf("ssid too long")
} else if len(ssid) < 1 {
return fmt.Errorf("ssid empty")
}

// callsign validation
if len(callsign) == 0 {
return fmt.Errorf("missing callsign")
}
if len(callsign) > 8 { // BUG(medium) fix
return fmt.Errorf("callsign too long")
} else if len(callsign) < 3 { // BUG(medium) fix
return fmt.Errorf("callsign too short")
}

callsign = strings.Trim(callsign, "-")
ssid = strings.Trim(ssid, "-")

return nil
os.Exit(cmd.Body())
}

0 comments on commit 7f1e5ab

Please sign in to comment.