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

Refactor sessions to use reflection for easier extensibility #7

Merged
merged 2 commits into from
Jul 23, 2024
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ The config supports two special types: templates and expressions. In this sectio

Templates provide a simple way to describe what value should be placed in a field. A good example is how it's used for the default path.

A template is a string with one or more `{}` placeholders inside. For example: `NetBox/{tenant}/{site}`.
A template is a string with one or more `{}` placeholders inside. For example: `NetBox/{tenant_name}/{site_name}`.
If the tenant is "Example" and the site is "Test", the template would evaluate to `"NetBox/Example/Test`.

Templates have access to the following variables:
```
type: Either device or virtual_machine
session_type: Either device or virtual_machine
credential: The default session credential name
path_template: The default path template
device_name_template: The default device name template
Expand All @@ -56,7 +56,7 @@ Here are a few sample expressions to get you started:
{{ site_group == 'adm' }}

# Returns the value of the tag "connection_protocol" if found, otherwise "SSH"
{{ findTag(device.Tags, 'connection_protocol') ?? 'SSH' }}
{{ FindTag(device.Tags, 'connection_protocol') ?? 'SSH' }}

# Returns true if the device name ends with example.com
{{ device_name endsWith '.example.com' }}
Expand All @@ -68,6 +68,10 @@ device: The device object (go struct, most fields are CamelCase, ex: device.Tags
site: The site object (go struct, most fields are CamelCase, ex: site.Slug)
```

Expressions have access to all expr functions and the following:
```
FindTag(<tags>, <tag_name)
```

### Debug
It's possible to debug expressions and templates, by enabling debug in the config file and examining the log file. The log file can be opened by clicking the icon and selecting "Open Log", when debug is enabled all variables will be output together with templates, and result.
Expand Down Expand Up @@ -97,11 +101,11 @@ session:
# Global Session Options
session_options:
# Allows you to override the connection protocol; supports templates and expressions
connection_protocol: "{{ findTag(device.Tags, 'connection_protocol') ?? 'SSH' }}"
connection_protocol: "{{ FindTag(device.Tags, 'connection_protocol') ?? 'SSH' }}"
# Set default credentials; they should be defined in SecureCRT beforehand under "Preferences -> General -> Credentials"
credential: <username>
# Set a firewall; supports templates and expressions
firewall: "{{ findTag(device.Tags, 'connection_firewall') ?? null }}"
firewall: "{{ FindTag(device.Tags, 'connection_firewall') ?? null }}"

# Overrides based on conditions
# target can be one of: path, device_name, description, connection_protocol, credential, firewall
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ go 1.21.1

require (
fyne.io/systray v1.10.0
github.com/expr-lang/expr v1.16.9
github.com/netbox-community/go-netbox/v3 v3.4.5
github.com/sqweek/dialog v0.0.0-20240226140203-065105509627
golang.org/x/sync v0.7.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -14,7 +16,6 @@ require (
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/expr-lang/expr v1.16.9 // indirect
github.com/go-openapi/analysis v0.21.2 // indirect
github.com/go-openapi/errors v0.20.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
10 changes: 7 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type ConfigNameOverwrite struct {
}

type ConfigSessionOptions struct {
ConnectionProtocol string `yaml:"connection_protocol"`
Credential *string `yaml:"credential"`
Firewall *string `yaml:"firewall"`
ConnectionProtocol string `yaml:"connection_protocol"`
Credential string `yaml:"credential"`
Firewall string `yaml:"firewall"`
}

type ConfigSession struct {
Expand Down Expand Up @@ -88,6 +88,10 @@ func (c *Config) SetDefaultsAndValidate() error {
c.Session.SessionOptions.ConnectionProtocol = "SSH"
}

if c.Session.SessionOptions.Firewall == "" {
c.Session.SessionOptions.Firewall = "None"
}

if c.Session.DeviceName == "" {
c.Session.DeviceName = "{device_name}"
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 31 additions & 28 deletions internal/tray/systray.go → internal/gui/systray.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tray
package gui

import (
"log/slog"
"time"

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

type SysTray struct {
Expand All @@ -22,8 +22,9 @@ type SysTray struct {

func New(cfg *config.Config) *SysTray {
return &SysTray{
ClickedCh: make(chan string),
cfg: cfg,
ClickedCh: make(chan string),
cfg: cfg,
animationTicker: time.NewTicker(time.Second / 5),
}
}

Expand All @@ -49,6 +50,9 @@ func (s *SysTray) onStartup() {

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

s.StopAnimateIcon()
go s.setupIconSpinner()

s.SetStatus(true)
s.SetStatusMessage("Status: Not synced yet")
s.togglePeriodicSync()
Expand Down Expand Up @@ -85,6 +89,28 @@ func (s *SysTray) togglePeriodicSync() {
}
}

func (s *SysTray) setupIconSpinner() {
imageCount := 6
currentFrame := 0
icons := [][]byte{
assets.AnimateIcon1,
assets.AnimateIcon2,
assets.AnimateIcon3,
assets.AnimateIcon4,
assets.AnimateIcon5,
assets.AnimateIcon6,
}

for range s.animationTicker.C {
if currentFrame == imageCount-1 {
currentFrame = 0
}

systray.SetTemplateIcon(icons[currentFrame], icons[currentFrame])
currentFrame = currentFrame + 1
}
}

func (s *SysTray) Run() {
systray.Run(s.onStartup, s.onExit)
}
Expand All @@ -99,31 +125,8 @@ func (s *SysTray) Quit() {

func (s *SysTray) StartAnimateIcon() {
if s.animationTicker != nil {
return
s.animationTicker.Reset(time.Second / 5)
}

imageCount := 6
currentFrame := 0
s.animationTicker = time.NewTicker(time.Second / 5)
icons := [][]byte{
assets.AnimateIcon1,
assets.AnimateIcon2,
assets.AnimateIcon3,
assets.AnimateIcon4,
assets.AnimateIcon5,
assets.AnimateIcon6,
}

go func() {
for range s.animationTicker.C {
if currentFrame == imageCount-1 {
currentFrame = 0
}

systray.SetTemplateIcon(icons[currentFrame], icons[currentFrame])
currentFrame = currentFrame + 1
}
}()
}

func (s *SysTray) StopAnimateIcon() {
Expand Down
Loading
Loading