diff --git a/.golangci.yml b/.golangci.yml index 2a7a05e63..900bb9eeb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,7 +9,6 @@ run: - ui/networks - ui/pods - ui/system - - ui/utils linters: enable-all: true disable: diff --git a/ui/utils/eventkey.go b/ui/utils/eventkey.go index fe78b6b07..b2714a5b1 100644 --- a/ui/utils/eventkey.go +++ b/ui/utils/eventkey.go @@ -2,12 +2,14 @@ package utils import "github.com/gdamore/tcell/v2" -// StringToEventKey returns list of key events equvalant to the input string +// StringToEventKey returns list of key events equvalant to the input string. func StringToEventKey(input string) []*tcell.EventKey { var events []*tcell.EventKey + for i := 0; i < len(input); i++ { ch := rune(input[i]) - events = append(events, tcell.NewEventKey(256, ch, tcell.ModNone)) + events = append(events, tcell.NewEventKey(256, ch, tcell.ModNone)) //nolint:gomnd } + return events } diff --git a/ui/utils/home.go b/ui/utils/home.go index 16f0765fb..340841f25 100644 --- a/ui/utils/home.go +++ b/ui/utils/home.go @@ -5,12 +5,13 @@ package utils import "github.com/containers/storage/pkg/unshare" -// UserHomeDir returns user's home directory +// UserHomeDir returns user's home directory. func UserHomeDir() (string, error) { - // only get HomeDir when necessary + // only get HomeDir when necessary. home, err := unshare.HomeDir() if err != nil { return "", err } + return home, nil } diff --git a/ui/utils/home_windows.go b/ui/utils/home_windows.go index bd8cd5184..b8e59b711 100644 --- a/ui/utils/home_windows.go +++ b/ui/utils/home_windows.go @@ -7,7 +7,7 @@ import ( "os" ) -// UserHomeDir returns user's home directory +// UserHomeDir returns user's home directory. func UserHomeDir() (string, error) { return os.UserHomeDir() } diff --git a/ui/utils/keys.go b/ui/utils/keys.go index 248c59a0f..28f9db83c 100644 --- a/ui/utils/keys.go +++ b/ui/utils/keys.go @@ -5,22 +5,23 @@ import ( "github.com/rs/zerolog/log" ) -// application key bindings names +// application key bindings names. + var ( CommandMenuKey = uiKeyInfo{ - Key: tcell.Key(256), + Key: tcell.Key(256), //nolint:gomnd KeyRune: rune('m'), KeyLabel: "m", KeyDesc: "display command menu", } NextScreenKey = uiKeyInfo{ - Key: tcell.Key(256), + Key: tcell.Key(256), //nolint:gomnd KeyRune: rune('l'), KeyLabel: "l", KeyDesc: "switch to next screen", } PreviousScreenKey = uiKeyInfo{ - Key: tcell.Key(256), + Key: tcell.Key(256), //nolint:gomnd KeyRune: rune('h'), KeyLabel: "h", KeyDesc: "switch to previous screen", @@ -124,33 +125,31 @@ var ( } ) -var ( - // UIKeysBindings user interface key bindings - UIKeysBindings = []uiKeyInfo{ - CommandMenuKey, - NextScreenKey, - PreviousScreenKey, - MoveUpKey, - MoveDownKey, - CloseDialogKey, - SwitchFocusKey, - DeleteKey, - ArrowUpKey, - ArrowDownKey, - ArrowLeftKey, - ArrowRightKey, - ScrollUpKey, - ScrollDownKey, - AppExitKey, - HelpScreenKey, - SystemScreenKey, - PodsScreenKey, - ContainersScreenKey, - VolumesScreenKey, - ImagesScreenKey, - NetworksScreenKey, - } -) +// UIKeysBindings user interface key bindings. +var UIKeysBindings = []uiKeyInfo{ + CommandMenuKey, + NextScreenKey, + PreviousScreenKey, + MoveUpKey, + MoveDownKey, + CloseDialogKey, + SwitchFocusKey, + DeleteKey, + ArrowUpKey, + ArrowDownKey, + ArrowLeftKey, + ArrowRightKey, + ScrollUpKey, + ScrollDownKey, + AppExitKey, + HelpScreenKey, + SystemScreenKey, + PodsScreenKey, + ContainersScreenKey, + VolumesScreenKey, + ImagesScreenKey, + NetworksScreenKey, +} type uiKeyInfo struct { Key tcell.Key @@ -175,7 +174,7 @@ func (key *uiKeyInfo) Description() string { return key.KeyDesc } -// ParseKeyEventKey parsed and changes key events key and rune base on keyname +// ParseKeyEventKey parsed and changes key events key and rune base on keyname. func ParseKeyEventKey(event *tcell.EventKey) *tcell.EventKey { log.Debug().Msgf("utils: parse key event (%v) key=%v name=%v", event, event.Key(), event.Name()) @@ -186,7 +185,7 @@ func ParseKeyEventKey(event *tcell.EventKey) *tcell.EventKey { return tcell.NewEventKey(MoveDownKey.Key, MoveDownKey.KeyRune, tcell.ModNone) } - switch event.Key() { + switch event.Key() { //nolint:exhaustive case ArrowLeftKey.Key: return tcell.NewEventKey(PreviousScreenKey.Key, PreviousScreenKey.KeyRune, tcell.ModNone) case ArrowRightKey.Key: diff --git a/ui/utils/prgbar.go b/ui/utils/prgbar.go index ca34c8b33..f0c5c6468 100644 --- a/ui/utils/prgbar.go +++ b/ui/utils/prgbar.go @@ -12,33 +12,37 @@ const ( prgCrit = 17 ) -// ProgressUsageString return progressbar string (bars + usage percentage) +// ProgressUsageString return progressbar string (bars + usage percentage). func ProgressUsageString(percentage float64) string { progressCell := "" - value := int(int(percentage) * (prgWidth) / 100) + value := int(percentage) * (prgWidth) / 100 //nolint:gomnd + for index := 0; index < prgWidth; index++ { if index < value { - progressCell = progressCell + getBarColor(index) - + progressCell += getBarColor(index) } else { - progressCell = progressCell + style.ProgressBarCell + progressCell += style.ProgressBarCell } } + return progressCell + fmt.Sprintf("%6.2f%%", percentage) } func getBarColor(value int) string { - barCell := "" barColor := "" - if value < prgWarn { + switch { + case value < prgWarn: barColor = style.GetColorName(style.PrgBarOKColor) - } else if value < prgCrit { + case value < prgCrit: barColor = style.GetColorName(style.PrgBarWarnColor) - } else { + default: barColor = style.GetColorName(style.PrgBarCritColor) } - barCell = fmt.Sprintf("[%s::]%s[%s::]", barColor, style.ProgressBarCell, style.GetColorName(style.PrgBarEmptyColor)) + + barCell = fmt.Sprintf("[%s::]%s[%s::]", + barColor, style.ProgressBarCell, style.GetColorName(style.PrgBarEmptyColor)) + return barCell } diff --git a/ui/utils/ui_dialog.go b/ui/utils/ui_dialog.go index 80b9afd92..a36995303 100644 --- a/ui/utils/ui_dialog.go +++ b/ui/utils/ui_dialog.go @@ -2,7 +2,7 @@ package utils import "github.com/rivo/tview" -type UiDialog interface { +type UIDialog interface { tview.Primitive IsDisplay() bool Hide() diff --git a/ui/utils/utils.go b/ui/utils/utils.go index e10036c9e..29c1b08f1 100644 --- a/ui/utils/utils.go +++ b/ui/utils/utils.go @@ -12,14 +12,19 @@ import ( ) const ( - // IDLength max ID length to display + // IDLength max ID length to display. IDLength = 12 - // RefreshInterval application refresh interval + // RefreshInterval application refresh interval. RefreshInterval = 1000 * time.Millisecond idLimit = 12 ) -// GetIDWithLimit return ID string with limited string characters +var ( + ErrURLMissingScheme = errors.New("url missing scheme") + ErrInvalidFilename = errors.New("invalid filename (should not contain ':')") +) + +// GetIDWithLimit return ID string with limited string characters. func GetIDWithLimit(id string) string { if len(id) > 0 { if len(id) >= idLimit { @@ -34,35 +39,40 @@ func GetIDWithLimit(id string) string { func AlignStringListWidth(list []string) ([]string, int) { var ( max = 0 - alignedList []string + alignedList = make([]string, 0) ) + for _, item := range list { if len(item) > max { max = len(item) } } + for _, item := range list { if len(item) < max { need := max - len(item) for i := 0; i < need; i++ { - item = item + " " + item += " " } } + alignedList = append(alignedList, item) } + return alignedList, max } -// EmptyBoxSpace returns simple Box without border with bgColor as background +// EmptyBoxSpace returns simple Box without border with bgColor as background. func EmptyBoxSpace(bgColor tcell.Color) *tview.Box { box := tview.NewBox() box.SetBackgroundColor(bgColor) box.SetBorder(false) + return box } // ResolveHomeDir converts a path referencing the home directory via "~" -// to an absolute path +// to an absolute path. func ResolveHomeDir(path string) (string, error) { // check if the path references the home dir to avoid work // don't use strings.HasPrefix(path, "~") as this doesn't match "~" alone @@ -85,22 +95,25 @@ func ResolveHomeDir(path string) (string, error) { // Following codes are from https://github.com/containers/podman/blob/main/cmd/podman/parse/net.go // ValidateFileName returns an error if filename contains ":" -// as it is currently not supported +// as it is currently not supported. func ValidateFileName(filename string) error { if strings.Contains(filename, ":") { - return fmt.Errorf("invalid filename (should not contain ':') %q", filename) + return fmt.Errorf("%w %q", ErrInvalidFilename, filename) } + return nil } -// ValidURL checks a string urlStr is a url or not +// ValidURL checks a string urlStr is a url or not. func ValidURL(urlStr string) error { url, err := url.ParseRequestURI(urlStr) if err != nil { return errors.Wrapf(err, "invalid url %q", urlStr) } + if url.Scheme == "" { - return fmt.Errorf("invalid url %q: missing scheme", urlStr) + return fmt.Errorf("%w %q", ErrURLMissingScheme, urlStr) } + return nil } diff --git a/ui/volumes/volumes.go b/ui/volumes/volumes.go index c0b7150a3..e683e58f4 100644 --- a/ui/volumes/volumes.go +++ b/ui/volumes/volumes.go @@ -169,8 +169,8 @@ func (vols *Volumes) HideAllDialogs() { } } -func (vols *Volumes) getInnerDialogs() []utils.UiDialog { - dialogs := []utils.UiDialog{ +func (vols *Volumes) getInnerDialogs() []utils.UIDialog { + dialogs := []utils.UIDialog{ vols.errorDialog, vols.progressDialog, vols.confirmDialog,