Tease apart wifi config into dedicated module and use module in existing wifi config tool

This commit is contained in:
KemoNine 2020-09-01 19:05:09 -04:00
parent 83743a9f90
commit 48d757a6b6
3 changed files with 60 additions and 24 deletions

View File

@ -6,6 +6,8 @@ import (
"os"
"os/exec"
"strings"
"git.kemonine.info/PiFrame/wifi"
)
// Various commands that we need for this process
@ -13,7 +15,6 @@ const (
CMD_BLKID = "/usr/sbin/blkid"
CMD_FINDMNT = "/usr/bin/findmnt"
CMD_MOUNT = "/usr/bin/mount"
CMD_NMCLI = "/usr/bin/nmcli"
CMD_UMOUNT = "/usr/bin/umount"
)
@ -90,29 +91,11 @@ func main() {
essid := config[0]
password := config[1]
log.Printf("Using config: %s / %s for WiFi\n", essid, password)
// Cleanup existing WiFi connections
nmcliOut, err := exec.Command(CMD_NMCLI, "-t", "connection", "show").Output()
if err != nil {
log.Fatalf("Error running %s : %s", CMD_NMCLI, err)
}
connections := strings.Split(strings.Trim(string(nmcliOut), "\n"), "\n")
for _, connection := range connections {
details := strings.Split(connection, ":")
if details[2] != "802-11-wireless" {
continue
}
log.Printf("Cleaning up WiFi connection %s", details[0])
err := exec.Command(CMD_NMCLI, "connection", "del", details[1]).Run()
if err != nil {
log.Fatalf("Error running %s : %s", CMD_NMCLI, err)
}
}
// Create new WiFi connection with network manager
log.Printf("Connecting to %s with password %s\n", essid, password)
err = exec.Command(CMD_NMCLI, "d", "wifi", "connect", essid, "password", password).Run()
if err != nil {
log.Fatalf("Error running %s : %s", CMD_NMCLI, err)
}
// Cleanup old wifi configs and apply new one
nmWifi := wifi.New(essid, password)
nmWifi.ApplyConfig()
// Unmount the filesystem and continue
err = exec.Command(CMD_UMOUNT, MOUNTPOINT).Run()
if err != nil {

3
wifi/README.md Normal file
View File

@ -0,0 +1,3 @@
# wifi
A very simple construct for calling ```nmcli``` in a way that'll clear existing WiFi configs and add a new one.

50
wifi/wifi.go Normal file
View File

@ -0,0 +1,50 @@
package wifi
import (
"log"
"os/exec"
"strings"
)
const (
CMD_NMCLI = "/usr/bin/nmcli"
)
type wifi struct {
essid string
password string
}
func New(essid string, password string) *wifi {
w := wifi{
essid: essid,
password: password,
}
return &w
}
func (w *wifi) ApplyConfig() {
// Cleanup existing WiFi connections
nmcliOut, err := exec.Command(CMD_NMCLI, "-t", "connection", "show").Output()
if err != nil {
log.Fatalf("Error running %s : %s", CMD_NMCLI, err)
}
connections := strings.Split(strings.Trim(string(nmcliOut), "\n"), "\n")
for _, connection := range connections {
details := strings.Split(connection, ":")
if details[2] != "802-11-wireless" {
continue
}
log.Printf("Cleaning up WiFi connection %s", details[0])
err := exec.Command(CMD_NMCLI, "connection", "del", details[1]).Run()
if err != nil {
log.Fatalf("Error running %s : %s", CMD_NMCLI, err)
}
}
// Create new WiFi connection with network manager
log.Printf("Connecting to %s with password %s\n", w.essid, w.password)
err = exec.Command(CMD_NMCLI, "d", "wifi", "connect", w.essid, "password", w.password).Run()
if err != nil {
log.Fatalf("Error running %s : %s", CMD_NMCLI, err)
}
}