Skip to content

Commit

Permalink
cmdline/gendoc: go1.19 updates, run gofmt on output, lint updates. (#62)
Browse files Browse the repository at this point in the history
This PR moves to go 1.19 formatting, lint and deprecation conventions. It modifies gendoc to run go fmt -s on the output to ensure that the output is consistent with the format expected for the currently installed/used version of go.
  • Loading branch information
cosnicolaou authored Aug 26, 2022
1 parent d019a4c commit efbcfc9
Show file tree
Hide file tree
Showing 40 changed files with 394 additions and 340 deletions.
7 changes: 2 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,20 @@ jobs:
name: downloads
command: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
go get github.com/matthewloring/validjson/cmd/validjson
go install -x github.com/matthewloring/validjson/cmd/validjson@latest
- run:
name: lint
command: |
golangci-lint run ./...
validjson ./...
workflows:
circleci:
jobs:
- test-linux:
matrix:
parameters:
go: ["1.13", "1.17"]
go: ["1.16", "1.19"]
- test-windows
- lint:
matrix:
parameters:
go: ["1.17"]
go: ["1.19"]
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ run:
timeout: 5m
issues-exit-code: 1
tests: true
build-tags:

linters-settings:
gocyclo:
min-complexity: 15

linters:
enable:
- deadcode
- gocritic
- gocyclo
- gofmt
Expand All @@ -22,8 +20,6 @@ linters:
- misspell
- exportloopref
- staticcheck
- structcheck
- typecheck
- unconvert
- varcheck
disable-all: true
16 changes: 8 additions & 8 deletions cmd/flagvar/flagvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ func literalDefault(typeName, literal string, initialValue interface{}) (value i
// Embedded (anonymous) structs may be used provided that they are not themselves
// tagged. For example:
//
// type CommonFlags struct {
// A int `cmdline:"a,,use a"`
// B int `cmdline:"b,,use b"`
// }
// type CommonFlags struct {
// A int `cmdline:"a,,use a"`
// B int `cmdline:"b,,use b"`
// }
//
// flagSet := struct{
// CommonFlags
// C bool `cmdline:"c,,use c"`
// }
// flagSet := struct{
// CommonFlags
// C bool `cmdline:"c,,use c"`
// }
//
// will result in three flags, --a, --b and --c.
// Note that embedding as a pointer is not supported.
Expand Down
58 changes: 31 additions & 27 deletions cmd/linewrap/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,45 @@ contain leading spaces. Spaces separating input words are output verbatim,
unless it would result in a line with leading or trailing spaces.
Example usage in a unix terminal:
$ cat myfile.txt | linewrap
$ cat myfile.txt | linewrap
See http://godoc.org/v.io/x/lib/textutil#WrapWriter for details on the
formatting algorithm.
Usage:
linewrap [flags]
linewrap [flags]
The linewrap flags are:
-indents=
Comma-separated indentation prefixes. Each entry specifes the prefix to use
for the corresponding paragraph line, or all subsequent paragraph lines if
there are no more entries. E.g. "AA,BBB,C" means the first line in each
paragraph is indented with "AA", the second line with "BBB", and all
subsequent lines with "C". The format of each indent prefix is a Go
interpreted string literal.
https://golang.org/ref/spec#String_literals
-line-term=\n
Line terminator. Every output line is terminated with this string. The
format is a Go interpreted string literal, where \n means newline.
https://golang.org/ref/spec#String_literals
-para-sep=\n
Paragraph separator. Every consecutive pair of non-empty paragraphs is
separated with this string. The format is a Go interpreted string literal,
where \n menas newline.
https://golang.org/ref/spec#String_literals
-width=<terminal width>
Target line width in runes. If negative the line width is unlimited; each
paragraph is output as a single line. If 0 each word is output on its own
line. Defaults to the terminal width.
-indents=
Comma-separated indentation prefixes. Each entry specifes the prefix to use
for the corresponding paragraph line, or all subsequent paragraph lines if
there are no more entries. E.g. "AA,BBB,C" means the first line in each
paragraph is indented with "AA", the second line with "BBB", and all
subsequent lines with "C". The format of each indent prefix is a Go
interpreted string literal.
https://golang.org/ref/spec#String_literals
-line-term=\n
Line terminator. Every output line is terminated with this string. The
format is a Go interpreted string literal, where \n means newline.
https://golang.org/ref/spec#String_literals
-para-sep=\n
Paragraph separator. Every consecutive pair of non-empty paragraphs is
separated with this string. The format is a Go interpreted string literal,
where \n menas newline.
https://golang.org/ref/spec#String_literals
-width=<terminal width>
Target line width in runes. If negative the line width is unlimited; each
paragraph is output as a single line. If 0 each word is output on its own
line. Defaults to the terminal width.
The global flags are:
-metadata=<just specify -metadata to activate>
Displays metadata for the program and exits.
-time=false
Dump timing information to stderr before exiting the program.
-metadata=<just specify -metadata to activate>
Displays metadata for the program and exits.
-time=false
Dump timing information to stderr before exiting the program.
*/
package main
49 changes: 25 additions & 24 deletions cmdline/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//
// The syntax for each command-line program is:
//
// command [flags] [subcommand [flags]]* [args]
// command [flags] [subcommand [flags]]* [args]
//
// Each sequence of flags is associated with the command that immediately
// precedes it. Flags registered on flag.CommandLine are considered global
Expand All @@ -25,7 +25,7 @@
// arguments "help ..."; this behavior is relied on when generating recursive
// help to distinguish between external subcommands with and without children.
//
// Pitfalls
// # Pitfalls
//
// The cmdline package must be in full control of flag parsing. Typically you
// call cmdline.Main in your main function, and flag parsing is taken care of.
Expand All @@ -43,7 +43,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -157,11 +156,11 @@ type Topic struct {
//
// Most main packages should be implemented as follows:
//
// var root := &cmdline.Command{...}
// var root := &cmdline.Command{...}
//
// func main() {
// cmdline.Main(root)
// }
// func main() {
// cmdline.Main(root)
// }
func Main(root *Command) {
env := EnvFromOS()
if env.Timer != nil && len(env.Timer.Intervals) > 0 {
Expand Down Expand Up @@ -197,21 +196,21 @@ var flagTime = flag.Bool("time", false, "Dump timing information to stderr befor
// special processing is required after parsing the args, and before the runner
// is run. An example:
//
// var root := &cmdline.Command{...}
// var root := &cmdline.Command{...}
//
// func main() {
// env := cmdline.EnvFromOS()
// os.Exit(cmdline.ExitCode(parseAndRun(env), env.Stderr))
// }
// func main() {
// env := cmdline.EnvFromOS()
// os.Exit(cmdline.ExitCode(parseAndRun(env), env.Stderr))
// }
//
// func parseAndRun(env *cmdline.Env) error {
// runner, args, err := cmdline.Parse(env, root, os.Args[1:])
// if err != nil {
// return err
// }
// // ... perform initialization that might parse flags ...
// return runner.Run(env, args)
// }
// func parseAndRun(env *cmdline.Env) error {
// runner, args, err := cmdline.Parse(env, root, os.Args[1:])
// if err != nil {
// return err
// }
// // ... perform initialization that might parse flags ...
// return runner.Run(env, args)
// }
//
// Parse merges root flags into flag.CommandLine and sets ContinueOnError, so
// that subsequent calls to flag.Parsed return true.
Expand Down Expand Up @@ -477,7 +476,7 @@ func parseFlags(path []*Command, env *Env, args []string) ([]string, map[string]
// 2) Discard all output (can't be nil, that means stderr).
// 3) Set an empty Usage (can't be nil, that means use the default).
flags.Init(cmd.Name, flag.ContinueOnError)
flags.SetOutput(ioutil.Discard)
flags.SetOutput(io.Discard)
flags.Usage = func() {}
if isRoot {
// If this is the root command, we must remember to undo the above changes
Expand Down Expand Up @@ -576,9 +575,11 @@ func (x ErrExitCode) Error() string {
const ErrUsage = ErrExitCode(2)

// ExitCode returns the exit code corresponding to err.
// 0: if err == nil
// code: if err is ErrExitCode(code)
// 1: all other errors
//
// 0: if err == nil
// code: if err is ErrExitCode(code)
// 1: all other errors
//
// Writes the error message for "all other errors" to w, if w is non-nil.
func ExitCode(err error, w io.Writer) int {
if err == nil {
Expand Down
3 changes: 1 addition & 2 deletions cmdline/cmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -2490,7 +2489,7 @@ func TestRootCommandFlags(t *testing.T) {

func TestExternalSubcommand(t *testing.T) {
// Create a temporary directory for the external subcommands.
tmpDir, err := ioutil.TempDir("", "cmdline-test")
tmpDir, err := os.MkdirTemp("", "cmdline-test")
if err != nil {
t.Fatalf("%v", err)
}
Expand Down
37 changes: 19 additions & 18 deletions cmdline/gendoc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@

/*
Usage of gendoc:
-build-cmd string
Comand to use for building/installing commands whose usage is to be documented, it must accept the same flags as 'go install'.
-copyright-notice string
File containing the copyright notice to be prepended to the autogenerated documentation; if specified as an empty string then no copyright notice will be used.
-env string
Environment variables to set before running command. If "os", grabs vars from the underlying OS. If empty, doesn't set any vars. Otherwise vars are expected to be comma-separated entries of the form KEY1=VALUE1,KEY2=VALUE2,... (default "os")
-go-flag-pkg
Set if the command is using the standard go flag package, it sets both use-stderr and postprocess-output to true
-install string
Comma separated list of packages to install before running command. All commands that are built will be on the PATH.
-out string
Path to the output file. (default "./doc.go")
-postprocess-output
If set, the help/usage output will be post processed to remove absolute path names that contain the build directory.
-tags string
Tags for go build, also added as build constraints in the generated output file.
-use-stderr
If set, read usage output from stderr rather than stdout; it also ignores the exit status of the command.
-build-cmd string
Comand to use for building/installing commands whose usage is to be documented, it must accept the same flags as 'go install'.
-copyright-notice string
File containing the copyright notice to be prepended to the autogenerated documentation; if specified as an empty string then no copyright notice will be used.
-env string
Environment variables to set before running command. If "os", grabs vars from the underlying OS. If empty, doesn't set any vars. Otherwise vars are expected to be comma-separated entries of the form KEY1=VALUE1,KEY2=VALUE2,... (default "os")
-go-flag-pkg
Set if the command is using the standard go flag package, it sets both use-stderr and postprocess-output to true
-install string
Comma separated list of packages to install before running command. All commands that are built will be on the PATH.
-out string
Path to the output file. (default "./doc.go")
-postprocess-output
If set, the help/usage output will be post processed to remove absolute path names that contain the build directory.
-tags string
Tags for go build, also added as build constraints in the generated output file.
-use-stderr
If set, read usage output from stderr rather than stdout; it also ignores the exit status of the command.
*/
package main
Loading

0 comments on commit efbcfc9

Please sign in to comment.