github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/diagnostics/diagnostics.go (about)

     1  package diagnostics
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	humanize "github.com/dustin/go-humanize"
     9  	"github.com/henvic/wedeploycli/color"
    10  	"github.com/henvic/wedeploycli/command/internal/we"
    11  	"github.com/henvic/wedeploycli/defaults"
    12  	"github.com/henvic/wedeploycli/diagnostics"
    13  	"github.com/henvic/wedeploycli/fancy"
    14  	"github.com/henvic/wedeploycli/verbose"
    15  	uuid "github.com/satori/go.uuid"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var (
    20  	serial  bool
    21  	print   bool
    22  	send    bool
    23  	timeout = 15
    24  )
    25  
    26  // DiagnosticsCmd sets the user credential
    27  var DiagnosticsCmd = &cobra.Command{
    28  	Use:    "diagnostics",
    29  	Short:  "Run system diagnostics and show report",
    30  	RunE:   diagnosticsRun,
    31  	Args:   cobra.NoArgs,
    32  	Hidden: true,
    33  }
    34  
    35  func diagnosticsRun(cmd *cobra.Command, args []string) error {
    36  	print = print || verbose.Enabled
    37  	var d = diagnostics.Diagnostics{
    38  		Timeout:     time.Duration(timeout) * time.Second,
    39  		Executables: diagnostics.Executables,
    40  		Serial:      serial,
    41  	}
    42  
    43  	fmt.Println("Running diagnostics tools...")
    44  	d.Run(context.Background())
    45  
    46  	var report = d.Collect()
    47  	fmt.Println()
    48  
    49  	if print {
    50  		fmt.Printf("%s", report)
    51  	}
    52  
    53  	bu := uint64(len([]byte(report)))
    54  
    55  	fmt.Println(fancy.Info("Diagnostics report size: ") +
    56  		color.Format(color.Bold, humanize.Bytes(bu)))
    57  
    58  	if !send && !cmd.Flag("send").Changed {
    59  		var report, askErr = fancy.Boolean("Send this report to Liferay Cloud?")
    60  
    61  		if askErr != nil {
    62  			return askErr
    63  		}
    64  
    65  		if report {
    66  			send = true
    67  		}
    68  	}
    69  
    70  	if !send {
    71  		return nil
    72  	}
    73  
    74  	return submit(report)
    75  }
    76  
    77  func submit(report diagnostics.Report) error {
    78  	var username string
    79  	var wectx = we.Context()
    80  	var conf = wectx.Config()
    81  	var params = conf.GetParams()
    82  	var rl = params.Remotes
    83  
    84  	if rl.Has(defaults.CloudRemote) {
    85  		cloudRemote := rl.Get(defaults.CloudRemote)
    86  		username = cloudRemote.Username
    87  	}
    88  
    89  	var entry = diagnostics.Entry{
    90  		ID:       uuid.NewV4().String(),
    91  		Username: username,
    92  		Report:   report,
    93  	}
    94  
    95  	var err = diagnostics.Submit(context.Background(), entry)
    96  
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	fmt.Println(fancy.Info("Report ID: ") + entry.ID)
   102  	fmt.Println(fancy.Info("In case you need support, providing the ID will help us to diagnose your situation."))
   103  	return nil
   104  }
   105  
   106  func init() {
   107  	DiagnosticsCmd.Flags().BoolVar(
   108  		&serial,
   109  		"serial",
   110  		false,
   111  		"Do not run diagnostics in parallel")
   112  
   113  	DiagnosticsCmd.Flags().BoolVar(
   114  		&print,
   115  		"print",
   116  		false,
   117  		"Print diagnostics")
   118  
   119  	DiagnosticsCmd.Flags().BoolVar(
   120  		&send,
   121  		"send",
   122  		false,
   123  		"Send to Liferay Cloud")
   124  
   125  	DiagnosticsCmd.Flags().IntVar(
   126  		&timeout,
   127  		"timeout",
   128  		15,
   129  		"Timeout for the diagnostics in seconds")
   130  
   131  	DiagnosticsCmd.Flag("serial").Hidden = true
   132  	DiagnosticsCmd.Flag("timeout").Hidden = true
   133  }