Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
cc519c47
Commit
cc519c47
authored
Nov 09, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Nov 14, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cmd/ipfs2: Ensure process exits smoothly
parent
f3733873
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
31 deletions
+43
-31
cmd/ipfs2/main.go
cmd/ipfs2/main.go
+43
-31
No files found.
cmd/ipfs2/main.go
View file @
cc519c47
...
...
@@ -28,19 +28,29 @@ const (
errorFormat
=
"ERROR: %v
\n\n
"
)
var
ofi
io
.
WriteCloser
func
main
()
{
args
:=
os
.
Args
[
1
:
]
req
,
root
:=
createRequest
(
args
)
handleOptions
(
req
,
root
)
res
:=
callCommand
(
req
,
root
)
outputResponse
(
res
,
root
)
// if debugging, setup profiling.
if
u
.
Debug
{
err
:=
writeHeapProfileToFile
()
var
err
error
ofi
,
err
=
os
.
Create
(
"cpu.prof"
)
if
err
!=
nil
{
log
.
Critical
(
err
)
fmt
.
Println
(
err
)
return
}
pprof
.
StartCPUProfile
(
ofi
)
}
res
:=
callCommand
(
req
,
root
)
outputResponse
(
res
,
root
)
exit
(
0
)
}
func
createRequest
(
args
[]
string
)
(
cmds
.
Request
,
*
cmds
.
Command
)
{
...
...
@@ -52,7 +62,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
options
,
err2
=
getOptions
(
req
,
root
)
if
err2
!=
nil
{
fmt
.
Println
(
err2
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
}
...
...
@@ -88,19 +98,19 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
}
else
{
fmt
.
Println
(
helpText
)
}
os
.
E
xit
(
1
)
e
xit
(
1
)
}
configPath
,
err
:=
getConfigRoot
(
options
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
conf
,
err
:=
getConfig
(
configPath
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
ctx
:=
req
.
Context
()
...
...
@@ -122,7 +132,7 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
options
,
err
:=
getOptions
(
req
,
root
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
if
help
,
found
:=
options
.
Option
(
"help"
);
found
{
...
...
@@ -133,10 +143,10 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
}
else
{
fmt
.
Println
(
helpText
)
}
os
.
E
xit
(
0
)
e
xit
(
0
)
}
else
if
!
ok
{
fmt
.
Println
(
"error: expected 'help' option to be a bool"
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
}
...
...
@@ -145,21 +155,9 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
u
.
Debug
=
true
u
.
SetAllLoggers
(
logging
.
DEBUG
)
// if debugging, setup profiling.
if
u
.
Debug
{
ofi
,
err
:=
os
.
Create
(
"cpu.prof"
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
}
pprof
.
StartCPUProfile
(
ofi
)
defer
ofi
.
Close
()
defer
pprof
.
StopCPUProfile
()
}
}
else
if
!
ok
{
fmt
.
Println
(
"error: expected 'debug' option to be a bool"
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
}
}
...
...
@@ -174,7 +172,7 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
options
,
err
:=
getOptions
(
req
,
root
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
var
found
bool
...
...
@@ -185,7 +183,7 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
localBool
,
ok
=
local
.
(
bool
)
if
!
ok
{
fmt
.
Println
(
"error: expected 'local' option to be a bool"
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
}
...
...
@@ -193,13 +191,13 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
addr
,
err
:=
ma
.
NewMultiaddr
(
req
.
Context
()
.
Config
.
Addresses
.
API
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
_
,
host
,
err
:=
manet
.
DialArgs
(
addr
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
client
:=
cmdsHttp
.
NewClient
(
host
)
...
...
@@ -207,14 +205,14 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
res
,
err
=
client
.
Send
(
req
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
}
else
{
node
,
err
:=
core
.
NewIpfsNode
(
req
.
Context
()
.
Config
,
false
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
E
xit
(
1
)
e
xit
(
1
)
}
defer
node
.
Close
()
req
.
Context
()
.
Node
=
node
...
...
@@ -239,7 +237,7 @@ func outputResponse(res cmds.Response, root *cmds.Command) {
}
}
os
.
E
xit
(
1
)
e
xit
(
1
)
}
out
,
err
:=
res
.
Reader
()
...
...
@@ -300,3 +298,17 @@ func writeHeapProfileToFile() error {
defer
mprof
.
Close
()
return
pprof
.
WriteHeapProfile
(
mprof
)
}
func
exit
(
code
int
)
{
if
u
.
Debug
{
pprof
.
StopCPUProfile
()
ofi
.
Close
()
err
:=
writeHeapProfileToFile
()
if
err
!=
nil
{
log
.
Critical
(
err
)
}
}
os
.
Exit
(
code
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment