Browse Source

Let user override default Service Codes via config

New config field service_codes takes an array of strings, each string
should be a valid service code that is used when updating rmslist.

I've extended the rmslist cache to support this. We now have multiple
cache files, one for each combination of service codes.

Closes #162
Martin Hebnes Pedersen 1 month ago
parent
commit
19532a5efd
4 changed files with 23 additions and 8 deletions
  1. 6 2
      cfg/config.go
  2. 5 0
      config.go
  3. 2 2
      internal/cmsapi/api.go
  4. 10 4
      rmslist.go

+ 6 - 2
cfg/config.go

@@ -25,6 +25,9 @@ type Config struct {
 	// Maidenhead grid square (e.g. JP20qe).
 	Locator string `json:"locator"`
 
+	// List of service codes for rmslist (defaults to PUBLIC)
+	ServiceCodes []string `json:"service_codes"`
+
 	// Default HTTP listen address (for web UI).
 	//
 	// Use ":8080" to listen on any device, port 8080.
@@ -188,8 +191,9 @@ type BeaconConfig struct {
 }
 
 var DefaultConfig Config = Config{
-	MOTD:     []string{"Open source Winlink client - getpat.io"},
-	AuxAddrs: []string{},
+	MOTD:         []string{"Open source Winlink client - getpat.io"},
+	AuxAddrs:     []string{},
+	ServiceCodes: []string{"PUBLIC"},
 	ConnectAliases: map[string]string{
 		"telnet": "telnet://{mycall}:CMSTelnet@cms.winlink.org:8772/wl2k",
 	},

+ 5 - 0
config.go

@@ -31,6 +31,11 @@ func LoadConfig(path string, fallback cfg.Config) (config cfg.Config, err error)
 		config.ConnectAliases["telnet"] = cfg.DefaultConfig.ConnectAliases["telnet"]
 	}
 
+	// Ensure ServiceCodes has a default value
+	if len(config.ServiceCodes) == 0 {
+		config.ServiceCodes = cfg.DefaultConfig.ServiceCodes
+	}
+
 	return config, nil
 }
 

+ 2 - 2
internal/cmsapi/api.go

@@ -126,7 +126,7 @@ func GetGatewayStatus(mode string, historyHours int, serviceCodes ...string) (io
 	return resp.Body, err
 }
 
-func GetGatewayStatusCached(cacheFile string, forceDownload bool) (io.ReadCloser, error) {
+func GetGatewayStatusCached(cacheFile string, forceDownload bool, serviceCodes ...string) (io.ReadCloser, error) {
 	if !forceDownload {
 		file, err := os.Open(cacheFile)
 		if err == nil {
@@ -140,7 +140,7 @@ func GetGatewayStatusCached(cacheFile string, forceDownload bool) (io.ReadCloser
 	}
 
 	log.Println("Downloading latest gateway status information...")
-	fresh, err := GetGatewayStatus("", 48)
+	fresh, err := GetGatewayStatus("", 48, serviceCodes...)
 	if err != nil {
 		return nil, err
 	}

+ 10 - 4
rmslist.go

@@ -16,10 +16,11 @@ import (
 
 	"github.com/la5nta/wl2k-go/mailbox"
 
-	"github.com/la5nta/pat/internal/cmsapi"
-	"github.com/pd0mz/go-maidenhead"
 	"sort"
 	"strconv"
+
+	"github.com/la5nta/pat/internal/cmsapi"
+	"github.com/pd0mz/go-maidenhead"
 )
 
 type rms struct {
@@ -51,14 +52,19 @@ func rmsListHandle(args []string) {
 	if err != nil {
 		log.Fatal(err)
 	}
-	filePath := path.Join(appDir, "rmslist.json") // Should be moved to a tmp-folder, along with logfile.
+	fileName := "rmslist"
+	isDefaultServiceCode := len(config.ServiceCodes) == 1 && config.ServiceCodes[0] == "PUBLIC"
+	if !isDefaultServiceCode {
+		fileName += "-" + strings.Join(config.ServiceCodes, "-")
+	}
+	filePath := path.Join(appDir, fileName+".json") // Should be moved to a tmp-folder, along with logfile.
 
 	var query string
 	if len(set.Args()) > 0 {
 		query = strings.ToUpper(set.Args()[0])
 	}
 
-	file, err := cmsapi.GetGatewayStatusCached(filePath, *forceDownload)
+	file, err := cmsapi.GetGatewayStatusCached(filePath, *forceDownload, config.ServiceCodes...)
 	if err != nil {
 		log.Fatal(err)
 	}