aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/executable.go
blob: b825306ab850f975e4abc9fad3262dff30c7ea10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package container

import (
	"fmt"
	"log"
	"os"
	"sync"

	"hakurei.app/message"
)

var (
	executable     string
	executableOnce sync.Once
)

func copyExecutable(msg message.Msg) {
	if name, err := os.Executable(); err != nil {
		m := fmt.Sprintf("cannot read executable path: %v", err)
		if msg != nil {
			msg.BeforeExit()
			msg.GetLogger().Fatal(m)
		} else {
			log.Fatal(m)
		}
	} else {
		executable = name
	}
}

// MustExecutable calls [os.Executable] and terminates the process on error.
//
// Deprecated: This is no longer used and will be removed in 0.4.
func MustExecutable(msg message.Msg) string {
	executableOnce.Do(func() { copyExecutable(msg) })
	return executable
}