github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/quickchain/bugcmd.go (about)

     1  
     2  package main
     3  
     4  import (
     5  	"bytes"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"net/url"
    10  	"os/exec"
    11  	"runtime"
    12  	"strings"
    13  
    14  	"github.com/quickchainproject/quickchain/cmd/internal/browser"
    15  	"github.com/quickchainproject/quickchain/params"
    16  
    17  	"github.com/quickchainproject/quickchain/cmd/utils"
    18  	cli "gopkg.in/urfave/cli.v1"
    19  )
    20  
    21  var bugCommand = cli.Command{
    22  	Action:    utils.MigrateFlags(reportBug),
    23  	Name:      "bug",
    24  	Usage:     "opens a window to report a bug on the quickchain repo",
    25  	ArgsUsage: " ",
    26  	Category:  "MISCELLANEOUS COMMANDS",
    27  }
    28  
    29  const issueUrl = "https://github.com/quickchainproject/quickchain/issues/new"
    30  
    31  // reportBug reports a bug by opening a new URL to the quickchain GH issue
    32  // tracker and setting default values as the issue body.
    33  func reportBug(ctx *cli.Context) error {
    34  	// execute template and write contents to buff
    35  	var buff bytes.Buffer
    36  
    37  	fmt.Fprintln(&buff, "#### System information")
    38  	fmt.Fprintln(&buff)
    39  	fmt.Fprintln(&buff, "Version:", params.Version)
    40  	fmt.Fprintln(&buff, "Go Version:", runtime.Version())
    41  	fmt.Fprintln(&buff, "OS:", runtime.GOOS)
    42  	printOSDetails(&buff)
    43  	fmt.Fprintln(&buff, header)
    44  
    45  	// open a new GH issue
    46  	if !browser.Open(issueUrl + "?body=" + url.QueryEscape(buff.String())) {
    47  		fmt.Printf("Please file a new issue at %s using this template:\n\n%s", issueUrl, buff.String())
    48  	}
    49  	return nil
    50  }
    51  
    52  // copied from the Go source. Copyright 2017 The Go Authors
    53  func printOSDetails(w io.Writer) {
    54  	switch runtime.GOOS {
    55  	case "darwin":
    56  		printCmdOut(w, "uname -v: ", "uname", "-v")
    57  		printCmdOut(w, "", "sw_vers")
    58  	case "linux":
    59  		printCmdOut(w, "uname -sr: ", "uname", "-sr")
    60  		printCmdOut(w, "", "lsb_release", "-a")
    61  	case "openbsd", "netbsd", "freebsd", "dragonfly":
    62  		printCmdOut(w, "uname -v: ", "uname", "-v")
    63  	case "solaris":
    64  		out, err := ioutil.ReadFile("/etc/release")
    65  		if err == nil {
    66  			fmt.Fprintf(w, "/etc/release: %s\n", out)
    67  		} else {
    68  			fmt.Printf("failed to read /etc/release: %v\n", err)
    69  		}
    70  	}
    71  }
    72  
    73  // printCmdOut prints the output of running the given command.
    74  // It ignores failures; 'go bug' is best effort.
    75  //
    76  // copied from the Go source. Copyright 2017 The Go Authors
    77  func printCmdOut(w io.Writer, prefix, path string, args ...string) {
    78  	cmd := exec.Command(path, args...)
    79  	out, err := cmd.Output()
    80  	if err != nil {
    81  		fmt.Printf("%s %s: %v\n", path, strings.Join(args, " "), err)
    82  		return
    83  	}
    84  	fmt.Fprintf(w, "%s%s\n", prefix, bytes.TrimSpace(out))
    85  }
    86  
    87  const header = `
    88  #### Expected behaviour
    89  
    90  
    91  #### Actual behaviour
    92  
    93  
    94  #### Steps to reproduce the behaviour
    95  
    96  
    97  #### Backtrace
    98  `