feat: refactor code
This commit is contained in:
parent
e626e79882
commit
7b06c1b782
|
@ -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
10
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package switcher
|
||||||
|
|
||||||
|
type SwitcherIface interface {
|
||||||
|
SwitchWIFI() error
|
||||||
|
}
|
Loading…
Reference in New Issue