refactor: rename address flag and environment variable to 'listen'

This commit is contained in:
henrygd
2025-03-05 23:37:51 -05:00
parent c8130a10d4
commit 5fbc0de07f
3 changed files with 36 additions and 36 deletions

View File

@@ -14,14 +14,14 @@ import (
// cli options // cli options
type cmdOptions struct { type cmdOptions struct {
key string // key is the public key(s) for SSH authentication. key string // key is the public key(s) for SSH authentication.
addr string // addr is the address or port to listen on. listen string // listen is the address or port to listen on.
} }
// parseFlags parses the command line flags and populates the config struct. // parseFlags parses the command line flags and populates the config struct.
func (opts *cmdOptions) parseFlags() { func (opts *cmdOptions) parseFlags() {
flag.StringVar(&opts.key, "key", "", "Public key(s) for SSH authentication") flag.StringVar(&opts.key, "key", "", "Public key(s) for SSH authentication")
flag.StringVar(&opts.addr, "addr", "", "Address or port to listen on") flag.StringVar(&opts.listen, "listen", "", "Address or port to listen on")
flag.Usage = func() { flag.Usage = func() {
fmt.Printf("Usage: %s [options] [subcommand]\n", os.Args[0]) fmt.Printf("Usage: %s [options] [subcommand]\n", os.Args[0])
@@ -82,11 +82,11 @@ func (opts *cmdOptions) loadPublicKeys() ([]ssh.PublicKey, error) {
// getAddress gets the address to listen on from the command line flag, environment variable, or default value. // getAddress gets the address to listen on from the command line flag, environment variable, or default value.
func (opts *cmdOptions) getAddress() string { func (opts *cmdOptions) getAddress() string {
// Try command line flag first // Try command line flag first
if opts.addr != "" { if opts.listen != "" {
return opts.addr return opts.listen
} }
// Try environment variables // Try environment variables
if addr, ok := agent.GetEnv("ADDR"); ok && addr != "" { if addr, ok := agent.GetEnv("LISTEN"); ok && addr != "" {
return addr return addr
} }
// Legacy PORT environment variable support // Legacy PORT environment variable support
@@ -101,7 +101,7 @@ func (opts *cmdOptions) getNetwork() string {
if network, _ := agent.GetEnv("NETWORK"); network != "" { if network, _ := agent.GetEnv("NETWORK"); network != "" {
return network return network
} }
if strings.HasPrefix(opts.addr, "/") { if strings.HasPrefix(opts.listen, "/") {
return "unix" return "unix"
} }
return "tcp" return "tcp"
@@ -117,7 +117,7 @@ func main() {
flag.Parse() flag.Parse()
opts.addr = opts.getAddress() opts.listen = opts.getAddress()
var serverConfig agent.ServerOptions var serverConfig agent.ServerOptions
var err error var err error
@@ -126,7 +126,7 @@ func main() {
log.Fatal("Failed to load public keys:", err) log.Fatal("Failed to load public keys:", err)
} }
serverConfig.Addr = opts.addr serverConfig.Addr = opts.listen
serverConfig.Network = opts.getNetwork() serverConfig.Network = opts.getNetwork()
agent := agent.NewAgent() agent := agent.NewAgent()

View File

@@ -27,22 +27,22 @@ func TestGetAddress(t *testing.T) {
{ {
name: "use address from flag", name: "use address from flag",
opts: cmdOptions{ opts: cmdOptions{
addr: "8080", listen: "8080",
}, },
expected: "8080", expected: "8080",
}, },
{ {
name: "use unix socket from flag", name: "use unix socket from flag",
opts: cmdOptions{ opts: cmdOptions{
addr: "/tmp/beszel.sock", listen: "/tmp/beszel.sock",
}, },
expected: "/tmp/beszel.sock", expected: "/tmp/beszel.sock",
}, },
{ {
name: "use ADDR env var", name: "use LISTEN env var",
opts: cmdOptions{}, opts: cmdOptions{},
envVars: map[string]string{ envVars: map[string]string{
"ADDR": "1.2.3.4:9090", "LISTEN": "1.2.3.4:9090",
}, },
expected: "1.2.3.4:9090", expected: "1.2.3.4:9090",
}, },
@@ -57,21 +57,21 @@ func TestGetAddress(t *testing.T) {
{ {
name: "use unix socket from env var", name: "use unix socket from env var",
opts: cmdOptions{ opts: cmdOptions{
addr: "", listen: "",
}, },
envVars: map[string]string{ envVars: map[string]string{
"ADDR": "/tmp/beszel.sock", "LISTEN": "/tmp/beszel.sock",
}, },
expected: "/tmp/beszel.sock", expected: "/tmp/beszel.sock",
}, },
{ {
name: "flag takes precedence over env vars", name: "flag takes precedence over env vars",
opts: cmdOptions{ opts: cmdOptions{
addr: ":8080", listen: ":8080",
}, },
envVars: map[string]string{ envVars: map[string]string{
"ADDR": ":9090", "LISTEN": ":9090",
"PORT": "7070", "PORT": "7070",
}, },
expected: ":8080", expected: ":8080",
}, },
@@ -201,27 +201,27 @@ func TestGetNetwork(t *testing.T) {
}, },
{ {
name: "only port", name: "only port",
opts: cmdOptions{addr: "8080"}, opts: cmdOptions{listen: "8080"},
expected: "tcp", expected: "tcp",
}, },
{ {
name: "ipv4 address", name: "ipv4 address",
opts: cmdOptions{addr: "1.2.3.4:8080"}, opts: cmdOptions{listen: "1.2.3.4:8080"},
expected: "tcp", expected: "tcp",
}, },
{ {
name: "ipv6 address", name: "ipv6 address",
opts: cmdOptions{addr: "[2001:db8::1]:8080"}, opts: cmdOptions{listen: "[2001:db8::1]:8080"},
expected: "tcp", expected: "tcp",
}, },
{ {
name: "unix network", name: "unix network",
opts: cmdOptions{addr: "/tmp/beszel.sock"}, opts: cmdOptions{listen: "/tmp/beszel.sock"},
expected: "unix", expected: "unix",
}, },
{ {
name: "env var network", name: "env var network",
opts: cmdOptions{addr: ":8080"}, opts: cmdOptions{listen: ":8080"},
envVars: map[string]string{"NETWORK": "tcp4"}, envVars: map[string]string{"NETWORK": "tcp4"},
expected: "tcp4", expected: "tcp4",
}, },
@@ -256,32 +256,32 @@ func TestParseFlags(t *testing.T) {
name: "no flags", name: "no flags",
args: []string{"cmd"}, args: []string{"cmd"},
expected: cmdOptions{ expected: cmdOptions{
key: "", key: "",
addr: "", listen: "",
}, },
}, },
{ {
name: "key flag only", name: "key flag only",
args: []string{"cmd", "-key", "testkey"}, args: []string{"cmd", "-key", "testkey"},
expected: cmdOptions{ expected: cmdOptions{
key: "testkey", key: "testkey",
addr: "", listen: "",
}, },
}, },
{ {
name: "addr flag only", name: "addr flag only",
args: []string{"cmd", "-addr", ":8080"}, args: []string{"cmd", "-listen", ":8080"},
expected: cmdOptions{ expected: cmdOptions{
key: "", key: "",
addr: ":8080", listen: ":8080",
}, },
}, },
{ {
name: "both flags", name: "both flags",
args: []string{"cmd", "-key", "testkey", "-addr", ":8080"}, args: []string{"cmd", "-key", "testkey", "-listen", ":8080"},
expected: cmdOptions{ expected: cmdOptions{
key: "testkey", key: "testkey",
addr: ":8080", listen: ":8080",
}, },
}, },
} }

View File

@@ -61,13 +61,13 @@ function copyDockerCompose(port = "45876", publicKey: string) {
# monitor other disks / partitions by mounting a folder in /extra-filesystems # monitor other disks / partitions by mounting a folder in /extra-filesystems
# - /mnt/disk/.beszel:/extra-filesystems/sda1:ro # - /mnt/disk/.beszel:/extra-filesystems/sda1:ro
environment: environment:
PORT: ${port} LISTEN: ${port}
KEY: "${publicKey}"`) KEY: "${publicKey}"`)
} }
function copyDockerRun(port = "45876", publicKey: string) { function copyDockerRun(port = "45876", publicKey: string) {
copyToClipboard( copyToClipboard(
`docker run -d --name beszel-agent --network host --restart unless-stopped -v /var/run/docker.sock:/var/run/docker.sock:ro -e KEY="${publicKey}" -e PORT=${port} henrygd/beszel-agent:latest` `docker run -d --name beszel-agent --network host --restart unless-stopped -v /var/run/docker.sock:/var/run/docker.sock:ro -e KEY="${publicKey}" -e LISTEN=${port} henrygd/beszel-agent:latest`
) )
} }
@@ -265,7 +265,7 @@ const CopyButton = memo((props: CopyButtonProps) => {
</a> </a>
</DropdownMenuItem> </DropdownMenuItem>
) : ( ) : (
<DropdownMenuItem onClick={props.dropdownOnClick}>{props.dropdownText}</DropdownMenuItem> <DropdownMenuItem onClick={props.dropdownOnClick} className="cursor-pointer">{props.dropdownText}</DropdownMenuItem>
)} )}
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>