-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (87 loc) · 2.65 KB
/
Copy pathmain.go
File metadata and controls
96 lines (87 loc) · 2.65 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2026 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main implements gopherd, a minimal PID 1 init process and service supervisor.
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
"github.com/haproxytech/gopherd/control"
"github.com/haproxytech/gopherd/version"
)
const defaultConfigPath = "/etc/gopherd/gopherd.yml"
func main() {
log.SetFlags(0)
log.SetPrefix("gopherd: ")
_ = version.Set()
// Split os.Args on "--": everything after is entrypoint args. A leading
// "-" arg is neither a client command nor a passthrough binary, so treat
// all args as entrypoint args.
var entrypointArgs []string
programArgs := os.Args[1:]
if len(programArgs) > 0 && strings.HasPrefix(programArgs[0], "-") {
// Strip a leading "--" separator if present.
if programArgs[0] == "--" {
entrypointArgs = programArgs[1:]
} else {
entrypointArgs = programArgs
}
programArgs = nil
} else {
for i, arg := range programArgs {
if arg == "--" {
entrypointArgs = programArgs[i+1:]
programArgs = programArgs[:i]
break
}
}
}
// CLI client mode or passthrough exec.
if len(programArgs) > 0 {
first := programArgs[0]
if control.IsClientCommand(programArgs) {
control.RunClient(programArgs)
return
}
if first == "version" {
fmt.Println("gopherd", version.Version)
fmt.Println("built from:", version.Repo)
fmt.Println("commit date:", version.CommitDate)
return
}
if first == "tag" {
fmt.Println(version.Tag)
return
}
// Passthrough: exec the command, replacing this process.
path, err := exec.LookPath(first)
if err != nil {
fmt.Fprintf(os.Stderr, "gopherd: %q not found (not a known command and not on PATH)\n", first)
fmt.Fprintf(os.Stderr, "Known commands: %s, version, tag\n", strings.Join(control.ClientCommandList(), ", "))
os.Exit(1)
}
// Re-append entrypoint args for passthrough.
execArgs := programArgs
if len(entrypointArgs) > 0 {
execArgs = append(execArgs, entrypointArgs...)
}
if err := syscall.Exec(path, execArgs, os.Environ()); err != nil {
log.Fatalf("exec %s: %v", path, err)
}
}
os.Exit(run(entrypointArgs))
}