From 6fabc43998c8879079007d138475c68f73ff4c5c Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Tue, 23 Aug 2022 03:16:43 +0100 Subject: [PATCH] return err from NewRemote\ pass Kind.Name to logout update examples/tests to reflect changes --- base.go | 6 +++--- examples/observer/observer.go | 11 ++++++++--- helper_test.go | 8 +++++++- remote.go | 10 ++++------ remote_test.go | 6 +++--- tests/helper_test.go | 9 ++++++++- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/base.go b/base.go index 5797dc0..b29615f 100644 --- a/base.go +++ b/base.go @@ -54,14 +54,14 @@ func login(kindId string) { fmt.Println(err) os.Exit(1) } - fmt.Println("Logged into API") + fmt.Printf("Logged into Voicemeeter %s\n", kindId) for pdirty() || mdirty() { } } // logout logs out of the API, // delayed for 100ms to allow final operation to complete. -func logout() { +func logout(kindId string) { time.Sleep(100 * time.Millisecond) res, _, _ := vmLogout.Call() if res != 0 { @@ -69,7 +69,7 @@ func logout() { fmt.Println(err) os.Exit(1) } - fmt.Println("Logged out of API") + fmt.Printf("Logged out of Voicemeeter %s\n", kindId) } // runVoicemeeter attempts to launch a Voicemeeter GUI of a kind. diff --git a/examples/observer/observer.go b/examples/observer/observer.go index 188e41a..d8357f5 100644 --- a/examples/observer/observer.go +++ b/examples/observer/observer.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "time" "github.com/onyx-and-iris/voicemeeter-api-go" @@ -46,7 +47,13 @@ func (o observer) OnUpdate(subject string) { } func main() { - vm := voicemeeter.NewRemote("potato") + vm, err := voicemeeter.NewRemote("potato") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + defer vm.Logout() + vm.Login() // enable level updates (disabled by default) vm.EventAdd("ldirty") @@ -55,6 +62,4 @@ func main() { o.Register() time.Sleep(30 * time.Second) o.Deregister() - - vm.Logout() } diff --git a/helper_test.go b/helper_test.go index cd38c4e..5b0b470 100644 --- a/helper_test.go +++ b/helper_test.go @@ -1,16 +1,22 @@ package voicemeeter import ( + "fmt" "os" "testing" "time" ) var ( - vm = NewRemote("potato") + vm, err = NewRemote("potato") ) func TestMain(m *testing.M) { + if err != nil { + fmt.Println(err) + os.Exit(1) + } + vm.Login() code := m.Run() vm.Logout() diff --git a/remote.go b/remote.go index 05996b8..8fbc826 100644 --- a/remote.go +++ b/remote.go @@ -2,7 +2,6 @@ package voicemeeter import ( "fmt" - "os" ) // A Remote type represents the API for a kind @@ -36,7 +35,7 @@ func (r *Remote) Login() { // it also terminates the pooler func (r *Remote) Logout() { r.pooler.run = false - logout() + logout(r.Kind.Name) } // Type returns the type of Voicemeeter (basic, banana, potato) @@ -289,12 +288,11 @@ func (potb *potatoBuilder) Build() remoteBuilder { // NewRemote returns a Remote type for a kind // this is the interface entry point -func NewRemote(kindId string) *Remote { +func NewRemote(kindId string) (*Remote, error) { _kind, ok := kindMap[kindId] if !ok { err := fmt.Errorf("unknown Voicemeeter kind '%s'", kindId) - fmt.Println(err) - os.Exit(1) + return nil, err } director := director{} @@ -307,5 +305,5 @@ func NewRemote(kindId string) *Remote { director.SetBuilder(&potatoBuilder{genericBuilder{_kind, Remote{}}}) } director.Construct() - return director.Get() + return director.Get(), nil } diff --git a/remote_test.go b/remote_test.go index a9e02e9..5789e3d 100644 --- a/remote_test.go +++ b/remote_test.go @@ -8,7 +8,7 @@ import ( func TestGetBasicRemote(t *testing.T) { //t.Skip("skipping test") - __rem := NewRemote("basic") + __rem, _ := NewRemote("basic") t.Run("Should return a remote basic type", func(t *testing.T) { assert.NotNil(t, __rem) }) @@ -34,7 +34,7 @@ func TestGetBasicRemote(t *testing.T) { func TestGetBananaRemote(t *testing.T) { //t.Skip("skipping test") - __rem := NewRemote("banana") + __rem, _ := NewRemote("banana") t.Run("Should return a remote banana type", func(t *testing.T) { assert.NotNil(t, __rem) }) @@ -60,7 +60,7 @@ func TestGetBananaRemote(t *testing.T) { func TestGetPotatoRemote(t *testing.T) { //t.Skip("skipping test") - __rem := NewRemote("potato") + __rem, _ := NewRemote("potato") t.Run("Should return a remote basic type", func(t *testing.T) { assert.NotNil(t, __rem) }) diff --git a/tests/helper_test.go b/tests/helper_test.go index daa7fa7..9c81691 100644 --- a/tests/helper_test.go +++ b/tests/helper_test.go @@ -1,6 +1,7 @@ package voicemeeter_test import ( + "fmt" "os" "testing" "time" @@ -9,10 +10,16 @@ import ( ) var ( - vm = voicemeeter.NewRemote("potato") + vm, err = voicemeeter.NewRemote("potato") ) func TestMain(m *testing.M) { + if err != nil { + fmt.Println(err) + os.Exit(1) + } + defer vm.Logout() + vm.Login() code := m.Run() vm.Logout()