Added interactive password prompt when no password is given

This commit is contained in:
Andreas Schneider 2018-04-02 15:25:01 +02:00
parent 10a3cc9637
commit 02e4443ab8
6 changed files with 89 additions and 3 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
/.idea /.idea
vendor/
/pkg
/bin

24
src/calanonsync/Gopkg.lock generated Normal file
View File

@ -0,0 +1,24 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "golang.org/x/crypto"
packages = ["ssh/terminal"]
revision = "12892e8c234f4fe6f6803f052061de9057903bb2"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = [
"unix",
"windows"
]
revision = "378d26f46672a356c46195c28f61bdb4c0a781dd"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "724028051592219199897d15df4916e95f132caea6497383bbfabbcdb0672123"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[prune]
go-tests = true
unused-packages = true
[[constraint]]
branch = "master"
name = "golang.org/x/crypto"

View File

@ -18,7 +18,7 @@ func main() {
time.Now().AddDate(0, -1, 0), // One month past time.Now().AddDate(0, -1, 0), // One month past
time.Now().AddDate(0, 2, 0)) // Two months ahead time.Now().AddDate(0, 2, 0)) // Two months ahead
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalf("Could not get EWS items: %s\n", err)
} }
relevantEWSItems := make(map[string]CalendarItem) relevantEWSItems := make(map[string]CalendarItem)
@ -35,7 +35,7 @@ func main() {
c := NewCalDAV(s.CalDAV) c := NewCalDAV(s.CalDAV)
calDavItems, err := c.GetEvents() calDavItems, err := c.GetEvents()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalf("Could not get CalDAV items: %s\n", err)
} }
// Build a map for easier lookup by UID // Build a map for easier lookup by UID
calDavItemMap := make(map[string]CalDAVItem) calDavItemMap := make(map[string]CalDAVItem)

View File

@ -274,6 +274,10 @@ func (c *CalDAV) GetEvents() ([]CalDAVItem, error) {
return nil, err return nil, err
} }
if resp.StatusCode != http.StatusMultiStatus {
return nil, fmt.Errorf("Unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close() defer resp.Body.Close()
ms := &MultiStatus{} ms := &MultiStatus{}
err = xml.NewDecoder(resp.Body).Decode(ms) err = xml.NewDecoder(resp.Body).Decode(ms)

View File

@ -2,8 +2,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"golang.org/x/crypto/ssh/terminal"
"os" "os"
"strings" "strings"
"syscall"
) )
type ServerSettings struct { type ServerSettings struct {
@ -20,6 +22,22 @@ type Settings struct {
} }
} }
func ensurePassword(password *string, name string) {
if *password != "" {
// Nothing to do. Password already set.
return
}
print(name + " password: ")
b, err := terminal.ReadPassword(syscall.Stdin)
println()
if err != nil {
panic(err)
}
*password = string(b)
}
func LoadSettings() Settings { func LoadSettings() Settings {
f, err := os.Open("calanonsync.json") f, err := os.Open("calanonsync.json")
if err != nil { if err != nil {
@ -35,5 +53,8 @@ func LoadSettings() Settings {
settings.CalDAV.URL += "/" settings.CalDAV.URL += "/"
} }
ensurePassword(&settings.EWS.Password, "EWS")
ensurePassword(&settings.CalDAV.Password, "CalDAV")
return settings return settings
} }