Skip to content

Commit

Permalink
feat(cmd): add migrate flag to connect command
Browse files Browse the repository at this point in the history
this is important for zero-input environments like docker and qri desktop, which need a surefire way to
get to a running qri node without interruption. callers that want no prompts at all should now use:

$ qri connect --setup --migrate

to auto-run setup when the qri repo path is empty, and auto run migrations before startup
  • Loading branch information
b5 committed Jun 25, 2020
1 parent 1f90248 commit 4a465a9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ RUN mkdir -p $IPFS_PATH && mkdir -p $QRI_PATH \
# VOLUME $QRI_PATH

# Set binary as entrypoint, initalizing ipfs & qri repos if none is mounted
CMD ["qri", "connect", "--setup"]
CMD ["qri", "connect", "--setup", "--migrate"]
11 changes: 11 additions & 0 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ peers & swapping data.`,
}

cmd.Flags().BoolVarP(&o.Setup, "setup", "", false, "run setup if necessary, reading options from environment variables")
cmd.Flags().BoolVarP(&o.Migrate, "migrate", "", false, "automatically run migrations if necessary")
cmd.Flags().StringVarP(&o.Registry, "registry", "", "", "specify registry to setup with. only works when --setup is true")

return cmd
Expand All @@ -53,6 +54,7 @@ type ConnectOptions struct {
inst *lib.Instance
Registry string
Setup bool
Migrate bool
}

// Complete adds any missing configuration that can only be added just before calling Run
Expand All @@ -76,6 +78,15 @@ func (o *ConnectOptions) Complete(f Factory, args []string) (err error) {
return errors.New(repo.ErrNoRepo, "no qri repo exists\nhave you run 'qri setup'?")
}

if o.Migrate {
// any required migration by default interrupts all commands with a prompt
// users setting the migrate flag are indicating we should skip the migration
// prompt, which is this only possible prompt in the `connect` command.
// setting noPrompt to true prior to calling init will have the effect
// of auto-running migrations
noPrompt = true
}

if err = f.Init(); err != nil {
return err
}
Expand Down
24 changes: 20 additions & 4 deletions config/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -126,9 +127,9 @@ func OneToTwo(cfg *config.Config) error {
if err := maybeRemoveIPFSRepo(cfg, oldIPFSPath); err != nil {
log.Debug(err)
fmt.Printf("error removing IPFS repo at %q:\n\t%s", oldIPFSPath, err)
fmt.Printf(`qri has successfully internalized this IPFS repo, and no longer
needs the folder at %q. you may want to remove it
`, oldIPFSPath)
fmt.Printf(`qri has successfully internalized this IPFS repo, and no longer
needs the folder at %q. you may want to remove it
`, oldIPFSPath)
}

return nil
Expand Down Expand Up @@ -302,5 +303,20 @@ longer requires the repo at %q`, oldPath)
}

fmt.Printf("moved IPFS repo from %q into qri repo\n", oldPath)
return os.RemoveAll(oldPath)
if err := os.RemoveAll(oldPath); err != nil {
return err
}

log.Error("successfully migrated repo, shutting down")

var wg sync.WaitGroup
go func() {
<-r.Done()
wg.Done()
}()

wg.Add(1)
cancel()
wg.Wait()
return nil
}

0 comments on commit 4a465a9

Please sign in to comment.