This repository has been archived on 2021-07-08. You can view files and clone it, but cannot push or open issues or pull requests.
opensend/config.go

109 lines
2.9 KiB
Go
Raw Normal View History

/*
Copyright © 2021 Arsen Musayelyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-12-03 10:12:43 +00:00
package main
import (
2020-12-31 07:54:18 +00:00
"errors"
2020-12-03 10:12:43 +00:00
"io/ioutil"
"os"
2021-06-19 07:36:48 +00:00
"github.com/pelletier/go-toml"
"github.com/rs/zerolog/log"
2020-12-03 10:12:43 +00:00
)
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
2021-06-19 07:36:48 +00:00
Sender SenderConfig
Targets map[string]Target
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 {
2021-06-19 07:36:48 +00:00
DestDir string `toml:"destinationDirectory"`
2020-12-31 07:54:18 +00:00
SkipZeroconf bool
2021-06-19 07:36:48 +00:00
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"`
}
2021-01-09 02:34:08 +00:00
type Target struct {
IP string
}
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
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
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
2021-01-09 02:34:08 +00:00
config.Targets = map[string]Target{}
2020-12-21 07:18:42 +00:00
}