Skip to content

Commit

Permalink
fix: better error handling on invalid credential name
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Falk Nielsen committed May 1, 2024
1 parent 7da2d0a commit de05423
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
3 changes: 1 addition & 2 deletions internal/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ func New(cfg *config.Config, nb *netbox.NetBox, scrt *securecrt.SecureCRT, systr
periodicTicker: time.NewTicker(time.Minute * time.Duration(*cfg.PeriodicSyncInterval)),
}

go inv.setupPeriodicSync()
return &inv
}

func (i *InventorySync) setupPeriodicSync() {
func (i *InventorySync) SetupPeriodicSync() {
for range i.periodicTicker.C {
if i.cfg.EnablePeriodicSync {
err := i.RunSync()
Expand Down
21 changes: 17 additions & 4 deletions internal/tray/systray.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package tray

import (
"fmt"
"time"

"fyne.io/systray"
"github.com/jysk-network/netbox-securecrt-inventory/internal/config"
"github.com/jysk-network/netbox-securecrt-inventory/internal/tray/assets"
)

type SysTrayStatus struct {
Message string
Status bool
MenusDisabled bool
}

type SysTray struct {
mStatus *systray.MenuItem
mSyncNow *systray.MenuItem
Expand All @@ -16,7 +23,7 @@ type SysTray struct {
cfg *config.Config
animationTicker *time.Ticker
ClickedCh chan string
closeCh chan struct{}
startupStatus SysTrayStatus
}

func New(cfg *config.Config) *SysTray {
Expand Down Expand Up @@ -46,8 +53,13 @@ func (s *SysTray) onStartup() {

s.mQuit = systray.AddMenuItem("Quit", "Quit the whole app")

s.SetStatus(true)
s.SetStatusMessage("Status: Started")
if s.startupStatus.MenusDisabled {
s.mSyncNow.Disable()
mSettings.Disable()
}

s.SetStatus(s.startupStatus.Status)
s.SetStatusMessage(fmt.Sprintf("Status: %s", s.startupStatus.Message))
s.togglePeriodicSync()
s.handleClicks()
}
Expand Down Expand Up @@ -78,7 +90,8 @@ func (s *SysTray) togglePeriodicSync() {
}
}

func (s *SysTray) Run() {
func (s *SysTray) Run(status SysTrayStatus) {
s.startupStatus = status
systray.Run(s.onStartup, s.onExit)
}

Expand Down
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ func main() {
panic(err)
}

// setup our startup status so we can report errors in the systray
sysTrayStatupStatus := tray.SysTrayStatus{
Status: true,
Message: "Not synced yet",
}

// setup securecrt config builder, and validate it's installed
scrt, err := securecrt.New(cfg.DefaultCredential)
if err != nil {
panic(err)
sysTrayStatupStatus.Message = err.Error()
sysTrayStatupStatus.Status = false
sysTrayStatupStatus.MenusDisabled = true
}

// setup the systray, and all menu items
Expand All @@ -32,8 +40,10 @@ func main() {
nb := netbox.New(cfg.NetboxUrl, cfg.NetboxToken)
invClient := inventory.New(cfg, nb, scrt, systray)

// handle periodic sync if enabled
go invClient.SetupPeriodicSync()

// handle click events
// TODO: should this be move, or should the periodic sync func be moved out here?
go func() {
for menuItem := range systray.ClickedCh {
if menuItem == "sync" {
Expand All @@ -53,5 +63,5 @@ func main() {
}()

// show the systray in a blocking way
systray.Run()
systray.Run(sysTrayStatupStatus)
}
2 changes: 1 addition & 1 deletion pkg/securecrt/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import "errors"
var (
ErrFailedToExpandHomeDir = errors.New("unable to expand user home dir")
ErrFailedToLoadConfig = errors.New("failed to get securecrt config")
ErrFailedToLoadCredentials = errors.New("failed to load the credentials")
ErrFailedToLoadCredentials = errors.New("failed to load default credentials")
ErrFailedToCreateSession = errors.New("failed to create session")
)
2 changes: 1 addition & 1 deletion pkg/securecrt/securecrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (scrt *SecureCRT) BuildSessionData(ip, protocol, site, address, machine_typ
data.WriteString(scrt.credentialValue)
data.WriteString(fmt.Sprintf("\nS:\"Hostname\"=%s", ip))
data.WriteString(fmt.Sprintf("\nS:\"Protocol Name\"=%s", protocol))
data.WriteString(fmt.Sprintf("\nZ:\"Description\"=00000003")) // number of lines to display
data.WriteString("\nZ:\"Description\"=00000003") // number of lines to display
data.WriteString(fmt.Sprintf("\n Site: %s", site))
data.WriteString(fmt.Sprintf("\n Type: %s", machine_type))
data.WriteString(fmt.Sprintf("\n Adresse: %s", address))
Expand Down

0 comments on commit de05423

Please sign in to comment.