2020-12-03 10:12:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-31 07:54:18 +00:00
|
|
|
"errors"
|
|
|
|
"github.com/pelletier/go-toml"
|
2020-12-03 10:12:43 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-01-03 02:00:39 +00:00
|
|
|
// Struct for unmarshaling of opensend TOML configs
|
2020-12-03 10:12:43 +00:00
|
|
|
type Config struct {
|
2020-12-31 07:54:18 +00:00
|
|
|
Receiver ReceiverConfig
|
|
|
|
Sender SenderConfig
|
|
|
|
Targets map[string]map[string]string
|
2020-12-03 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 02:00:39 +00:00
|
|
|
// Config section for receiver
|
2020-12-31 07:54:18 +00:00
|
|
|
type ReceiverConfig struct {
|
|
|
|
DestDir string `toml:"destinationDirectory"`
|
|
|
|
SkipZeroconf bool
|
|
|
|
WorkDir string `toml:"workingDirectory"`
|
2020-12-03 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 02:00:39 +00:00
|
|
|
// Config section for sender
|
2020-12-31 07:54:18 +00:00
|
|
|
type SenderConfig struct {
|
|
|
|
WorkDir string `toml:"workingDirectory"`
|
2020-12-08 01:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 02:00:39 +00:00
|
|
|
// Attempt to find config path
|
2020-12-31 07:54:18 +00:00
|
|
|
func GetConfigPath() string {
|
2020-12-03 10:12:43 +00:00
|
|
|
// Use ConsoleWriter logger
|
2020-12-03 17:32:03 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
|
2021-01-03 02:00:39 +00:00
|
|
|
// Possible config locations
|
2020-12-31 07:54:18 +00:00
|
|
|
configLocations := []string{"~/.config/opensend.toml", "/etc/opensend.toml"}
|
2021-01-03 02:00:39 +00:00
|
|
|
// For every possible location
|
2020-12-31 07:54:18 +00:00
|
|
|
for _, configLocation := range configLocations {
|
2021-01-03 02:00:39 +00:00
|
|
|
// Expand path (~ -> home dir and os.ExpandEnv())
|
2020-12-31 07:54:18 +00:00
|
|
|
expandedPath := ExpandPath(configLocation)
|
2021-01-03 02:00:39 +00:00
|
|
|
// If file does not exist
|
2020-12-31 07:54:18 +00:00
|
|
|
if _, err := os.Stat(expandedPath); errors.Is(err, os.ErrNotExist) {
|
2021-01-03 02:00:39 +00:00
|
|
|
// Skip
|
2020-12-31 07:54:18 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-01-03 02:00:39 +00:00
|
|
|
// Return path with existing file
|
2020-12-31 07:54:18 +00:00
|
|
|
return expandedPath
|
2020-12-21 07:18:42 +00:00
|
|
|
}
|
2021-01-03 02:00:39 +00:00
|
|
|
// If all else fails, return empty screen
|
2020-12-31 07:54:18 +00:00
|
|
|
return ""
|
2020-12-03 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 02:00:39 +00:00
|
|
|
// Create new config object using values from given path
|
2020-12-31 07:54:18 +00:00
|
|
|
func NewConfig(path string) *Config {
|
2020-12-03 10:12:43 +00:00
|
|
|
// Use ConsoleWriter logger
|
2020-12-03 17:32:03 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
|
2021-01-03 02:00:39 +00:00
|
|
|
// Create new empty config struct
|
2020-12-31 07:54:18 +00:00
|
|
|
newConfig := &Config{}
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set config defaults
|
2020-12-31 07:54:18 +00:00
|
|
|
newConfig.SetDefaults()
|
2021-01-03 02:00:39 +00:00
|
|
|
// If path is provided
|
2020-12-31 07:54:18 +00:00
|
|
|
if path != "" {
|
2021-01-03 02:00:39 +00:00
|
|
|
// Read file at path
|
2020-12-31 07:54:18 +00:00
|
|
|
confData, err := ioutil.ReadFile(path)
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
2020-12-31 07:54:18 +00:00
|
|
|
log.Fatal().Err(err).Msg("Error reading config")
|
2020-12-21 07:18:42 +00:00
|
|
|
}
|
2021-01-03 02:00:39 +00:00
|
|
|
// Unmarshal config data
|
2020-12-31 07:54:18 +00:00
|
|
|
err = toml.Unmarshal(confData, newConfig)
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
2020-12-31 07:54:18 +00:00
|
|
|
log.Fatal().Err(err).Msg("Error unmarshalling toml")
|
2020-12-21 07:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 02:00:39 +00:00
|
|
|
// Return new config struct
|
2020-12-31 07:54:18 +00:00
|
|
|
return newConfig
|
2020-12-03 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set config defaults
|
2020-12-31 07:54:18 +00:00
|
|
|
func (config *Config) SetDefaults() {
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set destination directory to $HOME/Downloads
|
2020-12-31 07:54:18 +00:00
|
|
|
config.Receiver.DestDir = ExpandPath("~/Downloads")
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set receiver working directory to $HOME/.opensend
|
2020-12-31 07:54:18 +00:00
|
|
|
config.Receiver.WorkDir = ExpandPath("~/.opensend")
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set do not skip zeroconf
|
2020-12-31 07:54:18 +00:00
|
|
|
config.Receiver.SkipZeroconf = false
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set sender working directory to $HOME/.opensend
|
2020-12-31 07:54:18 +00:00
|
|
|
config.Sender.WorkDir = ExpandPath("~/.opensend")
|
2021-01-03 02:00:39 +00:00
|
|
|
// Set targets to an empty map[string]map[string]string
|
2020-12-31 07:54:18 +00:00
|
|
|
config.Targets = map[string]map[string]string{}
|
2020-12-21 07:18:42 +00:00
|
|
|
}
|