Skip to content

Commit b6ae0db

Browse files
committed
feat: support use working directory from environment variable
1 parent 44895d4 commit b6ae0db

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

prefix_dialer.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ import (
44
"errors"
55
"net"
66
"os"
7+
"path"
78
"strings"
89
"syscall"
910
)
1011

1112
// PrefixDialer uses github.com/Microsoft/go-winio.{DialPipe,ListenPipe} on windows and net.{Dial,Listen} on other platforms.
1213
type PrefixDialer struct {
13-
Prefix string
14+
Prefix string
15+
WorkingDirectory string
1416
}
1517

1618
var _ Dialer = PrefixDialer{}
1719

1820
func (pd PrefixDialer) Listen(name string) (net.Listener, error) {
19-
return listen(pd.Prefix + name)
21+
return listen(path.Join(pd.WorkingDirectory, pd.Prefix+name))
2022
}
2123

2224
func (pd PrefixDialer) Dial(name string) (net.Conn, error) {
2325
var builder strings.Builder
2426
builder.WriteString(pd.Prefix)
2527
builder.WriteString(name)
2628

27-
c, err := dial(builder.String())
29+
c, err := dial(path.Join(pd.WorkingDirectory, builder.String()))
2830

2931
// attempt to remove the file if nobody is listening
3032
if err != nil && errorIsNobodyListening(err) {
@@ -43,3 +45,21 @@ func errorIsNobodyListening(err error) bool {
4345
}
4446
return false
4547
}
48+
49+
func (cfg Config) NewPrefixDialer(prefix string) PrefixDialer {
50+
// convert name to uppercase with underline
51+
name := strings.ToUpper(strings.ReplaceAll(cfg.Name, "-", "_"))
52+
if name == "" {
53+
name = "RISEFRONT"
54+
}
55+
workingDirectory := os.Getenv(name + "_WORKING_DIR")
56+
57+
if workingDirectory == "" {
58+
workingDirectory = getWorkingDir()
59+
}
60+
61+
return PrefixDialer{
62+
Prefix: prefix,
63+
WorkingDirectory: workingDirectory,
64+
}
65+
}

risefront.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func New(ctx context.Context, cfg Config) error {
7373
}
7474

7575
if cfg.Dialer == nil {
76-
cfg.Dialer = PrefixDialer{}
76+
cfg.Dialer = cfg.NewPrefixDialer("")
7777
}
7878
if cfg.Network == "" {
7979
cfg.Network = "tcp"

socket.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@
33
package risefront
44

55
import (
6-
"log"
76
"net"
87
"os"
9-
"path"
108

119
"golang.org/x/sys/unix"
1210
)
1311

14-
var workingDirectory string
15-
16-
func init() {
17-
workingDirectory, _ = os.Getwd()
18-
if unix.Access(workingDirectory, unix.W_OK) != nil {
19-
log.Println("The current working directory is not writable, using temp dir")
20-
workingDirectory = os.TempDir()
21-
}
22-
}
23-
2412
func listen(name string) (net.Listener, error) {
25-
return net.Listen("unix", path.Join(workingDirectory, name))
13+
return net.Listen("unix", name)
2614
}
2715

2816
func dial(name string) (net.Conn, error) {
29-
return net.Dial("unix", path.Join(workingDirectory, name))
17+
return net.Dial("unix", name)
18+
}
19+
20+
func getWorkingDir() string {
21+
dir, _ := os.Getwd()
22+
if unix.Access(dir, unix.W_OK) != nil {
23+
return os.TempDir()
24+
}
25+
return dir
3026
}

socket_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ func listen(name string) (net.Listener, error) {
1515
func dial(name string) (net.Conn, error) {
1616
return winio.DialPipe(`\\.\pipe\`+name, nil)
1717
}
18+
19+
func getWorkingDir() string {
20+
// Windows does not need it for named pipes
21+
return ""
22+
}

0 commit comments

Comments
 (0)