add create command

This commit is contained in:
Victor Vieux 2014-11-18 01:34:08 +00:00
parent 05e95dfc7e
commit 45ca8472f1
2 changed files with 28 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package discovery
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
@ -38,3 +39,13 @@ func RegisterSlave(addr, token string) error {
_, err := http.Post(fmt.Sprintf("%s/%s/%s", DISCOVERY_URL, "clusters", token), "application/json", buf)
return err
}
// CreateCluster returns a unique cluster token
func CreateCluster() (string, error) {
resp, err := http.Post(fmt.Sprintf("%s/%s", DISCOVERY_URL, "clusters"), "", nil)
if err != nil {
return "", err
}
token, err := ioutil.ReadAll(resp.Body)
return string(token), err
}

18
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"time"
@ -60,6 +61,19 @@ func main() {
}
app.Commands = []cli.Command{
{
Name: "create",
ShortName: "c",
Usage: "create a cluster",
Action: func(c *cli.Context) {
token, err := discovery.CreateCluster()
if err != nil {
log.Fatal(err)
}
fmt.Println(token)
},
},
{
Name: "manage",
ShortName: "m",
@ -144,5 +158,7 @@ func main() {
},
}
log.Fatal(app.Run(os.Args))
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}