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