| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cmd |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"context" |
| 12 |
"flag" |
| 13 |
"fmt" |
| 14 |
"io" |
| 15 |
"os" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"golang.org/x/tools/gopls/internal/debug" |
| 19 |
"golang.org/x/tools/gopls/internal/doc" |
| 20 |
licensespkg "golang.org/x/tools/gopls/internal/licenses" |
| 21 |
"golang.org/x/tools/gopls/internal/tool" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
type help struct { |
| 26 |
app *Application |
| 27 |
} |
| 28 |
|
| 29 |
func (h *help) Name() string { return "help" } |
| 30 |
func (h *help) Parent() string { return h.app.Name() } |
| 31 |
func (h *help) Usage() string { return "" } |
| 32 |
func (h *help) ShortHelp() string { return "print usage information for subcommands" } |
| 33 |
func (h *help) DetailedHelp(f *flag.FlagSet) { |
| 34 |
fmt.Fprint(f.Output(), ` |
| 35 |
|
| 36 |
Examples: |
| 37 |
$ gopls help # main gopls help message |
| 38 |
$ gopls help remote # help on 'remote' command |
| 39 |
$ gopls help remote sessions # help on 'remote sessions' subcommand |
| 40 |
`) |
| 41 |
printFlagDefaults(f) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (h *help) Run(ctx context.Context, args ...string) error { |
| 46 |
find := func(cmds []tool.Application, name string) tool.Application { |
| 47 |
for _, cmd := range cmds { |
| 48 |
if cmd.Name() == name { |
| 49 |
return cmd |
| 50 |
} |
| 51 |
} |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
var cmd tool.Application = h.app |
| 57 |
for i, arg := range args { |
| 58 |
cmd = find(getSubcommands(cmd), arg) |
| 59 |
if cmd == nil { |
| 60 |
return tool.CommandLineErrorf( |
| 61 |
"no such subcommand: %s", strings.Join(args[:i+1], " ")) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
fs := flag.NewFlagSet(cmd.Name(), flag.ExitOnError) |
| 69 |
return tool.Run(ctx, fs, h.app, append(args[:len(args):len(args)], "-h")) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
type version struct { |
| 74 |
JSON bool `flag:"json" help:"outputs in json format."` |
| 75 |
|
| 76 |
app *Application |
| 77 |
} |
| 78 |
|
| 79 |
func (v *version) Name() string { return "version" } |
| 80 |
func (v *version) Parent() string { return v.app.Name() } |
| 81 |
func (v *version) Usage() string { return "" } |
| 82 |
func (v *version) ShortHelp() string { return "print the gopls version information" } |
| 83 |
func (v *version) DetailedHelp(f *flag.FlagSet) { |
| 84 |
fmt.Fprint(f.Output(), ``) |
| 85 |
printFlagDefaults(f) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (v *version) Run(ctx context.Context, args ...string) error { |
| 90 |
var mode = debug.PlainText |
| 91 |
if v.JSON { |
| 92 |
mode = debug.JSON |
| 93 |
} |
| 94 |
var buf bytes.Buffer |
| 95 |
debug.WriteVersionInfo(&buf, v.app.verbose(), mode) |
| 96 |
_, err := io.Copy(os.Stdout, &buf) |
| 97 |
return err |
| 98 |
} |
| 99 |
|
| 100 |
type apiJSON struct { |
| 101 |
app *Application |
| 102 |
} |
| 103 |
|
| 104 |
func (j *apiJSON) Name() string { return "api-json" } |
| 105 |
func (j *apiJSON) Parent() string { return j.app.Name() } |
| 106 |
func (j *apiJSON) Usage() string { return "" } |
| 107 |
func (j *apiJSON) ShortHelp() string { return "print JSON describing gopls API" } |
| 108 |
func (j *apiJSON) DetailedHelp(f *flag.FlagSet) { |
| 109 |
fmt.Fprint(f.Output(), ` |
| 110 |
The api-json command prints a JSON value that describes |
| 111 |
and documents all gopls' public interfaces. |
| 112 |
Its schema is defined by golang.org/x/tools/gopls/internal/doc.API. |
| 113 |
`) |
| 114 |
printFlagDefaults(f) |
| 115 |
} |
| 116 |
|
| 117 |
func (j *apiJSON) Run(ctx context.Context, args ...string) error { |
| 118 |
os.Stdout.WriteString(doc.JSON) |
| 119 |
fmt.Println() |
| 120 |
return nil |
| 121 |
} |
| 122 |
|
| 123 |
type licenses struct { |
| 124 |
app *Application |
| 125 |
} |
| 126 |
|
| 127 |
func (l *licenses) Name() string { return "licenses" } |
| 128 |
func (l *licenses) Parent() string { return l.app.Name() } |
| 129 |
func (l *licenses) Usage() string { return "" } |
| 130 |
func (l *licenses) ShortHelp() string { return "print licenses of included software" } |
| 131 |
func (l *licenses) DetailedHelp(f *flag.FlagSet) { |
| 132 |
fmt.Fprint(f.Output(), ``) |
| 133 |
printFlagDefaults(f) |
| 134 |
} |
| 135 |
|
| 136 |
const licensePreamble = ` |
| 137 |
gopls is made available under the following BSD-style license: |
| 138 |
|
| 139 |
Copyright (c) 2009 The Go Authors. All rights reserved. |
| 140 |
|
| 141 |
Redistribution and use in source and binary forms, with or without |
| 142 |
modification, are permitted provided that the following conditions are |
| 143 |
met: |
| 144 |
|
| 145 |
* Redistributions of source code must retain the above copyright |
| 146 |
notice, this list of conditions and the following disclaimer. |
| 147 |
* Redistributions in binary form must reproduce the above |
| 148 |
copyright notice, this list of conditions and the following disclaimer |
| 149 |
in the documentation and/or other materials provided with the |
| 150 |
distribution. |
| 151 |
* Neither the name of Google Inc. nor the names of its |
| 152 |
contributors may be used to endorse or promote products derived from |
| 153 |
this software without specific prior written permission. |
| 154 |
|
| 155 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 156 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 157 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 158 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 159 |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 160 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 161 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 162 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 163 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 164 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 165 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 166 |
|
| 167 |
gopls implements the LSP specification, which is made available under the following license: |
| 168 |
|
| 169 |
Copyright (c) Microsoft Corporation |
| 170 |
|
| 171 |
All rights reserved. |
| 172 |
|
| 173 |
MIT License |
| 174 |
|
| 175 |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
| 176 |
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, |
| 177 |
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software |
| 178 |
is furnished to do so, subject to the following conditions: |
| 179 |
|
| 180 |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 181 |
|
| 182 |
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 183 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 184 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT |
| 185 |
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 186 |
|
| 187 |
gopls also includes software made available under these licenses: |
| 188 |
` |
| 189 |
|
| 190 |
func (l *licenses) Run(ctx context.Context, args ...string) error { |
| 191 |
txt := licensePreamble |
| 192 |
if licensespkg.Text == "" { |
| 193 |
txt += "(development gopls, license information not available)" |
| 194 |
} else { |
| 195 |
txt += licensespkg.Text |
| 196 |
} |
| 197 |
fmt.Fprint(os.Stdout, txt) |
| 198 |
return nil |
| 199 |
} |