Created
September 4, 2016 05:50
-
-
Save bndw/4f62002f0acb82682cc80ff94aae9ccb to your computer and use it in GitHub Desktop.
Converts an old format pick safe to the new format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"os" | |
"github.com/bndw/pick/backends" | |
"github.com/bndw/pick/safe" | |
"github.com/bndw/pick/utils" | |
) | |
var oldSafePath string | |
func init() { | |
flag.StringVar(&oldSafePath, "src", "", "Path to safe file") | |
} | |
func main() { | |
flag.Parse() | |
src, err := loadOldSafe() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
if err = createNewSafe(src); err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
func createNewSafe(src *oldSafe) error { | |
password, err := utils.GetPasswordInput("Enter a password for the new safe") | |
if err != nil { | |
return err | |
} | |
s, err := safe.Load(password, nil) | |
if err != nil { | |
return err | |
} | |
for _, a := range src.Alias { | |
// NOTE: This is going to lose the CreatedOn times from the old safe. | |
// TODO(): safe.Add should return the new account | |
err = s.Add(a.Alias, a.Username, a.Password) | |
if err != nil { | |
fmt.Printf("Error adding %s to new safe\n", a.Alias) | |
return err | |
} | |
} | |
return nil | |
} | |
type oldSafe struct { | |
Alias map[string]struct { | |
Alias string `json:"alias"` | |
CreatedOn int64 `json:"created_on"` | |
Password string `json:"password"` | |
Username string `json:"username"` | |
} `json:"data"` | |
} | |
func loadOldSafe() (*oldSafe, error) { | |
disk := backends.NewDiskBackend(&oldSafePath) | |
cipherText, err := disk.Load() | |
if err != nil { | |
return nil, err | |
} | |
password, err := utils.GetPasswordInput("Enter password for old safe") | |
if err != nil { | |
return nil, err | |
} | |
plaintext, err := utils.Decrypt(cipherText, password) | |
if err != nil { | |
return nil, err | |
} | |
var os oldSafe | |
if err = json.Unmarshal(plaintext, &os); err != nil { | |
return nil, err | |
} | |
return &os, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment