forked from snapcore/snapd
/
cmd_recovery.go
116 lines (98 loc) · 2.87 KB
/
cmd_recovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"errors"
"fmt"
"io"
"strings"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/release"
)
type cmdRecovery struct {
clientMixin
colorMixin
ShowKeys bool `long:"show-keys"`
}
var shortRecoveryHelp = i18n.G("List available recovery systems")
var longRecoveryHelp = i18n.G(`
The recovery command lists the available recovery systems.
With --show-keys it displays recovery keys that can be used to unlock the encrypted partitions if the device-specific automatic unlocking does not work.
`)
func init() {
addCommand("recovery", shortRecoveryHelp, longRecoveryHelp, func() flags.Commander {
// XXX: if we want more/nicer details we can add `snap recovery <system>` later
return &cmdRecovery{}
}, colorDescs.also(
map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"show-keys": i18n.G("Show recovery keys (if available) to unlock encrypted partitions."),
}), nil)
}
func notesForSystem(sys *client.System) string {
if sys.Current {
return "current"
}
return "-"
}
func (x *cmdRecovery) showKeys(w io.Writer) error {
if release.OnClassic {
return errors.New(`command "show-keys" is not available on classic systems`)
}
var srk *client.SystemRecoveryKeysResponse
err := x.client.SystemRecoveryKeys(&srk)
if err != nil {
return err
}
fmt.Fprintf(w, "recovery:\t%s\n", srk.RecoveryKey)
fmt.Fprintf(w, "reinstall:\t%s\n", srk.ReinstallKey)
return nil
}
func (x *cmdRecovery) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
esc := x.getEscapes()
w := tabWriter()
defer w.Flush()
if x.ShowKeys {
return x.showKeys(w)
}
systems, err := x.client.ListSystems()
if err != nil {
return err
}
if len(systems) == 0 {
fmt.Fprintf(Stderr, i18n.G("No recovery systems available.\n"))
return nil
}
fmt.Fprintf(w, i18n.G("Label\tBrand%s\tModel\tNotes\n"), fillerPublisher(esc))
for _, sys := range systems {
// doing it this way because otherwise it's a sea of %s\t%s\t%s
line := []string{
sys.Label,
shortPublisher(esc, &sys.Brand),
sys.Model.Model,
notesForSystem(&sys),
}
fmt.Fprintln(w, strings.Join(line, "\t"))
}
return nil
}