chore: rework reading of pis
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -25,3 +25,4 @@ go.work.sum
|
||||
# env file
|
||||
.env
|
||||
|
||||
*.yaml
|
||||
2
go.mod
2
go.mod
@@ -5,6 +5,7 @@ go 1.25.5
|
||||
require (
|
||||
fyne.io/fyne/v2 v2.7.2
|
||||
github.com/pkg/sftp v1.13.10
|
||||
github.com/stretchr/testify v1.11.1
|
||||
golang.org/x/crypto v0.46.0
|
||||
)
|
||||
|
||||
@@ -35,7 +36,6 @@ require (
|
||||
github.com/rymdport/portal v0.4.2 // indirect
|
||||
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect
|
||||
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect
|
||||
github.com/stretchr/testify v1.11.1 // indirect
|
||||
github.com/yuin/goldmark v1.7.16 // indirect
|
||||
golang.org/x/image v0.34.0 // indirect
|
||||
golang.org/x/net v0.48.0 // indirect
|
||||
|
||||
@@ -11,22 +11,22 @@ import (
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func translateRaspiName(targetName string) (string, string, string, string) {
|
||||
for _, raspiObj := range raspiList {
|
||||
func translateRaspiName(targetName string, cfg RaspiConfig) (string, string, string, string) {
|
||||
for _, n := range cfg.Raspis {
|
||||
if targetName == "" {
|
||||
break
|
||||
}
|
||||
if raspiObj[0] == targetName {
|
||||
if n.Name == targetName {
|
||||
// Found!
|
||||
return raspiObj[1], raspiObj[2], raspiObj[3], raspiObj[4]
|
||||
return n.Hostname, n.Port, n.Username, n.Password
|
||||
}
|
||||
}
|
||||
|
||||
return "", "", "", ""
|
||||
}
|
||||
|
||||
func createSSHClient(targetName string) (*ssh.Client, error) {
|
||||
hostname, port, username, password := translateRaspiName(targetName)
|
||||
func createSSHClient(targetName string, cfg RaspiConfig) (*ssh.Client, error) {
|
||||
hostname, port, username, password := translateRaspiName(targetName, cfg)
|
||||
log.Println("Connecting to:", hostname, port)
|
||||
|
||||
config := &ssh.ClientConfig{
|
||||
@@ -42,8 +42,8 @@ func createSSHClient(targetName string) (*ssh.Client, error) {
|
||||
return ssh.Dial("tcp", connAddr, config)
|
||||
}
|
||||
|
||||
func verifyCred(targetName string) bool {
|
||||
sshClient, err := createSSHClient(targetName)
|
||||
func verifyCred(targetName string, cfg RaspiConfig) bool {
|
||||
sshClient, err := createSSHClient(targetName, cfg)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Err occurred %v", err)
|
||||
@@ -55,8 +55,8 @@ func verifyCred(targetName string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func sftpUploadFile(targetName, localPath, remotePath string) bool {
|
||||
sshClient, err := createSSHClient(targetName)
|
||||
func sftpUploadFile(targetName, localPath, remotePath string, cfg RaspiConfig) bool {
|
||||
sshClient, err := createSSHClient(targetName, cfg)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to init the SSH connection %v", err)
|
||||
@@ -94,8 +94,8 @@ func sftpUploadFile(targetName, localPath, remotePath string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func restartShow(targetName string) bool {
|
||||
sshClient, err := createSSHClient(targetName)
|
||||
func restartShow(targetName string, cfg RaspiConfig) bool {
|
||||
sshClient, err := createSSHClient(targetName, cfg)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to init the SSH connection: %v", err)
|
||||
|
||||
10
src/draw.go
10
src/draw.go
@@ -113,7 +113,7 @@ func drawModeRow(targetMode *int) *fyne.Container {
|
||||
return modeCol
|
||||
}
|
||||
|
||||
func drawTargetSection(raspiNames []string, raspiTarget *string, uploadBtn, reloadBtn *widget.Button) *fyne.Container {
|
||||
func drawTargetSection(raspiNames []string, raspiTarget *string, uploadBtn, reloadBtn *widget.Button, cfg RaspiConfig) *fyne.Container {
|
||||
actionText := widget.NewLabel("Select Target")
|
||||
|
||||
var verifyBtn *widget.Button
|
||||
@@ -149,7 +149,7 @@ func drawTargetSection(raspiNames []string, raspiTarget *string, uploadBtn, relo
|
||||
refreshButtons(verifyBtn, uploadBtn, reloadBtn)
|
||||
|
||||
go func() {
|
||||
credOK = verifyCred(*raspiTarget)
|
||||
credOK = verifyCred(*raspiTarget, cfg)
|
||||
|
||||
if credOK {
|
||||
// Must update UI on main thread
|
||||
@@ -231,7 +231,7 @@ func drawFileSelection(localPath *string, targetMode *int, parentWindow *fyne.Wi
|
||||
}
|
||||
|
||||
// targetMode *int
|
||||
func drawFooter(app fyne.App, raspiTarget, localUploadPath *string) (*fyne.Container, *widget.Button, *widget.Button) {
|
||||
func drawFooter(app fyne.App, raspiTarget, localUploadPath *string, cfg RaspiConfig) (*fyne.Container, *widget.Button, *widget.Button) {
|
||||
remotePresPath := "/opt/akartontv/presentation.pptx"
|
||||
//remoteVideoPath := "/opt/akartontv/video.mp4"
|
||||
|
||||
@@ -241,7 +241,7 @@ func drawFooter(app fyne.App, raspiTarget, localUploadPath *string) (*fyne.Conta
|
||||
uploadBtn.Importance = widget.HighImportance
|
||||
refreshButtons(uploadBtn)
|
||||
|
||||
if sftpUploadFile(*raspiTarget, *localUploadPath, remotePresPath) {
|
||||
if sftpUploadFile(*raspiTarget, *localUploadPath, remotePresPath, cfg) {
|
||||
go flashColor(uploadBtn, "green")
|
||||
} else {
|
||||
go flashColor(uploadBtn, "red")
|
||||
@@ -257,7 +257,7 @@ func drawFooter(app fyne.App, raspiTarget, localUploadPath *string) (*fyne.Conta
|
||||
reloadBtn.Importance = widget.HighImportance
|
||||
refreshButtons(uploadBtn)
|
||||
|
||||
if restartShow(*raspiTarget) {
|
||||
if restartShow(*raspiTarget, cfg) {
|
||||
go flashColor(reloadBtn, "green") // flashcolor should handle RunOnMain internally
|
||||
} else {
|
||||
go flashColor(reloadBtn, "red")
|
||||
|
||||
17
src/main.go
17
src/main.go
@@ -28,13 +28,16 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := readConfig()
|
||||
log.Println(cfg)
|
||||
|
||||
app := app.NewWithID("nl.systemec.rpi-charon")
|
||||
app.Settings().SetTheme(theme.DefaultTheme())
|
||||
|
||||
w := app.NewWindow(appName)
|
||||
w.Resize(windowSize)
|
||||
|
||||
raspiNames := getRaspiNames()
|
||||
raspiNames := getRaspiNames(cfg)
|
||||
|
||||
// Define variables and print them out for debug
|
||||
// Presentation = 1
|
||||
@@ -51,9 +54,9 @@ func main() {
|
||||
}
|
||||
|
||||
// Call the draw functions -> ./src/draw.go
|
||||
footerRow, uploadBtn, reloadBtn := drawFooter(app, &raspiTarget, &localUploadPath)
|
||||
footerRow, uploadBtn, reloadBtn := drawFooter(app, &raspiTarget, &localUploadPath, cfg)
|
||||
modeBtnRow := drawModeRow(&targetMode)
|
||||
selectionRow := drawTargetSection(raspiNames, &raspiTarget, uploadBtn, reloadBtn)
|
||||
selectionRow := drawTargetSection(raspiNames, &raspiTarget, uploadBtn, reloadBtn, cfg)
|
||||
fileSelectRow := drawFileSelection(&localUploadPath, &targetMode, &w)
|
||||
|
||||
center := container.NewVBox(
|
||||
@@ -85,13 +88,13 @@ func refreshButtons(givenButtons ...*widget.Button) {
|
||||
}
|
||||
|
||||
// The raspiList is defined in the other go file, must me maintained separately
|
||||
func getRaspiNames() []string {
|
||||
raspiNames := make([]string, 0, len(raspiList))
|
||||
func getRaspiNames(cfg RaspiConfig) []string {
|
||||
raspiNames := make([]string, 0, len(cfg.Raspis))
|
||||
|
||||
// Append the name of the pi to the list we need for the radio button
|
||||
// i is the index field correlated to the name as per defined in rpi-list.go
|
||||
for _, n := range raspiList {
|
||||
raspiNames = append(raspiNames, n[0]) // append only the first element
|
||||
for _, n := range cfg.Raspis {
|
||||
raspiNames = append(raspiNames, n.Name) // append only the first element
|
||||
}
|
||||
|
||||
return raspiNames
|
||||
|
||||
34
src/raspi.go
Normal file
34
src/raspi.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/stretchr/testify/assert/yaml"
|
||||
)
|
||||
|
||||
type RaspberryPi struct {
|
||||
Name string `yaml:"name"`
|
||||
Hostname string `yaml:"hostname"`
|
||||
Port string `yaml:"port"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password,omitempty"` // optional
|
||||
}
|
||||
|
||||
type RaspiConfig struct {
|
||||
Raspis []RaspberryPi `yaml:"raspis"`
|
||||
}
|
||||
|
||||
func readConfig() RaspiConfig {
|
||||
data, err := os.ReadFile("raspis.yaml")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read config YAML file (raspis.yaml): %v", err)
|
||||
}
|
||||
|
||||
var cfg RaspiConfig
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
log.Fatalf("Failed to parse YAML file (raspis.yaml): %v", err)
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package main
|
||||
|
||||
var (
|
||||
// 3 Rows, 3 Columns - 1 Row per Pi
|
||||
// 0 Raspberry Pi Name
|
||||
// 1 Raspberry Pi IP-Adress
|
||||
// 2 Raspberry Pi SSH-port
|
||||
// 3 Raspberry Pi SSH Username
|
||||
// 4 Raspberry Pi SSH Password
|
||||
raspiList [][]string = [][]string{
|
||||
{"Kantine Pi", "172.16.64.228", "22", "systemec", ""},
|
||||
{"Productie Pi", "172.16.64.229", "22", "", ""},
|
||||
{"Sales Pi", "192.168.110.175", "22", "systemec", "Doerak2003!"},
|
||||
}
|
||||
)
|
||||
16
templates/raspis.yaml.template
Normal file
16
templates/raspis.yaml.template
Normal file
@@ -0,0 +1,16 @@
|
||||
raspis:
|
||||
- name: rpi1
|
||||
ip: 192.168.1.10
|
||||
port: 22
|
||||
user: pi
|
||||
password: mypassword1
|
||||
- name: rpi2
|
||||
ip: 192.168.1.11
|
||||
port: 2222
|
||||
user: pi
|
||||
password: mypassword2
|
||||
- name: rpi3
|
||||
ip: 192.168.1.12
|
||||
port: 22
|
||||
user: pi
|
||||
password: mypassword3
|
||||
Reference in New Issue
Block a user