github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/cmd/smc/bugcmd.go (about)

     1  // Copyright 2017 The Spectrum Authors
     2  // This file is part of Spectrum.
     3  //
     4  // Spectrum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // Spectrum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with Spectrum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"io/ioutil"
    24  	"net/url"
    25  	"os/exec"
    26  	"runtime"
    27  	"strings"
    28  
    29  	"github.com/SmartMeshFoundation/Spectrum/cmd/internal/browser"
    30  	"github.com/SmartMeshFoundation/Spectrum/params"
    31  
    32  	"github.com/SmartMeshFoundation/Spectrum/cmd/utils"
    33  	cli "gopkg.in/urfave/cli.v1"
    34  )
    35  
    36  var bugCommand = cli.Command{
    37  	Action:    utils.MigrateFlags(reportBug),
    38  	Name:      "bug",
    39  	Usage:     "opens a window to report a bug on the smc repo",
    40  	ArgsUsage: " ",
    41  	Category:  "MISCELLANEOUS COMMANDS",
    42  }
    43  
    44  const issueUrl = "https://github.com/SmartMeshFoundation/Spectrum/issues/new"
    45  
    46  // reportBug reports a bug by opening a new URL to the Spectrum GH issue
    47  // tracker and setting default values as the issue body.
    48  func reportBug(ctx *cli.Context) error {
    49  	// execute template and write contents to buff
    50  	var buff bytes.Buffer
    51  
    52  	fmt.Fprintln(&buff, header)
    53  	fmt.Fprintln(&buff, "Version:", params.Version)
    54  	fmt.Fprintln(&buff, "Go Version:", runtime.Version())
    55  	fmt.Fprintln(&buff, "OS:", runtime.GOOS)
    56  	printOSDetails(&buff)
    57  
    58  	// open a new GH issue
    59  	if !browser.Open(issueUrl + "?body=" + url.QueryEscape(buff.String())) {
    60  		fmt.Printf("Please file a new issue at %s using this template:\n%s", issueUrl, buff.String())
    61  	}
    62  	return nil
    63  }
    64  
    65  // copied from the Go source. Copyright 2017 The Go Authors
    66  func printOSDetails(w io.Writer) {
    67  	switch runtime.GOOS {
    68  	case "darwin":
    69  		printCmdOut(w, "uname -v: ", "uname", "-v")
    70  		printCmdOut(w, "", "sw_vers")
    71  	case "linux":
    72  		printCmdOut(w, "uname -sr: ", "uname", "-sr")
    73  		printCmdOut(w, "", "lsb_release", "-a")
    74  	case "openbsd", "netbsd", "freebsd", "dragonfly":
    75  		printCmdOut(w, "uname -v: ", "uname", "-v")
    76  	case "solaris":
    77  		out, err := ioutil.ReadFile("/etc/release")
    78  		if err == nil {
    79  			fmt.Fprintf(w, "/etc/release: %s\n", out)
    80  		} else {
    81  			fmt.Printf("failed to read /etc/release: %v\n", err)
    82  		}
    83  	}
    84  }
    85  
    86  // printCmdOut prints the output of running the given command.
    87  // It ignores failures; 'go bug' is best effort.
    88  //
    89  // copied from the Go source. Copyright 2017 The Go Authors
    90  func printCmdOut(w io.Writer, prefix, path string, args ...string) {
    91  	cmd := exec.Command(path, args...)
    92  	out, err := cmd.Output()
    93  	if err != nil {
    94  		fmt.Printf("%s %s: %v\n", path, strings.Join(args, " "), err)
    95  		return
    96  	}
    97  	fmt.Fprintf(w, "%s%s\n", prefix, bytes.TrimSpace(out))
    98  }
    99  
   100  const header = `Please answer these questions before submitting your issue. Thanks!
   101  
   102  #### What did you do?
   103   
   104  #### What did you expect to see?
   105   
   106  #### What did you see instead?
   107   
   108  #### System details
   109  `