go-wifi-switcher/main.go

68 lines
1.5 KiB
Go

package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"gitea.urkob.com/urko/go-wifi-switcher/cfg"
"gitea.urkob.com/urko/go-wifi-switcher/internal/providers"
"gitea.urkob.com/urko/go-wifi-switcher/internal/switcher"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/leakless"
)
func main() {
envFile := ""
if os.Getenv("ENV") != "prod" {
envFile = "./.env"
}
config := cfg.NewConfig(envFile)
log.SetFlags(log.Lmicroseconds)
config.LogFile = false
if config.LogFile {
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
}
var lc *launcher.Launcher
var remoteControlBrowserURL string
log.Println("config", config)
if config.Bin != "" {
lc = launcher.New().Bin(config.Bin)
remoteControlBrowserURL = lc.MustLaunch()
log.Println("u = lc.MustLaunch()", remoteControlBrowserURL)
} else {
lc = launcher.New()
path := launcher.NewBrowser().MustGet()
args := lc.FormatArgs()
cmd := leakless.New().Command(path, args...)
parser := launcher.NewURLParser()
cmd.Stderr = parser
utils.E(cmd.Start())
remoteControlBrowserURL = launcher.MustResolveURL(<-parser.URL)
}
sw := switcher.NewSwitcher(remoteControlBrowserURL, config)
prv := providers.NewNucom(sw.Page)
err := sw.SwitchWIFI(prv)
if err != nil {
panic(err)
}
log.Println("task completed")
}