Skip to content

Commit

Permalink
Merge pull request #383 from navidys/golangci_lint_p1
Browse files Browse the repository at this point in the history
running golangci-lint on app package
  • Loading branch information
navidys authored Nov 25, 2023
2 parents 5a39503 + c5f1711 commit c564c23
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 49 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ run:
skip-files:
- ".*_test.go"
skip-dirs:
- app
- ui/containers
- ui/dialogs
- ui/images
Expand Down
35 changes: 27 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/rs/zerolog/log"
)

// App represents main application struct
// App represents main application struct.
type App struct {
*tview.Application
infoBar *infobar.InfoBar
Expand All @@ -40,7 +40,7 @@ type App struct {
config *config.Config
}

// NewApp returns new app
// NewApp returns new app.
func NewApp(name string, version string) *App {
log.Debug().Msg("app: new application")

Expand All @@ -49,10 +49,11 @@ func NewApp(name string, version string) *App {
Application: tview.NewApplication(),
pages: tview.NewPages(),
needInitUI: false,
fastRefreshChan: make(chan bool, 10),
fastRefreshChan: make(chan bool, 10), //nolint:gomnd
}

var err error

app.config, err = config.NewConfig()
if err != nil {
log.Fatal().Msgf("%v", err)
Expand All @@ -68,12 +69,15 @@ func NewApp(name string, version string) *App {
app.images = images.NewImages()
app.networks = networks.NewNetworks()
app.system = system.NewSystem()

app.system.SetConnectionListFunc(app.config.ServicesConnections)
app.system.SetConnectionSetDefaultFunc(func(name string) error {
err := app.config.SetDefaultService(name)
app.system.UpdateConnectionsData()

return err
})

app.system.SetConnectionConnectFunc(app.health.Connect)
app.system.SetConnectionDisconnectFunc(app.health.Disconnect)
app.system.SetConnectionAddFunc(app.config.Add)
Expand All @@ -82,15 +86,15 @@ func NewApp(name string, version string) *App {
app.help = help.NewHelp(name, version)

// set refresh channel for container page
// its required for container exec dialog
// its required for container exec dialog.
app.containers.SetFastRefreshChannel(app.fastRefreshChan)

// set refresh channel for image page
// its required for image build dialog
// its required for image build dialog.
app.images.SetFastRefreshChannel(app.fastRefreshChan)

// menu items
var menuItems = [][]string{
menuItems := [][]string{
{utils.HelpScreenKey.Label(), app.help.GetTitle()},
{utils.SystemScreenKey.Label(), app.system.GetTitle()},
{utils.PodsScreenKey.Label(), app.pods.GetTitle()},
Expand All @@ -99,7 +103,9 @@ func NewApp(name string, version string) *App {
{utils.ImagesScreenKey.Label(), app.images.GetTitle()},
{utils.NetworksScreenKey.Label(), app.networks.GetTitle()},
}

app.menu = newMenu(menuItems)

app.pages.AddPage(app.help.GetTitle(), app.help, true, false)
app.pages.AddPage(app.system.GetTitle(), app.system, true, false)
app.pages.AddPage(app.pods.GetTitle(), app.pods, true, false)
Expand All @@ -112,7 +118,7 @@ func NewApp(name string, version string) *App {
}

// Run starts the application loop.
func (app *App) Run() error {
func (app *App) Run() error { //nolint:cyclop
log.Info().Msg("app: run")

flex := tview.NewFlex().
Expand All @@ -134,6 +140,7 @@ func (app *App) Run() error {
app.Stop()
os.Exit(0)
}

if !app.frontScreenHasActiveDialog() {
event = utils.ParseKeyEventKey(event)

Expand All @@ -142,49 +149,58 @@ func (app *App) Run() error {
case utils.NextScreenKey.Rune():
// next screen
app.switchToNextScreen()

return nil

case utils.PreviousScreenKey.Rune():
// previous screen
app.switchToPreviousScreen()

return nil
}

// normal page key switch
switch event.Key() {
switch event.Key() { //nolint:exhaustive
case utils.HelpScreenKey.EventKey():
// help page
app.switchToScreen(app.help.GetTitle())

return nil

case utils.SystemScreenKey.EventKey():
// system page
app.switchToScreen(app.system.GetTitle())

return nil

case utils.PodsScreenKey.EventKey():
// pods page
app.switchToScreen(app.pods.GetTitle())

return nil

case utils.ContainersScreenKey.EventKey():
// containers page
app.switchToScreen(app.containers.GetTitle())

return nil

case utils.VolumesScreenKey.EventKey():
// volumes page
app.switchToScreen(app.volumes.GetTitle())

return nil

case utils.ImagesScreenKey.EventKey():
// images page
app.switchToScreen(app.images.GetTitle())

return nil

case utils.NetworksScreenKey.EventKey():
// networks page
app.switchToScreen(app.networks.GetTitle())

return nil
}
}
Expand All @@ -197,8 +213,10 @@ func (app *App) Run() error {
return nil
}
}

return event
})

app.currentPage = app.system.GetTitle()
app.pages.SwitchToPage(app.system.GetTitle())

Expand All @@ -211,5 +229,6 @@ func (app *App) Run() error {
if err := app.SetRoot(flex, true).SetFocus(app.system).EnableMouse(false).Run(); err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func (app *App) initUI() {
app.volumes.UpdateData()
app.initInfoBar()
}

app.system.UpdateConnectionsData()
}

Expand All @@ -28,6 +29,7 @@ func (app *App) initInfoBar() {
app.infoBar.UpdateBasicInfo(hostname, kernel, ostype)
app.infoBar.UpdateSystemUsageInfo(memUsage, swapUsage)
app.infoBar.UpdatePodmanInfo(apiVer, runtime, conmonVer, buildahVer)

return
}

Expand Down
16 changes: 11 additions & 5 deletions app/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@ import (
"strings"

"github.com/containers/podman-tui/ui/style"

"github.com/rivo/tview"
)

func newMenu(menuItems [][]string) *tview.TextView {

menu := tview.NewTextView().
SetDynamicColors(true).
SetWrap(true).
SetTextAlign(tview.AlignCenter)

menu.SetBackgroundColor(style.BgColor)

var menuList []string

for i := 0; i < len(menuItems); i++ {
key, item := genMenuItem(menuItems[i])
if i == len(menuItems)-1 {
item = item + " "
item += " "
}

menuList = append(menuList, key+item)
}

fmt.Fprintf(menu, "%s", strings.Join(menuList, " "))

return menu
}

func genMenuItem(items []string) (string, string) {

key := fmt.Sprintf("[%s::b] <%s>[-:-:-]", style.GetColorHex(style.PageHeaderFgColor), items[0])
desc := fmt.Sprintf("[%s:%s:b] %s [-:-:-]", style.GetColorHex(style.PageHeaderFgColor), style.GetColorHex(style.MenuBgColor), strings.ToUpper(items[1]))
desc := fmt.Sprintf("[%s:%s:b] %s [-:-:-]",
style.GetColorHex(style.PageHeaderFgColor),
style.GetColorHex(style.MenuBgColor),
strings.ToUpper(items[1]))

return key, desc
}
76 changes: 44 additions & 32 deletions app/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,42 @@ import (

func (app *App) refresh() {
log.Debug().Msgf("app: starting refresh loop (interval=%v)", utils.RefreshInterval)

tick := time.NewTicker(utils.RefreshInterval)

for {
select {
case <-tick.C:
connStatus, connMsg := app.health.ConnStatus()
switch connStatus {
case registry.ConnectionStatusConnected:
app.refreshConnOK()
case registry.ConnectionStatusConnectionError:
app.refreshNotConnOK()
if registry.ConnectionIsSet() {
name := registry.ConnectionName()
app.system.ConnectionProgressDisplay(true)
app.system.SetConnectionProgressDestName(name)
app.system.SetConnectionProgressMessage(connMsg)
}
case registry.ConnectionStatusDisconnected:
app.refreshNotConnOK()
if registry.ConnectionIsSet() {
name := registry.ConnectionName()
app.system.ConnectionProgressDisplay(true)
app.system.SetConnectionProgressDestName(name)
}
<-tick.C

connStatus, connMsg := app.health.ConnStatus()

switch connStatus {
case registry.ConnectionStatusConnected:
app.refreshConnOK()
case registry.ConnectionStatusConnectionError:
app.refreshNotConnOK()

if registry.ConnectionIsSet() {
name := registry.ConnectionName()

app.system.ConnectionProgressDisplay(true)
app.system.SetConnectionProgressDestName(name)
app.system.SetConnectionProgressMessage(connMsg)
}

case registry.ConnectionStatusDisconnected:
app.refreshNotConnOK()

if registry.ConnectionIsSet() {
name := registry.ConnectionName()

app.system.ConnectionProgressDisplay(true)
app.system.SetConnectionProgressDestName(name)
}
app.initInfoBar()
app.infoBar.UpdateConnStatus(connStatus)
app.Application.Draw()
}

app.initInfoBar()
app.infoBar.UpdateConnStatus(connStatus)
app.Application.Draw()
}
}

Expand All @@ -46,10 +54,13 @@ func (app *App) refreshConnOK() {
// init ui after reconnection
app.initUI()
app.system.ConnectionProgressDisplay(false)

app.needInitUI = false

app.pages.SwitchToPage(app.currentPage)
app.setPageFocus(app.currentPage)
}

app.flushEvents()
}

Expand All @@ -59,33 +70,34 @@ func (app *App) refreshNotConnOK() {
app.clearViewsData()
app.switchToScreen(app.system.GetTitle())
}

app.system.UpdateConnectionsData()

app.needInitUI = true
}

func (app *App) flushEvents() {
// update events
eventTypes := app.health.GetEvents()

for _, evt := range eventTypes {
app.updatePageDataFromEvent(evt)
}

if app.health.HasNewEvent() {
app.system.SetEventMessage(app.health.GetEventMessages())
}
}

// fastRefresh method will refresh the screen as soon as it receives
// the refresh signal. Its required for some feature e.g. container exec
// the refresh signal. Its required for some feature e.g. container exec.
func (app *App) fastRefresh() {
log.Debug().Msg("app: starting fast refresh loop")

for {
select {
case refresh := <-app.fastRefreshChan:
{
if refresh {
app.Application.Draw()
}
}
refresh := <-app.fastRefreshChan
if refresh {
app.Application.Draw()
}
}
}
Loading

0 comments on commit c564c23

Please sign in to comment.