Skip to content

Commit 17f261c

Browse files
committed
Refined ports filter
1 parent 670fe39 commit 17f261c

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

main.go

+27-18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io"
2727
"os"
2828
"os/exec"
29+
"regexp"
2930
"runtime"
3031
"runtime/debug"
3132
"strconv"
@@ -69,24 +70,27 @@ var (
6970

7071
// iniflags
7172
var (
72-
address = iniConf.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
73-
appName = iniConf.String("appName", "", "")
74-
gcType = iniConf.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
75-
hostname = iniConf.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
76-
httpProxy = iniConf.String("httpProxy", "", "Proxy server for HTTP requests")
77-
httpsProxy = iniConf.String("httpsProxy", "", "Proxy server for HTTPS requests")
78-
indexURL = iniConf.String("indexURL", "https://downloads.arduino.cc/packages/package_index.json", "The address from where to download the index json containing the location of upload tools")
79-
iniConf = flag.NewFlagSet("ini", flag.ContinueOnError)
80-
logDump = iniConf.String("log", "off", "off = (default)")
81-
origins = iniConf.String("origins", "", "Allowed origin list for CORS")
82-
regExpFilter = iniConf.String("regex", "usb|acm|com", "Regular expression to filter serial port list")
83-
signatureKey = iniConf.String("signatureKey", globals.SignatureKey, "Pem-encoded public key to verify signed commandlines")
84-
updateURL = iniConf.String("updateUrl", "", "")
85-
verbose = iniConf.Bool("v", true, "show debug logging")
86-
crashreport = iniConf.Bool("crashreport", false, "enable crashreport logging")
87-
autostartMacOS = iniConf.Bool("autostartMacOS", true, "the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)")
73+
address = iniConf.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
74+
appName = iniConf.String("appName", "", "")
75+
gcType = iniConf.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
76+
hostname = iniConf.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
77+
httpProxy = iniConf.String("httpProxy", "", "Proxy server for HTTP requests")
78+
httpsProxy = iniConf.String("httpsProxy", "", "Proxy server for HTTPS requests")
79+
indexURL = iniConf.String("indexURL", "https://downloads.arduino.cc/packages/package_index.json", "The address from where to download the index json containing the location of upload tools")
80+
iniConf = flag.NewFlagSet("ini", flag.ContinueOnError)
81+
logDump = iniConf.String("log", "off", "off = (default)")
82+
origins = iniConf.String("origins", "", "Allowed origin list for CORS")
83+
portsFilterRegexp = iniConf.String("regex", "usb|acm|com", "Regular expression to filter serial port list")
84+
signatureKey = iniConf.String("signatureKey", globals.SignatureKey, "Pem-encoded public key to verify signed commandlines")
85+
updateURL = iniConf.String("updateUrl", "", "")
86+
verbose = iniConf.Bool("v", true, "show debug logging")
87+
crashreport = iniConf.Bool("crashreport", false, "enable crashreport logging")
88+
autostartMacOS = iniConf.Bool("autostartMacOS", true, "the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)")
8889
)
8990

91+
// the ports filter provided by the user via the -regex flag, if any
92+
var portsFilter *regexp.Regexp
93+
9094
var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHTML))
9195

9296
// If you navigate to this server's homepage, you'll get this HTML
@@ -302,8 +306,13 @@ func loop() {
302306
}
303307

304308
// see if they provided a regex filter
305-
if len(*regExpFilter) > 0 {
306-
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
309+
if len(*portsFilterRegexp) > 0 {
310+
log.Printf("You specified a serial port regular expression filter: %v\n", *portsFilterRegexp)
311+
if filter, err := regexp.Compile("(?i)" + *portsFilterRegexp); err != nil {
312+
log.Panicf("Error compiling the regex filter: %v\n", err)
313+
} else {
314+
portsFilter = filter
315+
}
307316
}
308317

309318
// list serial ports

seriallist.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package main
1919

2020
import (
2121
"fmt"
22-
"regexp"
22+
"slices"
2323
"strings"
2424

2525
log "github.com/sirupsen/logrus"
@@ -59,21 +59,14 @@ func enumerateSerialPorts() ([]OsSerialPort, error) {
5959
}
6060

6161
// see if we should filter the list
62-
if len(*regExpFilter) > 0 {
63-
// yes, user asked for a filter
64-
reFilter := regexp.MustCompile("(?i)" + *regExpFilter)
65-
66-
newarrPorts := []OsSerialPort{}
67-
for _, element := range arrPorts {
68-
// if matches regex, include
69-
if reFilter.MatchString(element.Name) {
70-
newarrPorts = append(newarrPorts, element)
71-
} else {
72-
log.Debugf("serial port did not match. port: %v\n", element)
62+
if portsFilter != nil {
63+
arrPorts = slices.DeleteFunc(arrPorts, func(port OsSerialPort) bool {
64+
match := portsFilter.MatchString(port.Name)
65+
if !match {
66+
log.Debugf("ignoring port not matching filter. port: %v\n", port)
7367
}
74-
75-
}
76-
arrPorts = newarrPorts
68+
return match
69+
})
7770
}
7871

7972
return arrPorts, err

0 commit comments

Comments
 (0)