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 ( import (
"fmt" "fmt"
@ -25,14 +25,14 @@ var (
wlanEnableID = "#wlanEnable" wlanEnableID = "#wlanEnable"
) )
func newSwitcher(remoteControlBrowserUrl string, config *cfg.Config) *switcher { func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) *switcher {
return &switcher{ return &switcher{
remoteControlBrowserUrl: remoteControlBrowserUrl, remoteControlBrowserUrl: remoteControlBrowserUrl,
config: config, config: config,
} }
} }
func (s *switcher) switchOff() error { func (s *switcher) SwitchWIFI() error {
browser := rod.New(). browser := rod.New().
ControlURL(s.remoteControlBrowserUrl). ControlURL(s.remoteControlBrowserUrl).
MustConnect(). MustConnect().

10
main.go
View File

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