2018-03-31 18:52:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-04-02 15:25:01 +02:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2018-03-31 18:52:32 +02:00
|
|
|
"os"
|
2018-04-01 20:02:53 +02:00
|
|
|
"strings"
|
2018-04-02 15:25:01 +02:00
|
|
|
"syscall"
|
2018-03-31 18:52:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerSettings struct {
|
|
|
|
URL string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Settings struct {
|
|
|
|
EWS ServerSettings
|
|
|
|
CalDAV ServerSettings
|
|
|
|
Anonymize struct {
|
|
|
|
Title string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:25:01 +02:00
|
|
|
func ensurePassword(password *string, name string) {
|
|
|
|
if *password != "" {
|
|
|
|
// Nothing to do. Password already set.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
print(name + " password: ")
|
2018-04-03 12:17:17 +02:00
|
|
|
b, err := terminal.ReadPassword(int(syscall.Stdin))
|
2018-04-02 15:25:01 +02:00
|
|
|
println()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*password = string(b)
|
|
|
|
}
|
|
|
|
|
2018-03-31 18:52:32 +02:00
|
|
|
func LoadSettings() Settings {
|
|
|
|
f, err := os.Open("calanonsync.json")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
settings := Settings{}
|
|
|
|
err = json.NewDecoder(f).Decode(&settings)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-01 20:02:53 +02:00
|
|
|
|
2018-04-01 20:24:09 +02:00
|
|
|
if settings.CalDAV.URL != "" && !strings.HasSuffix(settings.CalDAV.URL, "/") {
|
2018-04-01 20:02:53 +02:00
|
|
|
settings.CalDAV.URL += "/"
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:25:01 +02:00
|
|
|
ensurePassword(&settings.EWS.Password, "EWS")
|
|
|
|
ensurePassword(&settings.CalDAV.Password, "CalDAV")
|
|
|
|
|
2018-03-31 18:52:32 +02:00
|
|
|
return settings
|
|
|
|
}
|