github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/lsp/cmd/info.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cmd
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"encoding/json"
    11  	"flag"
    12  	"fmt"
    13  	"net/url"
    14  	"os"
    15  	"strings"
    16  
    17  	"github.com/jhump/golang-x-tools/internal/lsp/browser"
    18  	"github.com/jhump/golang-x-tools/internal/lsp/debug"
    19  	"github.com/jhump/golang-x-tools/internal/lsp/source"
    20  )
    21  
    22  // version implements the version command.
    23  type version struct {
    24  	JSON bool `flag:"json" help:"outputs in json format."`
    25  
    26  	app *Application
    27  }
    28  
    29  func (v *version) Name() string      { return "version" }
    30  func (v *version) Parent() string    { return v.app.Name() }
    31  func (v *version) Usage() string     { return "" }
    32  func (v *version) ShortHelp() string { return "print the gopls version information" }
    33  func (v *version) DetailedHelp(f *flag.FlagSet) {
    34  	fmt.Fprint(f.Output(), ``)
    35  	printFlagDefaults(f)
    36  }
    37  
    38  // Run prints version information to stdout.
    39  func (v *version) Run(ctx context.Context, args ...string) error {
    40  	var mode = debug.PlainText
    41  	if v.JSON {
    42  		mode = debug.JSON
    43  	}
    44  
    45  	return debug.PrintVersionInfo(ctx, os.Stdout, v.app.verbose(), mode)
    46  }
    47  
    48  // bug implements the bug command.
    49  type bug struct {
    50  	app *Application
    51  }
    52  
    53  func (b *bug) Name() string      { return "bug" }
    54  func (b *bug) Parent() string    { return b.app.Name() }
    55  func (b *bug) Usage() string     { return "" }
    56  func (b *bug) ShortHelp() string { return "report a bug in gopls" }
    57  func (b *bug) DetailedHelp(f *flag.FlagSet) {
    58  	fmt.Fprint(f.Output(), ``)
    59  	printFlagDefaults(f)
    60  }
    61  
    62  const goplsBugPrefix = "x/tools/gopls: <DESCRIBE THE PROBLEM>"
    63  const goplsBugHeader = `ATTENTION: Please answer these questions BEFORE submitting your issue. Thanks!
    64  
    65  #### What did you do?
    66  If possible, provide a recipe for reproducing the error.
    67  A complete runnable program is good.
    68  A link on play.golang.org is better.
    69  A failing unit test is the best.
    70  
    71  #### What did you expect to see?
    72  
    73  
    74  #### What did you see instead?
    75  
    76  
    77  `
    78  
    79  // Run collects some basic information and then prepares an issue ready to
    80  // be reported.
    81  func (b *bug) Run(ctx context.Context, args ...string) error {
    82  	buf := &bytes.Buffer{}
    83  	fmt.Fprint(buf, goplsBugHeader)
    84  	debug.PrintVersionInfo(ctx, buf, true, debug.Markdown)
    85  	body := buf.String()
    86  	title := strings.Join(args, " ")
    87  	if !strings.HasPrefix(title, goplsBugPrefix) {
    88  		title = goplsBugPrefix + title
    89  	}
    90  	if !browser.Open("https://github.com/golang/go/issues/new?title=" + url.QueryEscape(title) + "&body=" + url.QueryEscape(body)) {
    91  		fmt.Print("Please file a new issue at golang.org/issue/new using this template:\n\n")
    92  		fmt.Print(body)
    93  	}
    94  	return nil
    95  }
    96  
    97  type apiJSON struct {
    98  	app *Application
    99  }
   100  
   101  func (j *apiJSON) Name() string      { return "api-json" }
   102  func (j *apiJSON) Parent() string    { return j.app.Name() }
   103  func (j *apiJSON) Usage() string     { return "" }
   104  func (j *apiJSON) ShortHelp() string { return "print json describing gopls API" }
   105  func (j *apiJSON) DetailedHelp(f *flag.FlagSet) {
   106  	fmt.Fprint(f.Output(), ``)
   107  	printFlagDefaults(f)
   108  }
   109  
   110  func (j *apiJSON) Run(ctx context.Context, args ...string) error {
   111  	js, err := json.MarshalIndent(source.GeneratedAPIJSON, "", "\t")
   112  	if err != nil {
   113  		return err
   114  	}
   115  	fmt.Fprint(os.Stdout, string(js))
   116  	return nil
   117  }
   118  
   119  type licenses struct {
   120  	app *Application
   121  }
   122  
   123  func (l *licenses) Name() string      { return "licenses" }
   124  func (l *licenses) Parent() string    { return l.app.Name() }
   125  func (l *licenses) Usage() string     { return "" }
   126  func (l *licenses) ShortHelp() string { return "print licenses of included software" }
   127  func (l *licenses) DetailedHelp(f *flag.FlagSet) {
   128  	fmt.Fprint(f.Output(), ``)
   129  	printFlagDefaults(f)
   130  }
   131  
   132  const licensePreamble = `
   133  gopls is made available under the following BSD-style license:
   134  
   135  Copyright (c) 2009 The Go Authors. All rights reserved.
   136  
   137  Redistribution and use in source and binary forms, with or without
   138  modification, are permitted provided that the following conditions are
   139  met:
   140  
   141     * Redistributions of source code must retain the above copyright
   142  notice, this list of conditions and the following disclaimer.
   143     * Redistributions in binary form must reproduce the above
   144  copyright notice, this list of conditions and the following disclaimer
   145  in the documentation and/or other materials provided with the
   146  distribution.
   147     * Neither the name of Google Inc. nor the names of its
   148  contributors may be used to endorse or promote products derived from
   149  this software without specific prior written permission.
   150  
   151  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   152  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   153  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   154  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   155  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   156  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   157  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   158  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   159  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   160  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   161  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   162  
   163  gopls implements the LSP specification, which is made available under the following license:
   164  
   165  Copyright (c) Microsoft Corporation
   166  
   167  All rights reserved.
   168  
   169  MIT License
   170  
   171  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
   172  files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
   173  modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
   174  is furnished to do so, subject to the following conditions:
   175  
   176  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
   177  
   178  THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
   179  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   180  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
   181  OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   182  
   183  gopls also includes software made available under these licenses:
   184  `
   185  
   186  func (l *licenses) Run(ctx context.Context, args ...string) error {
   187  	opts := source.DefaultOptions()
   188  	l.app.options(opts)
   189  	txt := licensePreamble
   190  	if opts.LicensesText == "" {
   191  		txt += "(development gopls, license information not available)"
   192  	} else {
   193  		txt += opts.LicensesText
   194  	}
   195  	fmt.Fprint(os.Stdout, txt)
   196  	return nil
   197  }