Adapt key command to context world.

This commit is contained in:
Pauline Middelink 2017-03-08 20:17:30 +01:00
parent b4526c4e6e
commit 0f7b6ec5ac

View File

@ -1,13 +1,13 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"restic" "restic"
"github.com/spf13/cobra"
"restic/errors" "restic/errors"
"restic/repository" "restic/repository"
"github.com/spf13/cobra"
) )
var cmdKey = &cobra.Command{ var cmdKey = &cobra.Command{
@ -25,15 +25,12 @@ func init() {
cmdRoot.AddCommand(cmdKey) cmdRoot.AddCommand(cmdKey)
} }
func listKeys(s *repository.Repository) error { func listKeys(ctx context.Context, s *repository.Repository) error {
tab := NewTable() tab := NewTable()
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created") tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created")
tab.RowFormat = "%s%-10s %-10s %-10s %s" tab.RowFormat = "%s%-10s %-10s %-10s %s"
done := make(chan struct{}) for id := range s.List(restic.KeyFile, ctx.Done()) {
defer close(done)
for id := range s.List(restic.KeyFile, done) {
k, err := repository.LoadKey(s, id.String()) k, err := repository.LoadKey(s, id.String())
if err != nil { if err != nil {
Warnf("LoadKey() failed: %v\n", err) Warnf("LoadKey() failed: %v\n", err)
@ -124,6 +121,9 @@ func runKey(gopts GlobalOptions, args []string) error {
return errors.Fatal("wrong number of arguments") return errors.Fatal("wrong number of arguments")
} }
ctx, cancel := context.WithCancel(gopts.ctx)
defer cancel()
repo, err := OpenRepository(gopts) repo, err := OpenRepository(gopts)
if err != nil { if err != nil {
return err return err
@ -137,7 +137,7 @@ func runKey(gopts GlobalOptions, args []string) error {
return err return err
} }
return listKeys(repo) return listKeys(ctx, repo)
case "add": case "add":
lock, err := lockRepo(repo) lock, err := lockRepo(repo)
defer unlockRepo(lock) defer unlockRepo(lock)