From 82028213cf97667350fc3ced74937170a71d0ce0 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Fri, 8 Jan 2021 18:34:08 -0800 Subject: [PATCH] Clean up code and add config --- .metroline.yml | 6 ------ config.go | 14 +++++++++----- go.mod | 1 + go.sum | 2 ++ main.go | 39 +++++++++++++++++++++++++++++---------- opensend.toml | 12 ++++++++++++ 6 files changed, 53 insertions(+), 21 deletions(-) delete mode 100644 .metroline.yml create mode 100644 opensend.toml diff --git a/.metroline.yml b/.metroline.yml deleted file mode 100644 index d7debd6..0000000 --- a/.metroline.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: '1' -jobs: - build: - image: golang:latest - script: - - go build diff --git a/config.go b/config.go index 8b3472c..307c787 100644 --- a/config.go +++ b/config.go @@ -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{} } diff --git a/go.mod b/go.mod index dd34f4b..c6bfaf3 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index ff12b29..5e58ece 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 1141c15..1ceed74 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/opensend.toml b/opensend.toml new file mode 100644 index 0000000..7ea84a7 --- /dev/null +++ b/opensend.toml @@ -0,0 +1,12 @@ +[sender] +workingDirectory = "~/.opensend" + +[receiver] +skipZeroconf = false +workingDirectory = "~/.opensend" +destinationDirectory = "~/Downloads" + +[targets] + + [targets.coral] + ip = "192.168.1.2"