return err from NewRemote\

pass Kind.Name to logout

update examples/tests to reflect changes
This commit is contained in:
onyx-and-iris 2022-08-23 03:16:43 +01:00
parent 3fd08ff606
commit 6fabc43998
6 changed files with 33 additions and 17 deletions

View File

@ -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.

View File

@ -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()
}

View File

@ -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()

View File

@ -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
}

View File

@ -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)
})

View File

@ -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()