-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support defining hamlib_rigs through env variables
The envconfig package introduced in 77cc83b does not handle map of structs, so we have to handle hamlib_rigs manually. This also defaults the "network" attribute to "tcp", which is what most users want (for rigctld).
- Loading branch information
1 parent
8e4aa1b
commit 2e61f66
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/la5nta/pat/cfg" | ||
) | ||
|
||
func TestReadRigsFromEnv(t *testing.T) { | ||
const prefix = "PAT_HAMLIB_RIGS" | ||
unset := func() { | ||
for _, env := range os.Environ() { | ||
key, _, _ := strings.Cut(env, "=") | ||
if strings.HasPrefix(key, prefix) { | ||
os.Unsetenv(key) | ||
} | ||
} | ||
} | ||
t.Run("simple", func(t *testing.T) { | ||
defer unset() | ||
var rigs map[string]cfg.HamlibConfig | ||
os.Setenv(prefix+"_rig", "localhost:4532") | ||
if err := readRigsFromEnv(&rigs); err != nil { | ||
t.Fatal(err) | ||
} | ||
if got := rigs["rig"]; (got != cfg.HamlibConfig{Address: "localhost:4532"}) { | ||
t.Fatalf("Got unexpected config: %#v", got) | ||
} | ||
}) | ||
t.Run("with VFO", func(t *testing.T) { | ||
defer unset() | ||
var rigs map[string]cfg.HamlibConfig | ||
os.Setenv(prefix+"_rig", "localhost:4532") | ||
os.Setenv(prefix+"_rig_VFO", "A") | ||
if err := readRigsFromEnv(&rigs); err != nil { | ||
t.Fatal(err) | ||
} | ||
if got := rigs["rig"]; (got != cfg.HamlibConfig{Address: "localhost:4532", VFO: "A"}) { | ||
t.Fatalf("Got unexpected config: %#v", got) | ||
} | ||
}) | ||
t.Run("full", func(t *testing.T) { | ||
defer unset() | ||
var rigs map[string]cfg.HamlibConfig | ||
os.Setenv(prefix+"_rig_ADDRESS", "/dev/ttyS0") | ||
os.Setenv(prefix+"_rig_NETWORK", "serial") | ||
os.Setenv(prefix+"_rig_VFO", "B") | ||
if err := readRigsFromEnv(&rigs); err != nil { | ||
t.Fatal(err) | ||
} | ||
expect := cfg.HamlibConfig{ | ||
Address: "/dev/ttyS0", | ||
Network: "serial", | ||
VFO: "B", | ||
} | ||
if got := rigs["rig"]; got != expect { | ||
t.Fatalf("Got unexpected config: %#v", got) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters