github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/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  	"fmt"
    24  	"strings"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  	"github.com/snapcore/snapd/i18n"
    30  )
    31  
    32  type cmdRecovery struct {
    33  	clientMixin
    34  	colorMixin
    35  }
    36  
    37  var shortRecoveryHelp = i18n.G("List available recovery systems")
    38  var longRecoveryHelp = i18n.G(`
    39  The recovery command lists the available recovery systems.
    40  `)
    41  
    42  func init() {
    43  	addCommand("recovery", shortRecoveryHelp, longRecoveryHelp, func() flags.Commander {
    44  		// XXX: if we want more/nicer details we can add `snap recovery <system>` later
    45  		return &cmdRecovery{}
    46  	}, nil, nil)
    47  }
    48  
    49  func notesForSystem(sys *client.System) string {
    50  	if sys.Current {
    51  		return "current"
    52  	}
    53  	return "-"
    54  }
    55  
    56  func (x *cmdRecovery) Execute(args []string) error {
    57  	if len(args) > 0 {
    58  		return ErrExtraArgs
    59  	}
    60  
    61  	systems, err := x.client.ListSystems()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	if len(systems) == 0 {
    66  		fmt.Fprintf(Stderr, i18n.G("No recovery systems available.\n"))
    67  		return nil
    68  	}
    69  
    70  	esc := x.getEscapes()
    71  	w := tabWriter()
    72  	defer w.Flush()
    73  	fmt.Fprintf(w, i18n.G("Label\tBrand\tModel\tNotes\n"))
    74  	for _, sys := range systems {
    75  		// doing it this way because otherwise it's a sea of %s\t%s\t%s
    76  		line := []string{
    77  			sys.Label,
    78  			shortPublisher(esc, &sys.Brand),
    79  			sys.Model.Model,
    80  			notesForSystem(&sys),
    81  		}
    82  		fmt.Fprintln(w, strings.Join(line, "\t"))
    83  	}
    84  
    85  	return nil
    86  }