github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/cmd/snap/cmd_recovery.go (about)

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