gitee.com/mysnapcore/mysnapd@v0.1.0/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  	"gitee.com/mysnapcore/mysnapd/client"
    31  	"gitee.com/mysnapcore/mysnapd/i18n"
    32  	"gitee.com/mysnapcore/mysnapd/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  	if srk.ReinstallKey != "" {
    78  		fmt.Fprintf(w, "reinstall:\t%s\n", srk.ReinstallKey)
    79  	}
    80  	return nil
    81  }
    82  
    83  func (x *cmdRecovery) Execute(args []string) error {
    84  	if len(args) > 0 {
    85  		return ErrExtraArgs
    86  	}
    87  
    88  	esc := x.getEscapes()
    89  	w := tabWriter()
    90  	defer w.Flush()
    91  
    92  	if x.ShowKeys {
    93  		return x.showKeys(w)
    94  	}
    95  
    96  	systems, err := x.client.ListSystems()
    97  	if err != nil {
    98  		return err
    99  	}
   100  	if len(systems) == 0 {
   101  		fmt.Fprintf(Stderr, i18n.G("No recovery systems available.\n"))
   102  		return nil
   103  	}
   104  
   105  	fmt.Fprintf(w, i18n.G("Label\tBrand%s\tModel\tNotes\n"), fillerPublisher(esc))
   106  	for _, sys := range systems {
   107  		// doing it this way because otherwise it's a sea of %s\t%s\t%s
   108  		line := []string{
   109  			sys.Label,
   110  			shortPublisher(esc, &sys.Brand),
   111  			sys.Model.Model,
   112  			notesForSystem(&sys),
   113  		}
   114  		fmt.Fprintln(w, strings.Join(line, "\t"))
   115  	}
   116  
   117  	return nil
   118  }