github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap-repair/cmd_show.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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  	"io"
    25  	"strings"
    26  )
    27  
    28  func init() {
    29  	const (
    30  		short = "Shows specific repairs run on this device"
    31  		long  = ""
    32  	)
    33  
    34  	if _, err := parser.AddCommand("show", short, long, &cmdShow{}); err != nil {
    35  		panic(err)
    36  	}
    37  
    38  }
    39  
    40  type cmdShow struct {
    41  	Positional struct {
    42  		Repair []string `positional-arg-name:"<repair>"`
    43  	} `positional-args:"yes"`
    44  }
    45  
    46  func showRepairDetails(w io.Writer, repair string) error {
    47  	i := strings.LastIndex(repair, "-")
    48  	if i < 0 {
    49  		return fmt.Errorf("cannot parse repair %q", repair)
    50  	}
    51  	brand := repair[:i]
    52  	seq := repair[i+1:]
    53  
    54  	repairTraces, err := newRepairTraces(brand, seq)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	if len(repairTraces) == 0 {
    59  		return fmt.Errorf("cannot find repair \"%s-%s\"", brand, seq)
    60  	}
    61  
    62  	for _, trace := range repairTraces {
    63  		fmt.Fprintf(w, "repair: %s\n", trace.Repair())
    64  		fmt.Fprintf(w, "revision: %s\n", trace.Revision())
    65  		fmt.Fprintf(w, "status: %s\n", trace.Status())
    66  		fmt.Fprintf(w, "summary: %s\n", trace.Summary())
    67  
    68  		fmt.Fprintf(w, "script:\n")
    69  		if err := trace.WriteScriptIndented(w, 2); err != nil {
    70  			fmt.Fprintf(w, "%serror: %s\n", indentPrefix(2), err)
    71  		}
    72  
    73  		fmt.Fprintf(w, "output:\n")
    74  		if err := trace.WriteOutputIndented(w, 2); err != nil {
    75  			fmt.Fprintf(w, "%serror: %s\n", indentPrefix(2), err)
    76  		}
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func (c *cmdShow) Execute([]string) error {
    83  	for _, repair := range c.Positional.Repair {
    84  		if err := showRepairDetails(Stdout, repair); err != nil {
    85  			return err
    86  		}
    87  		fmt.Fprintf(Stdout, "\n")
    88  	}
    89  
    90  	return nil
    91  }