func isPublic(path string) (bool, error) { res, err := xattr.Get(path, "public") if err != nil { if !xattr.IsNotExist(err) { return false, err } } else { if string(res) == "1" { return true, nil } } return false, nil }
func main() { commentPtr := flag.String("comment", "", "optional commit comment") publicPtr := flag.Bool("public", false, "share the node publicly (default to semi-private)") // shareTTLPtr := flag.String("share-ttl", "1h", "TTL for the semi-private sharing linl (default to 1h)") flag.Usage = Usage flag.Parse() if flag.NArg() < 1 { Usage() os.Exit(2) } cmd := flag.Arg(0) rsocket, err := ioutil.ReadFile(".blobfs_socket") // TODO(tsileo): do the same for bash transport := &httpunix.Transport{ DialTimeout: 100 * time.Millisecond, RequestTimeout: 1 * time.Second, ResponseHeaderTimeout: 1 * time.Second, } transport.RegisterLocation("blobfs", string(rsocket)) var client = http.Client{ Transport: transport, } u := "http+unix://blobfs" if cmd == "__ps1_bash" { if os.IsNotExist(err) { fmt.Printf("") return } request, err := http.NewRequest("GET", fmt.Sprintf("%s%s", u, "/ref"), nil) if err != nil { return } resp, err := client.Do(request) if err != nil { return } if resp.StatusCode != 200 { return } rr := &RefResp{} if err := json.NewDecoder(resp.Body).Decode(rr); err != nil { return } fmt.Printf("%s:(%s) ", bold("blobfs"), yellow(rr.Ref)) return } if cmd == "__ps1_zsh" { if os.IsNotExist(err) { fmt.Printf("") return } // TODO(tsileo): a getRef request, err := http.NewRequest("GET", fmt.Sprintf("%s%s", u, "/ref"), nil) if err != nil { return } resp, err := client.Do(request) if err != nil { return } if resp.StatusCode != 200 { return } rr := &RefResp{} if err := json.NewDecoder(resp.Body).Decode(rr); err != nil { return } fmt.Print("%Bblobfs%b:(%{\033[33m%}" + rr.Ref + "%{\033[0m%}) ") return } if err != nil { // panic(err) } url := string(u) switch cmd { case "checkout": if err := Checkout(client, url, flag.Arg(1)); err != nil { panic(err) } case "history", "log": if err := Log(client, url); err != nil { panic(err) } case "sync", "push": if err := Sync(client, url, *commentPtr); err != nil { panic(err) } case "fetch", "pull": if err := Pull(client, url); err != nil { panic(err) } case "debug": if err := Debug(client, url); err != nil { panic(err) } case "share": path := "." if flag.NArg() == 2 { path = flag.Arg(1) } public, err := isPublic(path) if err != nil { panic(err) } fmt.Printf("public:%s\n", public) // Share in "public" mode if *publicPtr { if public { burl, err := xattr.Get(path, "url") if err != nil { panic(err) } fmt.Printf("%s\n", burl) } else { if err := xattr.Set(path, "public", []byte("1")); err != nil { panic(err) } burl, err := xattr.Get(path, "url") if err != nil { panic(err) } fmt.Printf("%s\n", burl) } fmt.Printf("\nYou still need to commit for the file to become available.") return } // TODO(tsileo): display a special message if a semi-privat link is requested // for a node in staging only. // Share in semi-private mode (e.g. anyone with the link can access it) burl, err := xattr.Get(path, "url.semiprivate") if err != nil { panic(err) } fmt.Printf("%s\n", burl) fmt.Printf("\nYou still need to make a sync for the file to become available.") case "unshare": path := "." if flag.NArg() == 2 { path = flag.Arg(1) } public, err := isPublic(path) if err != nil { panic(err) } fmt.Printf("public:%s\n", public) if !public { // XXX(tsileo): color in red? fmt.Printf("You can only unshare public nodes") // TODO(tsileo): return with error code return } if err := xattr.Set(path, "public", []byte("0")); err != nil { panic(err) } fmt.Printf("\nYou still need to commit for the file to become unavailable.") case "prune", "public": // XXX(tsileo): find a better name than `public` for listing public nodes fmt.Printf("Not implemented yet") default: fmt.Printf("unknown cmd %v", cmd) } }