Skip to content

Commit ba1b39d

Browse files
committed
プロセス情報を JSON 形式で標準出力へ出力するように変更。
1 parent 83c7d71 commit ba1b39d

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

main.go

+29-13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const FLAG_NAME_RANDOM_PORT = "random-port"
2626
const FLAG_NAME_PID_FILE = "pid-file"
2727
const FLAG_NAME_PORT_FILE = "port-file"
2828

29+
const OUTPUT_TEMPLATE = "{\n \"pid\": %d,\n \"address\": \"%s\",\n \"port\": %d\n}\n"
30+
2931
//go:embed LICENSE
3032
var license string
3133

@@ -107,16 +109,26 @@ func main() {
107109
panic(err)
108110
}
109111

110-
checkAndCreatePidFile(cCtx.String(FLAG_NAME_PID_FILE))
112+
alreadyRunning, pid, err := checkAndCreatePidFile(cCtx.String(FLAG_NAME_PID_FILE))
113+
if err != nil {
114+
panic(err)
115+
}
111116

112117
address := cCtx.String(FLAG_NAME_ADDRESS)
113118
port := cCtx.Int(FLAG_NAME_PORT)
114119

120+
if alreadyRunning {
121+
fmt.Printf(OUTPUT_TEMPLATE, pid, address, port)
122+
os.Exit(0)
123+
}
124+
115125
if cCtx.Bool(FLAG_NAME_RANDOM_PORT) {
116126
port = getRandomPort()
117127
savePortToCache(cCtx.String(FLAG_NAME_PORT_FILE), port)
118128
}
119129

130+
fmt.Printf(OUTPUT_TEMPLATE, pid, address, port)
131+
120132
startListen(address, strconv.Itoa(port))
121133
return nil
122134
},
@@ -140,53 +152,55 @@ func main() {
140152
// 本当は PID ファイルを消すようにするのがいいのだけど、
141153
// 「3.」でどのみちプロセスの生死確認をしないといけないので、
142154
// それに任せるようにする
143-
func checkAndCreatePidFile(pidFile string) {
155+
//
156+
// alreadyRunning, pid, error を返却する。
157+
func checkAndCreatePidFile(pidFile string) (bool, int, error) {
144158
_, err := os.Stat(pidFile)
145159
if err == nil {
146160
// PID ファイルが存在する場合
147161
// プロセスの有無確認処理を行う
148-
fmt.Println("pid file found.")
162+
fmt.Fprintln(os.Stderr, "pid file found.")
149163
pidFileContent, err := os.ReadFile(pidFile)
150164
if err != nil {
151-
panic(err)
165+
return false, 0, err
152166
}
153167

154168
// 3. ファイルに記載されている PID に対応するプロセスの存在確認
155169

156170
// 取得した PID を数値に変換
157171
existedPid, err := strconv.Atoi(string(pidFileContent))
158172
if err != nil {
159-
panic(err)
173+
return false, existedPid, err
160174
}
161175

162176
// 数値に変換した PID を使ってプロセスの存在確認
163-
fmt.Printf("Test running process PID: %d.\n", existedPid)
177+
fmt.Fprintf(os.Stderr, "Test running process PID: %d.\n", existedPid)
164178
process, err := os.FindProcess(existedPid)
165179
if err == nil {
166180
isRunning, err := IsRunningProcess(process)
167181
if err != nil {
168182
// そもそもチェック処理で失敗
169-
panic(err)
183+
return false, process.Pid, err
170184
}
171185

172186
if isRunning {
173187
// プロセス実行中
174-
fmt.Println("clipboard-receiver already running.")
175-
os.Exit(0)
188+
fmt.Fprintln(os.Stderr, "clipboard-receiver already running.")
189+
return true, process.Pid, nil
176190
} else {
177191
// プロセスが実行中でない
178-
fmt.Println("clipboard-receiver process not found.")
192+
fmt.Fprintln(os.Stderr, "clipboard-receiver process not found.")
179193
err = os.Remove(pidFile)
180194
if err != nil {
181-
panic(err)
195+
return false, process.Pid, err
182196
}
183197
}
184198
} else {
185199
// プロセスが存在していないなら PID ファイルを削除
186-
fmt.Println("clipboard-receiver process not found.")
200+
fmt.Fprintln(os.Stderr, "clipboard-receiver process not found.")
187201
err = os.Remove(pidFile)
188202
if err != nil {
189-
panic(err)
203+
return false, 0, err
190204
}
191205
}
192206
}
@@ -200,6 +214,8 @@ func checkAndCreatePidFile(pidFile string) {
200214
if err != nil {
201215
panic(err)
202216
}
217+
218+
return false, currentPid, nil
203219
}
204220

205221
func getProcessInfoFiles() (string, string, error) {

0 commit comments

Comments
 (0)