Exemplo n.º 1
0
Arquivo: ls.go Projeto: 40a/cbfs
func lsCommand(u string, args []string) {
	client, err := cbfsclient.New(u)
	cbfstool.MaybeFatal(err, "Error creating client: %v", err)

	result, err := client.List(lsFlags.Arg(0))
	cbfstool.MaybeFatal(err, "Error listing directory: %v", err)

	dirnames := sort.StringSlice{}
	filenames := sort.StringSlice{}
	for k := range result.Dirs {
		dirnames = append(dirnames, k)
	}
	for k := range result.Files {
		filenames = append(filenames, k)
	}
	dirnames.Sort()
	filenames.Sort()

	if *lsDashL {
		totalFiles := 0
		totalSize := uint64(0)
		tw := tabwriter.NewWriter(os.Stdout, 2, 4, 2, ' ', 0)
		for i := range dirnames {
			dn := dirnames[i]
			di := result.Dirs[dn]
			fmt.Fprintf(tw, "d %8s\t%s\t(%s descendants)\n",
				humanize.Bytes(uint64(di.Size)), dn,
				humanize.Comma(int64(di.Descendants)))
			totalSize += uint64(di.Size)
			totalFiles += di.Descendants
		}
		for i := range filenames {
			fn := filenames[i]
			fi := result.Files[fn]
			fmt.Fprintf(tw, "f %8s\t%s\t%s\n",
				humanize.Bytes(uint64(fi.Length)), fn,
				fi.Headers.Get("Content-Type"))

			totalSize += uint64(fi.Length)
			totalFiles++
		}
		fmt.Fprintf(tw, "----------------------------------------\n")
		fmt.Fprintf(tw, "Tot: %s\t\t%s files\n",
			humanize.Bytes(totalSize),
			humanize.Comma(int64(totalFiles)))
		tw.Flush()
	} else {
		allnames := sort.StringSlice{}
		for i := range dirnames {
			allnames = append(allnames, dirnames[i])
		}
		for i := range filenames {
			allnames = append(allnames, filenames[i])
		}
		allnames.Sort()
		for _, a := range allnames {
			fmt.Println(a)
		}
	}
}
Exemplo n.º 2
0
func newCBFSStore(ustr string) (cbfsStore, error) {
	rv := cbfsStore{known: map[string]bool{}}

	u, err := url.Parse(ustr)
	if err != nil {
		return rv, err
	}
	rv.path = u.Path
	u.Path = "/"

	client, err := cbfsclient.New(u.String())
	if err != nil {
		return rv, err
	}

	l, err := client.List(rv.path)
	if err != nil {
		return rv, err
	}

	for fn := range l.Files {
		rv.known[fn] = true
	}

	rv.client = client

	return rv, nil
}