Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable entombing and detombing from the command line #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/cli-input.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var cliCommands = []cliCommand{
{"download", downloadCommand{}, "Download a numbered detachment to disk", contextInbox},
{"drafts", showDraftsSummaryCommand{}, "Show drafts", 0},
{"edit", editCommand{}, "Edit the draft message", contextDraft},
{"entomb", entombCommand{}, "Entomb a statefile", 0},
{"help", helpCommand{}, "List known commands", 0},
{"identity", showIdentityCommand{}, "Show identity", 0},
{"inbox", showInboxSummaryCommand{}, "Show the Inbox", 0},
Expand Down Expand Up @@ -72,6 +73,7 @@ type closeCommand struct{}
type composeCommand struct{}
type deleteCommand struct{}
type editCommand struct{}
type entombCommand struct{}
type logCommand struct{}
type quitCommand struct{}
type replyCommand struct{}
Expand Down
98 changes: 98 additions & 0 deletions client/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"strings"
"strconv"
"sync"
"syscall"
Expand Down Expand Up @@ -413,6 +415,55 @@ func (c *cliClient) createAccountUI(stateFile *disk.StateFile, pw string) (bool,
defaultServer = msgDefaultDevServer
}

PromptLoop:
for {
c.term.SetPrompt("Import an entombed statefile? (y/n)> ")
line, err := c.term.ReadLine()
if err != nil {
return false, err
}
switch line {
case "n", "no", "No":
break PromptLoop
case "y", "yes", "Yes":
var f *os.File
var tombPath string
var tombKey string

c.term.SetPrompt("Path to entombed statefile> ")
tombPath, err = c.term.ReadLine()
if err != nil {
panic(err)
}
if tombPath[:2] == "~/" {
usr, _ := user.Current()
dir := usr.HomeDir
tombPath = strings.Replace(tombPath, "~", dir, 1)
}

f, err := os.OpenFile(tombPath, os.O_RDONLY, 0)
if err != nil {
c.Printf("Unable to open %s - Please try again.\n", tombPath)
continue;
}
f.Close()

c.term.SetPrompt("Key for entombed statefile> ")
tombKey, err = c.term.ReadLine()

if err := c.importTombFile(stateFile, tombKey, tombPath); err == nil {
err = c.loadState(stateFile, pw)
}
if err != nil {
c.Printf("Unable to import %s \n", tombPath)
continue
}

c.lastErasureStorageTime = time.Now()
return true, nil
}
}

c.Printf("%s %s\n", termInfoPrefix, msgCreateAccount)
c.Printf("%s\n", termInfoPrefix)
c.Printf("%s Either leave this blank to use the default server, enter a pondserver:// address, or type one of the following server nicknames:\n", termInfoPrefix)
Expand Down Expand Up @@ -1233,6 +1284,53 @@ Handle:
default:
}

case entombCommand:
var f *os.File
var err error
var tombPath string

// Get the tombfile
for {
c.term.SetPrompt("Path to output statefile> ")
tombPath, err = c.term.ReadLine()
if err != nil {
panic(err)
}
if tombPath[:2] == "~/" {
usr, _ := user.Current()
dir := usr.HomeDir
tombPath = strings.Replace(tombPath, "~", dir, 1)
}

f, err = os.OpenFile(tombPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0400)
if err == nil {
break
}
c.Printf("Unable to write to %s - Please try again.\n", tombPath)
}

//Log thingie..
var logText string
log := func(msg string, args ...interface{}) {
logText += fmt.Sprintf(msg, args...)
}

//Do the thing
_, ok := c.entomb(tombPath, f, log)
if ok {
log("\nThe process has completed successfully.\n")
} else {
log("\nThe process failed! Your statefile is still intact. Please restart when ready.")
}

//Show the message
c.Printf("%s\n", logText)

c.ShutdownAndSuspend()
c.Printf("Goodbye!\n")
shouldQuit = true
return

case closeCommand:
c.setCurrentObject(nil)

Expand Down