Commit 3dd7a9a5 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

cmd/ipfs2: Broke up main into subfunctions

parent 7c1e4578
...@@ -21,7 +21,13 @@ var log = u.Logger("cmd/ipfs") ...@@ -21,7 +21,13 @@ var log = u.Logger("cmd/ipfs")
func main() { func main() {
args := os.Args[1:] args := os.Args[1:]
req, root := createRequest(args)
handleOptions(req, root)
res := callCommand(req, root)
outputResponse(res)
}
func createRequest(args []string) (cmds.Request, *cmds.Command) {
req, root, err := cmdsCli.Parse(args, Root, commands.Root) req, root, err := cmdsCli.Parse(args, Root, commands.Root)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -35,6 +41,40 @@ func main() { ...@@ -35,6 +41,40 @@ func main() {
os.Exit(1) os.Exit(1)
} }
configPath, err := getConfigRoot(options)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
conf, err := getConfig(configPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ctx := req.Context()
ctx.ConfigRoot = configPath
ctx.Config = conf
if _, found := options.Option("encoding"); !found {
if req.Command().Format != nil {
req.SetOption("encoding", cmds.Text)
} else {
req.SetOption("encoding", cmds.JSON)
}
}
return req, root
}
func handleOptions(req cmds.Request, root *cmds.Command) {
options, err := getOptions(req, root)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if help, found := options.Option("help"); found { if help, found := options.Option("help"); found {
if helpBool, ok := help.(bool); helpBool && ok { if helpBool, ok := help.(bool); helpBool && ok {
fmt.Println(req.Command().Help) fmt.Println(req.Command().Help)
...@@ -65,39 +105,25 @@ func main() { ...@@ -65,39 +105,25 @@ func main() {
os.Exit(1) os.Exit(1)
} }
} }
}
configPath, err := getConfigRoot(options) func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
if err != nil { var res cmds.Response
fmt.Println(err)
os.Exit(1)
}
conf, err := getConfig(configPath) if root == Root {
res = root.Call(req)
} else {
options, err := getOptions(req, root)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
ctx := req.Context()
ctx.ConfigRoot = configPath
ctx.Config = conf
if _, found := options.Option("encoding"); !found {
if req.Command().Format != nil {
req.SetOption("encoding", cmds.Text)
} else {
req.SetOption("encoding", cmds.JSON)
}
}
var res cmds.Response
if root == Root {
res = root.Call(req)
} else {
var found bool var found bool
var local interface{}
localBool := false localBool := false
if local, found := options.Option("local"); found { if local, found = options.Option("local"); found {
var ok bool var ok bool
localBool, ok = local.(bool) localBool, ok = local.(bool)
if !ok { if !ok {
...@@ -106,7 +132,7 @@ func main() { ...@@ -106,7 +132,7 @@ func main() {
} }
} }
if (!found || !localBool) && daemon.Locked(configPath) { if (!found || !localBool) && daemon.Locked(req.Context().ConfigRoot) {
res, err = cmdsHttp.Send(req) res, err = cmdsHttp.Send(req)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -114,23 +140,27 @@ func main() { ...@@ -114,23 +140,27 @@ func main() {
} }
} else { } else {
node, err := core.NewIpfsNode(conf, false) node, err := core.NewIpfsNode(req.Context().Config, false)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
ctx.Node = node req.Context().Node = node
res = root.Call(req) res = root.Call(req)
} }
} }
return res
}
func outputResponse(res cmds.Response) {
if res.Error() != nil { if res.Error() != nil {
fmt.Println(res.Error().Error()) fmt.Println(res.Error().Error())
if req.Command().Help != "" && res.Error().Code == cmds.ErrClient { if res.Request().Command().Help != "" && res.Error().Code == cmds.ErrClient {
// TODO: convert from markdown to ANSI terminal format? // TODO: convert from markdown to ANSI terminal format?
fmt.Println(req.Command().Help) fmt.Println(res.Request().Command().Help)
} }
os.Exit(1) os.Exit(1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment