From 7b06c1b7821d766f375f3fedb87ad981ae92dac7 Mon Sep 17 00:00:00 2001 From: Urko Date: Tue, 7 Mar 2023 22:57:30 +0100 Subject: [PATCH] feat: refactor code --- switcher.go => internal/switcher/switcher.go | 6 +++--- main.go | 10 +++++++--- pkg/switcher/switcher.go | 5 +++++ 3 files changed, 15 insertions(+), 6 deletions(-) rename switcher.go => internal/switcher/switcher.go (95%) create mode 100644 pkg/switcher/switcher.go diff --git a/switcher.go b/internal/switcher/switcher.go similarity index 95% rename from switcher.go rename to internal/switcher/switcher.go index f67b4b8..71feec5 100644 --- a/switcher.go +++ b/internal/switcher/switcher.go @@ -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(). diff --git a/main.go b/main.go index a4aaeb8..12324f4 100644 --- a/main.go +++ b/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) } diff --git a/pkg/switcher/switcher.go b/pkg/switcher/switcher.go new file mode 100644 index 0000000..882c15e --- /dev/null +++ b/pkg/switcher/switcher.go @@ -0,0 +1,5 @@ +package switcher + +type SwitcherIface interface { + SwitchWIFI() error +}