feat: refactor code

This commit is contained in:
Urko 2023-03-07 22:57:30 +01:00
parent e626e79882
commit 7b06c1b782
3 changed files with 15 additions and 6 deletions

View File

@ -1,4 +1,4 @@
package main
package switcher
import (
"fmt"
@ -25,14 +25,14 @@ var (
wlanEnableID = "#wlanEnable"
)
func newSwitcher(remoteControlBrowserUrl string, config *cfg.Config) *switcher {
func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) *switcher {
return &switcher{
remoteControlBrowserUrl: remoteControlBrowserUrl,
config: config,
}
}
func (s *switcher) switchOff() error {
func (s *switcher) SwitchWIFI() error {
browser := rod.New().
ControlURL(s.remoteControlBrowserUrl).
MustConnect().

10
main.go
View File

@ -8,11 +8,15 @@ import (
"time"
"gitea.urkob.com/urko/go-wifi-switcher/cfg"
"gitea.urkob.com/urko/go-wifi-switcher/internal/switcher"
pkgswitcher "gitea.urkob.com/urko/go-wifi-switcher/pkg/switcher"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/leakless"
)
var switcherIface pkgswitcher.SwitcherIface
func main() {
envFile := ""
if os.Getenv("ENV") != "prod" {
@ -53,9 +57,9 @@ func main() {
remoteControlBrowserURL = launcher.MustResolveURL(<-parser.URL)
}
sw := newSwitcher(remoteControlBrowserURL, config)
if err := sw.switchOff(); err != nil {
err := retry(5, time.Second*5, sw.switchOff)
sw := switcher.NewSwitcher(remoteControlBrowserURL, config)
if err := sw.SwitchWIFI(); err != nil {
err := retry(5, time.Second*5, sw.SwitchWIFI)
if err != nil {
log.Fatalln("couldn't retry:", err)
}

5
pkg/switcher/switcher.go Normal file
View File

@ -0,0 +1,5 @@
package switcher
type SwitcherIface interface {
SwitchWIFI() error
}