diff --git a/config.go b/config.go index b84a476..8b3472c 100644 --- a/config.go +++ b/config.go @@ -9,58 +9,82 @@ import ( "os" ) +// Struct for unmarshaling of opensend TOML configs type Config struct { Receiver ReceiverConfig Sender SenderConfig Targets map[string]map[string]string } +// Config section for receiver type ReceiverConfig struct { DestDir string `toml:"destinationDirectory"` SkipZeroconf bool WorkDir string `toml:"workingDirectory"` } +// Config section for sender type SenderConfig struct { WorkDir string `toml:"workingDirectory"` } +// Attempt to find config path func GetConfigPath() string { // Use ConsoleWriter logger log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) + // Possible config locations configLocations := []string{"~/.config/opensend.toml", "/etc/opensend.toml"} + // For every possible location for _, configLocation := range configLocations { + // Expand path (~ -> home dir and os.ExpandEnv()) expandedPath := ExpandPath(configLocation) + // If file does not exist if _, err := os.Stat(expandedPath); errors.Is(err, os.ErrNotExist) { + // Skip continue } + // Return path with existing file return expandedPath } + // If all else fails, return empty screen return "" } +// Create new config object using values from given path func NewConfig(path string) *Config { // Use ConsoleWriter logger log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) + // Create new empty config struct newConfig := &Config{} + // Set config defaults newConfig.SetDefaults() + // If path is provided if path != "" { + // Read file at path confData, err := ioutil.ReadFile(path) if err != nil { log.Fatal().Err(err).Msg("Error reading config") } + // Unmarshal config data err = toml.Unmarshal(confData, newConfig) if err != nil { log.Fatal().Err(err).Msg("Error unmarshalling toml") } } + // Return new config struct return newConfig } +// Set config defaults func (config *Config) SetDefaults() { + // Set destination directory to $HOME/Downloads config.Receiver.DestDir = ExpandPath("~/Downloads") + // Set receiver working directory to $HOME/.opensend config.Receiver.WorkDir = ExpandPath("~/.opensend") + // Set do not skip zeroconf config.Receiver.SkipZeroconf = false + // 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{} } diff --git a/extra.go b/extra.go index 98d1f79..e6ff72e 100644 --- a/extra.go +++ b/extra.go @@ -11,14 +11,20 @@ import ( func ExpandPath(s string) string { // Use ConsoleWriter logger log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) + // Get user's home directory homeDir, err := os.UserHomeDir() if err != nil { log.Fatal().Err(err).Msg("Error getting home directory") } + // Expand any environment variables in string expandedString := os.ExpandEnv(s) + // If string starts with ~ if strings.HasPrefix(expandedString, "~") { + // Replace ~ with user's home directory expandedString = strings.Replace(expandedString, "~", homeDir, 1) } + // Clean file path expandedString = filepath.Clean(expandedString) + // Return expanded string return expandedString }