Clean up code and add config

This commit is contained in:
Elara 2021-01-08 18:34:08 -08:00
parent 333cc4e195
commit 82028213cf
6 changed files with 53 additions and 21 deletions

View File

@ -1,6 +0,0 @@
version: '1'
jobs:
build:
image: golang:latest
script:
- go build

View File

@ -12,15 +12,15 @@ import (
// Struct for unmarshaling of opensend TOML configs
type Config struct {
Receiver ReceiverConfig
Sender SenderConfig
Targets map[string]map[string]string
Sender SenderConfig
Targets map[string]Target
}
// Config section for receiver
type ReceiverConfig struct {
DestDir string `toml:"destinationDirectory"`
DestDir string `toml:"destinationDirectory"`
SkipZeroconf bool
WorkDir string `toml:"workingDirectory"`
WorkDir string `toml:"workingDirectory"`
}
// Config section for sender
@ -28,6 +28,10 @@ type SenderConfig struct {
WorkDir string `toml:"workingDirectory"`
}
type Target struct {
IP string
}
// Attempt to find config path
func GetConfigPath() string {
// Use ConsoleWriter logger
@ -86,5 +90,5 @@ func (config *Config) SetDefaults() {
// Set sender working directory to $HOME/.opensend
config.Sender.WorkDir = ExpandPath("~/.opensend")
// Set targets to an empty map[string]map[string]string
config.Targets = map[string]map[string]string{}
config.Targets = map[string]Target{}
}

1
go.mod
View File

@ -8,4 +8,5 @@ require (
github.com/pelletier/go-toml v1.8.1
github.com/pkg/browser v0.0.0-20201112035734-206646e67786
github.com/rs/zerolog v1.20.0
github.com/spf13/pflag v1.0.5
)

2
go.sum
View File

@ -19,6 +19,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=

39
main.go
View File

@ -4,7 +4,7 @@ import (
"bufio"
"crypto/rand"
"encoding/hex"
"flag"
flag "github.com/spf13/pflag"
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@ -23,9 +23,6 @@ func main() {
// Use ConsoleWriter logger
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
confPath := GetConfigPath()
config := NewConfig(confPath)
// Create --send-to flag to send to a specific IP
sendTo := flag.String("send-to", "", "Use IP address of receiver instead of mDNS")
// Create --dest-dir flag to save to a specified folder
@ -33,28 +30,50 @@ func main() {
workDir = flag.String("work-dir", "", "Working directory for opensend")
givenCfgPath := flag.String("config", "", "Opensend config to use")
// Create --skip-mdns to skip service registration
skipMdns := flag.Bool("skip-mdns", config.Receiver.SkipZeroconf, "Skip zeroconf service registration (use if mdns fails)")
skipMdns := flag.Bool("skip-mdns", false, "Skip zeroconf service registration (use if mdns fails)")
// Create -t flag for type
actionType := flag.String("t", "", "Type of data being sent")
actionType := flag.StringP("type", "t", "", "Type of data being sent")
// Create -d flag for data
actionData := flag.String("d", "", "Data to send")
actionData := flag.StringP("data", "d", "", "Data to send")
// Create -s flag for sending
sendFlag := flag.Bool("s", false, "Send data")
sendFlag := flag.BoolP("send", "s", false, "Send data")
// Create -r flag for receiving
recvFlag := flag.Bool("r", false, "Receive data")
recvFlag := flag.BoolP("receive", "r", false, "Receive data")
targetFlag := flag.StringP("target", "T", "", "Target as defined in opensend.toml")
// Parse flags
flag.Parse()
if *givenCfgPath != "" {
// Declare config variable
var config *Config
// If config flag not provided
if *givenCfgPath == "" {
// Get config path
confPath := GetConfigPath()
// Read config at path
config = NewConfig(confPath)
} else {
// Otherwise, read config at provided path
config = NewConfig(*givenCfgPath)
}
// If work directory flag not provided
if *workDir == "" {
// If send flag provided
if *sendFlag {
// Set work directory to sender as defined in config
*workDir = ExpandPath(config.Sender.WorkDir)
} else {
// Otherwise set work directory to receiver as defined in config
*workDir = ExpandPath(config.Receiver.WorkDir)
}
}
// If target flag provided
if *targetFlag != "" {
// Set IP to target's IP
*sendTo = config.Targets[*targetFlag].IP
}
// Create channel for signals
sig := make(chan os.Signal, 1)
// Send message on channel upon reception of SIGINT or SIGTERM

12
opensend.toml Normal file
View File

@ -0,0 +1,12 @@
[sender]
workingDirectory = "~/.opensend"
[receiver]
skipZeroconf = false
workingDirectory = "~/.opensend"
destinationDirectory = "~/Downloads"
[targets]
[targets.coral]
ip = "192.168.1.2"