Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running golangci-lint on ui/utils package #384

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ run:
- ui/networks
- ui/pods
- ui/system
- ui/utils
linters:
enable-all: true
disable:
Expand Down
6 changes: 4 additions & 2 deletions ui/utils/eventkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions ui/utils/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion ui/utils/home_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
65 changes: 32 additions & 33 deletions ui/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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())

Expand All @@ -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:
Expand Down
24 changes: 14 additions & 10 deletions ui/utils/prgbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion ui/utils/ui_dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package utils

import "github.com/rivo/tview"

type UiDialog interface {
type UIDialog interface {
tview.Primitive
IsDisplay() bool
Hide()
Expand Down
35 changes: 24 additions & 11 deletions ui/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions ui/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading