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