func ListFiles(client *server.Client, glob string) { var items []*vault.Item var err error err = prompt.NewProcess("Retriving file information ...", func() error { if glob != "" { items, err = client.Find(glob) } else { items, err = client.List() } return err }) if err != nil { fmt.Printf("%v", err) os.Exit(1) } fmt.Printf("total %d\n", len(items)) writer := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0) for _, i := range items { fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n", humanize.Bytes(i.Size), i.Name, i.Mime, i.Id) } writer.Flush() }
func getCmd(client *server.Client, id string) { r, e := client.Reader(id) if e != nil { fmt.Fprintf(os.Stderr, "%v\n", e) os.Exit(1) } io.Copy(os.Stdout, r) }
func removeCmd(client *server.Client, id string) { err := client.Remove(id) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) } }
func add(path string, config addConfig, client *server.Client) (*vault.Item, error) { if !filepath.IsAbs(path) { abs, err := filepath.Abs(path) if err != nil { return nil, err } path = abs } origiNalPath := path stat, err := os.Stat(path) if err != nil { return nil, err } if stat.IsDir() { tmpDir, err := ioutil.TempDir("", "vault") if err != nil { return nil, err } defer os.RemoveAll(tmpDir) tmpDir = filepath.Join(tmpDir, "vault_zip.zip") err = vault.Pack(path, tmpDir) if err != nil { return nil, err } path = tmpDir } if config.name == "" { basename := filepath.Base(origiNalPath) config.name = basename } c := vault.ItemCreateOptions{ Name: config.name, Size: uint64(stat.Size()), } if config.key != "" { key := vault.Key([]byte(config.key)) c.Key = &key } if c.Mime == "" { ext := filepath.Ext(path) c.Mime = mime.TypeByExtension(ext) } file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() return client.Create(file, c) }