more win32 tweaks

This commit is contained in:
Jarek Kowalski
2016-06-03 20:53:51 -07:00
parent dd11a6e671
commit 72d6bcdd74
5 changed files with 47 additions and 36 deletions

View File

@@ -11,9 +11,10 @@
"net/http/httptest"
"net/url"
"os"
"os/exec"
"time"
"github.com/skratchdot/open-golang/open"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
@@ -107,10 +108,12 @@ func (gcs *gcsStorage) PutBlock(b string, data io.ReadCloser, options PutOptions
Name: gcs.getObjectNameString(b),
}
defer data.Close()
_, err := gcs.objectsService.Insert(gcs.BucketName, &object).
IfGenerationMatch(0).
Media(data).
Do()
call := gcs.objectsService.Insert(gcs.BucketName, &object).Media(data)
if !options.Overwrite {
call = call.IfGenerationMatch(0)
}
_, err := call.Do()
return err
}
@@ -346,15 +349,7 @@ func tokenFromWeb(ctx context.Context, config *oauth2.Config) (*oauth2.Token, er
}
func openURL(url string) error {
try := []string{"xdg-open", "google-chrome", "open"}
for _, bin := range try {
err := exec.Command(bin, url).Run()
if err == nil {
return nil
}
}
log.Printf("Error opening URL in browser.")
return fmt.Errorf("Error opening URL in browser")
return open.Start(url)
}
func authPrompt(url string, state string) (authenticationCode string, err error) {

View File

@@ -8,6 +8,7 @@
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"time"
@@ -95,7 +96,7 @@ func runBackupCommand(context *kingpin.ParseContext) error {
if len(previous) > 0 {
var m backup.Manifest
if err := vlt.Get(previous[0], &m); err != nil {
return fmt.Errorf("error loading previous backup: %vlt", err)
return fmt.Errorf("error loading previous backup: %v", err)
}
oldManifest = &m
}
@@ -110,10 +111,10 @@ func runBackupCommand(context *kingpin.ParseContext) error {
err = vlt.Put(fileID, &manifest)
if err != nil {
return fmt.Errorf("cannot save manifest: %vlt", err)
return fmt.Errorf("cannot save manifest: %v", err)
}
log.Printf("Root: %vlt", manifest.RootObjectID)
log.Printf("Root: %v", manifest.RootObjectID)
}
return nil
@@ -129,7 +130,15 @@ func getBackupUser() string {
log.Fatalf("Cannot determine current user: %s", err)
}
return currentUser.Username
u := currentUser.Username
if runtime.GOOS == "windows" {
if p := strings.Index(u, "\\"); p >= 0 {
// On Windows ignore domain name.
u = u[p+1:]
}
}
return u
}
func getBackupHostName() string {

View File

@@ -15,7 +15,6 @@
var (
backupsCommand = app.Command("backups", "List backup history.")
backupsDirectory = backupsCommand.Arg("directory", "Directory to show history of").ExistingDir()
backupsAll = backupsCommand.Flag("all", "Show history of all backups.").Bool()
maxResultsPerPath = backupsCommand.Flag("maxresults", "Maximum number of results.").Default("100").Int()
)
@@ -44,8 +43,7 @@ func runBackupsCommand(context *kingpin.ParseContext) error {
var prefix string
if !*backupsAll {
if *backupsDirectory != "" {
dir, err := filepath.Abs(*backupsDirectory)
if err != nil {
return fmt.Errorf("invalid directory: '%s': %s", *backupsDirectory, err)
@@ -72,7 +70,7 @@ func runBackupsCommand(context *kingpin.ParseContext) error {
for _, n := range previous {
var m backup.Manifest
if err := vlt.Get(n, &m); err != nil {
return fmt.Errorf("error loading previous backup: %vlt", err)
return fmt.Errorf("error loading previous backup: %v", err)
}
if m.HostName != lastHost || m.UserName != lastUser || m.SourceDirectory != lastDir {

View File

@@ -72,7 +72,6 @@ func openStorageAndEnsureEmpty(url string) (blob.Storage, error) {
}
return s, nil
}
func runCreateCommand(context *kingpin.ParseContext) error {

View File

@@ -5,14 +5,15 @@
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/bgentry/speakeasy"
"github.com/kopia/kopia/blob"
"github.com/kopia/kopia/vault"
"golang.org/x/crypto/ssh/terminal"
)
var (
@@ -39,6 +40,14 @@ func mustOpenVault() *vault.Vault {
}
func getHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
@@ -52,7 +61,13 @@ func persistVaultConfig(v *vault.Vault) error {
return err
}
return ioutil.WriteFile(vaultConfigFileName(), []byte(cfg), 0600)
fname := vaultConfigFileName()
log.Printf("saving configuration %v", fname)
if err := os.MkdirAll(filepath.Dir(fname), 0700); err != nil {
return err
}
return ioutil.WriteFile(fname, []byte(cfg), 0600)
}
func getPersistedVaultConfig() string {
@@ -129,9 +144,7 @@ func getVaultCredentials(isNew bool) (vault.Credentials, error) {
}
if isNew {
for {
fmt.Printf("Enter password to create new vault: ")
p1, err := askPass()
fmt.Println()
p1, err := askPass("Enter password to create new vault: ")
if err == errPasswordTooShort {
fmt.Printf("Password too short, must be at least %v characters, you entered %v. Try again.", vault.MinPasswordLength, len(p1))
fmt.Println()
@@ -140,12 +153,10 @@ func getVaultCredentials(isNew bool) (vault.Credentials, error) {
if err != nil {
return nil, err
}
fmt.Printf("Re-enter password for verification: ")
p2, err := askPass()
p2, err := askPass("Re-enter password for verification: ")
if err != nil {
return nil, err
}
fmt.Println()
if p1 != p2 {
fmt.Println("Passwords don't match!")
} else {
@@ -153,8 +164,7 @@ func getVaultCredentials(isNew bool) (vault.Credentials, error) {
}
}
} else {
fmt.Printf("Enter password to open vault: ")
p1, err := askPass()
p1, err := askPass("Enter password to open vault: ")
if err != nil {
return nil, err
}
@@ -163,8 +173,8 @@ func getVaultCredentials(isNew bool) (vault.Credentials, error) {
}
}
func askPass() (string, error) {
b, err := terminal.ReadPassword(0)
func askPass(prompt string) (string, error) {
b, err := speakeasy.Ask(prompt)
if err != nil {
return "", err
}