@@ -26,6 +26,8 @@ const FLAG_NAME_RANDOM_PORT = "random-port"
26
26
const FLAG_NAME_PID_FILE = "pid-file"
27
27
const FLAG_NAME_PORT_FILE = "port-file"
28
28
29
+ const OUTPUT_TEMPLATE = "{\n \" pid\" : %d,\n \" address\" : \" %s\" ,\n \" port\" : %d\n }\n "
30
+
29
31
//go:embed LICENSE
30
32
var license string
31
33
@@ -107,16 +109,26 @@ func main() {
107
109
panic (err )
108
110
}
109
111
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
+ }
111
116
112
117
address := cCtx .String (FLAG_NAME_ADDRESS )
113
118
port := cCtx .Int (FLAG_NAME_PORT )
114
119
120
+ if alreadyRunning {
121
+ fmt .Printf (OUTPUT_TEMPLATE , pid , address , port )
122
+ os .Exit (0 )
123
+ }
124
+
115
125
if cCtx .Bool (FLAG_NAME_RANDOM_PORT ) {
116
126
port = getRandomPort ()
117
127
savePortToCache (cCtx .String (FLAG_NAME_PORT_FILE ), port )
118
128
}
119
129
130
+ fmt .Printf (OUTPUT_TEMPLATE , pid , address , port )
131
+
120
132
startListen (address , strconv .Itoa (port ))
121
133
return nil
122
134
},
@@ -140,53 +152,55 @@ func main() {
140
152
// 本当は PID ファイルを消すようにするのがいいのだけど、
141
153
// 「3.」でどのみちプロセスの生死確認をしないといけないので、
142
154
// それに任せるようにする
143
- func checkAndCreatePidFile (pidFile string ) {
155
+ //
156
+ // alreadyRunning, pid, error を返却する。
157
+ func checkAndCreatePidFile (pidFile string ) (bool , int , error ) {
144
158
_ , err := os .Stat (pidFile )
145
159
if err == nil {
146
160
// PID ファイルが存在する場合
147
161
// プロセスの有無確認処理を行う
148
- fmt .Println ( "pid file found." )
162
+ fmt .Fprintln ( os . Stderr , "pid file found." )
149
163
pidFileContent , err := os .ReadFile (pidFile )
150
164
if err != nil {
151
- panic ( err )
165
+ return false , 0 , err
152
166
}
153
167
154
168
// 3. ファイルに記載されている PID に対応するプロセスの存在確認
155
169
156
170
// 取得した PID を数値に変換
157
171
existedPid , err := strconv .Atoi (string (pidFileContent ))
158
172
if err != nil {
159
- panic ( err )
173
+ return false , existedPid , err
160
174
}
161
175
162
176
// 数値に変換した PID を使ってプロセスの存在確認
163
- fmt .Printf ( "Test running process PID: %d.\n " , existedPid )
177
+ fmt .Fprintf ( os . Stderr , "Test running process PID: %d.\n " , existedPid )
164
178
process , err := os .FindProcess (existedPid )
165
179
if err == nil {
166
180
isRunning , err := IsRunningProcess (process )
167
181
if err != nil {
168
182
// そもそもチェック処理で失敗
169
- panic ( err )
183
+ return false , process . Pid , err
170
184
}
171
185
172
186
if isRunning {
173
187
// プロセス実行中
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
176
190
} else {
177
191
// プロセスが実行中でない
178
- fmt .Println ( "clipboard-receiver process not found." )
192
+ fmt .Fprintln ( os . Stderr , "clipboard-receiver process not found." )
179
193
err = os .Remove (pidFile )
180
194
if err != nil {
181
- panic ( err )
195
+ return false , process . Pid , err
182
196
}
183
197
}
184
198
} else {
185
199
// プロセスが存在していないなら PID ファイルを削除
186
- fmt .Println ( "clipboard-receiver process not found." )
200
+ fmt .Fprintln ( os . Stderr , "clipboard-receiver process not found." )
187
201
err = os .Remove (pidFile )
188
202
if err != nil {
189
- panic ( err )
203
+ return false , 0 , err
190
204
}
191
205
}
192
206
}
@@ -200,6 +214,8 @@ func checkAndCreatePidFile(pidFile string) {
200
214
if err != nil {
201
215
panic (err )
202
216
}
217
+
218
+ return false , currentPid , nil
203
219
}
204
220
205
221
func getProcessInfoFiles () (string , string , error ) {
0 commit comments