Permalink
Browse files

Working towards a better client interface.

It's a bit tedious on some of the other paths in the commandline
client, so I don't have the entire thing consistent yet.
  • Loading branch information...
1 parent aa10e4c commit b85ace847b3ddf2a728d3743950f3e5106932d13 @dustin dustin committed Nov 3, 2012
Showing with 40 additions and 26 deletions.
  1. +10 −2 cbfsclient/conf.go
  2. +23 −0 client/client.go
  3. +7 −24 client/conf.go
View
@@ -7,8 +7,16 @@ import (
"github.com/couchbaselabs/cbfs/client"
)
+func getClient(u string) *cbfsclient.Client {
+ c, err := cbfsclient.New(u)
+ if err != nil {
+ log.Fatalf("Error getting config: %v", err)
+ }
+ return c
+}
+
func getConfCommand(u string, args []string) {
- conf, err := cbfsclient.GetConfig(u)
+ conf, err := getClient(u).GetConfig()
if err != nil {
log.Fatalf("Error getting config: %v", err)
}
@@ -17,7 +25,7 @@ func getConfCommand(u string, args []string) {
func setConfCommand(u string, args []string) {
key, val := args[0], args[1]
- err := cbfsclient.SetConfigParam(u, key, val)
+ err := getClient(u).SetConfigParam(key, val)
if err != nil {
log.Fatalf("Error setting config: %v", err)
}
View
@@ -0,0 +1,23 @@
+// Go client for cbfs
+//
+// Most storage operations are simple HTTP PUT, GET or DELETE
+// operations. Convenience operations are provided for easier access.
+package cbfsclient
+
+import (
+ "net/url"
+)
+
+// A cbfs client.
+type Client string
+
+// Construct a new cbfs client.
+func New(u string) (*Client, error) {
+ uc, err := url.Parse(u)
+ if err != nil {
+ return nil, err
+ }
+ uc.Path = "/"
+ rv := Client(uc.String())
+ return &rv, nil
+}
View
@@ -5,31 +5,19 @@ import (
"encoding/json"
"fmt"
"net/http"
- "net/url"
"github.com/couchbaselabs/cbfs/config"
)
-func getConfURL(uin string) (string, error) {
- u, err := url.Parse(uin)
- if err != nil {
- return "", err
- }
-
- u.Path = "/.cbfs/config/"
- return u.String(), nil
+func (c Client) confURL() string {
+ return string(c) + ".cbfs/config/"
}
// Get the current configuration.
-func GetConfig(u string) (cbfsconfig.CBFSConfig, error) {
+func (c Client) GetConfig() (cbfsconfig.CBFSConfig, error) {
conf := cbfsconfig.CBFSConfig{}
- confu, err := getConfURL(u)
- if err != nil {
- return conf, err
- }
-
- res, err := http.Get(confu)
+ res, err := http.Get(c.confURL())
if err != nil {
return conf, err
}
@@ -43,8 +31,8 @@ func GetConfig(u string) (cbfsconfig.CBFSConfig, error) {
}
// Set a configuration parameter by name.
-func SetConfigParam(u, key, val string) error {
- conf, err := GetConfig(u)
+func (c Client) SetConfigParam(key, val string) error {
+ conf, err := c.GetConfig()
if err != nil {
return err
}
@@ -59,12 +47,7 @@ func SetConfigParam(u, key, val string) error {
return err
}
- confu, err := getConfURL(u)
- if err != nil {
- return err
- }
-
- req, err := http.NewRequest("PUT", confu,
+ req, err := http.NewRequest("PUT", c.confURL(),
bytes.NewBuffer(data))
if err != nil {
return err

0 comments on commit b85ace8

Please sign in to comment.