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
5package cmd
6
7import (
8	"bytes"
9	"context"
10	"encoding/json"
11	"flag"
12	"fmt"
13	"net/url"
14	"os"
15	"strings"
16
17	"golang.org/x/tools/internal/lsp/browser"
18	"golang.org/x/tools/internal/lsp/debug"
19	"golang.org/x/tools/internal/lsp/source"
20)
21
22// version implements the version command.
23type version struct {
24	app *Application
25}
26
27func (v *version) Name() string      { return "version" }
28func (v *version) Usage() string     { return "" }
29func (v *version) ShortHelp() string { return "print the gopls version information" }
30func (v *version) DetailedHelp(f *flag.FlagSet) {
31	fmt.Fprint(f.Output(), ``)
32	f.PrintDefaults()
33}
34
35// Run prints version information to stdout.
36func (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.
42type bug struct{}
43
44func (b *bug) Name() string      { return "bug" }
45func (b *bug) Usage() string     { return "" }
46func (b *bug) ShortHelp() string { return "report a bug in gopls" }
47func (b *bug) DetailedHelp(f *flag.FlagSet) {
48	fmt.Fprint(f.Output(), ``)
49	f.PrintDefaults()
50}
51
52const goplsBugPrefix = "x/tools/gopls: <DESCRIBE THE PROBLEM>"
53const goplsBugHeader = `ATTENTION: Please answer these questions BEFORE submitting your issue. Thanks!
54
55#### What did you do?
56If possible, provide a recipe for reproducing the error.
57A complete runnable program is good.
58A link on play.golang.org is better.
59A 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.
71func (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
87type apiJSON struct{}
88
89func (j *apiJSON) Name() string      { return "api-json" }
90func (j *apiJSON) Usage() string     { return "" }
91func (j *apiJSON) ShortHelp() string { return "print json describing gopls API" }
92func (j *apiJSON) DetailedHelp(f *flag.FlagSet) {
93	fmt.Fprint(f.Output(), ``)
94	f.PrintDefaults()
95}
96
97func (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
106type licenses struct {
107	app *Application
108}
109
110func (l *licenses) Name() string      { return "licenses" }
111func (l *licenses) Usage() string     { return "" }
112func (l *licenses) ShortHelp() string { return "print licenses of included software" }
113func (l *licenses) DetailedHelp(f *flag.FlagSet) {
114	fmt.Fprint(f.Output(), ``)
115	f.PrintDefaults()
116}
117
118const licensePreamble = `
119gopls is made available under the following BSD-style license:
120
121Copyright (c) 2009 The Go Authors. All rights reserved.
122
123Redistribution and use in source and binary forms, with or without
124modification, are permitted provided that the following conditions are
125met:
126
127   * Redistributions of source code must retain the above copyright
128notice, this list of conditions and the following disclaimer.
129   * Redistributions in binary form must reproduce the above
130copyright notice, this list of conditions and the following disclaimer
131in the documentation and/or other materials provided with the
132distribution.
133   * Neither the name of Google Inc. nor the names of its
134contributors may be used to endorse or promote products derived from
135this software without specific prior written permission.
136
137THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
138"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
139LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
140A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
141OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
142SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
143LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
144DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
145THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
146(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
147OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
148
149gopls implements the LSP specification, which is made available under the following license:
150
151Copyright (c) Microsoft Corporation
152
153All rights reserved.
154
155MIT License
156
157Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
158files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
159modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
160is furnished to do so, subject to the following conditions:
161
162The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
163
164THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
165OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
166BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
167OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
168
169gopls also includes software made available under these licenses:
170`
171
172func (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}
184