Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Resolve misc. lint violations and formatting issues (#471)
Browse files Browse the repository at this point in the history
* Ignore staticcheck violations about error message formatting

* Avoid using `strings.Title`

* Rename error type to start with "err"

* Iterate over string runes directly

* go fmt code comments

* go fmt build directives
  • Loading branch information
mislav authored Nov 24, 2022
1 parent f0d409e commit 82a5fab
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (c *Confirm) getBool(showHelp bool, config *PromptConfig) (bool, error) {
continue
default:
// we didnt get a valid answer, so print error and prompt again
//lint:ignore ST1005 it should be fine for this error message to have punctuation
if err := c.Error(config, fmt.Errorf("%q is not a valid answer, please try again.", val)); err != nil {
return c.Default, err
}
Expand Down
20 changes: 10 additions & 10 deletions core/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ var TemplateFuncsNoColor = map[string]interface{}{
},
}

//RunTemplate returns two formatted strings given a template and
//the data it requires. The first string returned is generated for
//user-facing output and may or may not contain ANSI escape codes
//for colored output. The second string does not contain escape codes
//and can be used by the renderer for layout purposes.
// RunTemplate returns two formatted strings given a template and
// the data it requires. The first string returned is generated for
// user-facing output and may or may not contain ANSI escape codes
// for colored output. The second string does not contain escape codes
// and can be used by the renderer for layout purposes.
func RunTemplate(tmpl string, data interface{}) (string, string, error) {
tPair, err := GetTemplatePair(tmpl)
if err != nil {
Expand All @@ -52,11 +52,11 @@ var (
memoMutex = &sync.RWMutex{}
)

//GetTemplatePair returns a pair of compiled templates where the
//first template is generated for user-facing output and the
//second is generated for use by the renderer. The second
//template does not contain any color escape codes, whereas
//the first template may or may not depending on DisableColor.
// GetTemplatePair returns a pair of compiled templates where the
// first template is generated for user-facing output and the
// second is generated for use by the renderer. The second
// template does not contain any color escape codes, whereas
// the first template may or may not depending on DisableColor.
func GetTemplatePair(tmpl string) ([2]*template.Template, error) {
memoMutex.RLock()
if t, ok := memoizedGetTemplate[tmpl]; ok {
Expand Down
14 changes: 8 additions & 6 deletions core/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ func (err errFieldNotMatch) Is(target error) bool { // implements the dynamic er
// It returns the Question.Name that couldn't be matched with a destination field.
//
// Usage:
// err := survey.Ask(qs, &v);
// if err != nil {
// if name, ok := core.IsFieldNotMatch(err); ok {
// [...name is the not matched question name]
// }
// }
//
// if err := survey.Ask(qs, &v); err != nil {
// if name, ok := core.IsFieldNotMatch(err); ok {
// // name is the question name that did not match a field
// }
// }
func IsFieldNotMatch(err error) (string, bool) {
if err != nil {
if v, ok := err.(errFieldNotMatch); ok {
Expand Down Expand Up @@ -301,6 +301,7 @@ func copy(t reflect.Value, v reflect.Value) (err error) {
case reflect.Float64:
castVal, casterr = strconv.ParseFloat(vString, 64)
default:
//lint:ignore ST1005 allow this error message to be capitalized
return fmt.Errorf("Unable to convert from string to type %s", t.Kind())
}

Expand Down Expand Up @@ -335,6 +336,7 @@ func copy(t reflect.Value, v reflect.Value) (err error) {
}

// we're copying an option answer to an incorrect type
//lint:ignore ST1005 allow this error message to be capitalized
return fmt.Errorf("Unable to convert from OptionAnswer to type %s", t.Kind())
}

Expand Down
6 changes: 3 additions & 3 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
)

if err == nil {
err = readLineAgain
err = errReadLineAgain
}

return []rune(i.typedAnswer), true, err
})
}

var readLineAgain = errors.New("read line again")
var errReadLineAgain = errors.New("read line again")

func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
// render the template
Expand Down Expand Up @@ -170,7 +170,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
}

line, err = rr.ReadLineWithDefault(0, line, i.onRune(config))
if err == readLineAgain {
if err == errReadLineAgain {
continue
}

Expand Down
1 change: 0 additions & 1 deletion survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ in the documentation. For example:
}
survey.AskOne(prompt, &name)
*/
func AskOne(p Prompt, response interface{}, opts ...AskOpt) error {
err := Ask([]*Question{{Prompt: p}}, response, opts...)
Expand Down
1 change: 1 addition & 0 deletions terminal/display_posix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package terminal
Expand Down
1 change: 1 addition & 0 deletions terminal/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import (
)

var (
//lint:ignore ST1012 keeping old name for backwards compatibility
InterruptErr = errors.New("interrupt")
)
1 change: 1 addition & 0 deletions terminal/output.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package terminal
Expand Down
3 changes: 1 addition & 2 deletions terminal/runereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ func StringWidth(str string) int {
w := 0
ansi := false

rs := []rune(str)
for _, r := range rs {
for _, r := range str {
// increase width only when outside of ANSI escape sequences
if ansi || isAnsiMarker(r) {
ansi = !isAnsiTerminator(r)
Expand Down
1 change: 1 addition & 0 deletions terminal/runereader_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package terminal
Expand Down
1 change: 1 addition & 0 deletions terminal/runereader_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux && !ppc64le
// +build linux,!ppc64le

package terminal
Expand Down
1 change: 1 addition & 0 deletions terminal/runereader_posix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

// The terminal mode manipulation code is derived heavily from:
Expand Down
1 change: 1 addition & 0 deletions terminal/runereader_ppc64le.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ppc64le && linux
// +build ppc64le,linux

package terminal
Expand Down
5 changes: 4 additions & 1 deletion transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package survey
import (
"reflect"
"strings"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

// TransformString returns a `Transformer` based on the "f"
Expand Down Expand Up @@ -62,7 +65,7 @@ func ToLower(ans interface{}) interface{} {
// return a nil value, meaning that the above answer
// will not be affected by this call at all.
func Title(ans interface{}) interface{} {
transformer := TransformString(strings.Title)
transformer := TransformString(cases.Title(language.English).String)
return transformer(ans)
}

Expand Down
1 change: 1 addition & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func Required(val interface{}) error {

// if the value passed in is the zero value of the appropriate type
if isZero(value) && value.Kind() != reflect.Bool {
//lint:ignore ST1005 this error message should render as capitalized
return errors.New("Value is required")
}
return nil
Expand Down

0 comments on commit 82a5fab

Please sign in to comment.