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