Example #1
0
File: s3tree.go Project: a8m/s3tree
func main() {
	flag.Usage = func() { fmt.Fprint(os.Stderr, usage) }
	flag.Parse()
	var noPrefix = len(*prefix) == 0
	if len(*bucket) == 0 {
		err := errors.New("-b(s3 bucket) is required.")
		errAndExit(err)
	}
	svc := s3.New(session.New(&aws.Config{Region: region}))
	spin := NewSpin()
	resp, err := svc.ListObjects(&s3.ListObjectsInput{
		Bucket: bucket,
		Prefix: prefix,
	})
	spin.Done()
	var fs = NewFs()
	if err != nil {
		errAndExit(err)
	} else {
		// Loop over s3 object
		for _, obj := range resp.Contents {
			key := *obj.Key
			if noPrefix {
				key = fmt.Sprintf("%s/%s", *bucket, key)
			}
			fs.addFile(key, obj)
		}
	}
	var nd, nf int
	rootDir := *prefix
	if noPrefix {
		rootDir = *bucket
	}
	// Output file
	var outFile = os.Stdout
	if *o != "" {
		outFile, err = os.Create(*o)
		if err != nil {
			errAndExit(err)
		}
	}
	defer outFile.Close()
	opts := &tree.Options{
		Fs:        fs,
		OutFile:   outFile,
		All:       *a,
		DirsOnly:  *d,
		FullPath:  *f,
		ByteSize:  *s,
		UnitSize:  *h,
		Quotes:    *Q,
		LastMod:   *D,
		Colorize:  *C,
		DeepLevel: *L,
		NoSort:    *U,
		ReverSort: *r,
		DirSort:   *dirsfirst,
		VerSort:   *v || *sort == "version",
		NameSort:  *sort == "name",
		SizeSort:  *sort == "size",
		ModSort:   *t,
		Pattern:   *P,
		IPattern:  *I,
	}
	inf := tree.New(rootDir)
	if d, f := inf.Visit(opts); f != 0 {
		nd, nf = nd+d-1, nf+f
	}
	inf.Print(opts)
	// print footer
	footer := fmt.Sprintf("\n%d directories", nd)
	if !opts.DirsOnly {
		footer += fmt.Sprintf(", %d files", nf)
	}
	fmt.Fprintf(outFile, footer)
}
Example #2
0
File: tree.go Project: a8m/tree
func main() {
	flag.Usage = func() { fmt.Fprint(os.Stderr, usage) }
	var nd, nf int
	var dirs = []string{"."}
	flag.Parse()
	// Make it work with leading dirs
	if args := flag.Args(); len(args) > 0 {
		dirs = args
	}
	// Output file
	var outFile = os.Stdout
	var err error
	if *o != "" {
		outFile, err = os.Create(*o)
		if err != nil {
			errAndExit(err)
		}
	}
	defer outFile.Close()
	// Check sort-type
	if *sort != "" {
		switch *sort {
		case "version", "mtime", "ctime", "name", "size":
		default:
			msg := fmt.Sprintf("sort type '%s' not valid, should be one of: "+
				"name,version,size,mtime,ctime", *sort)
			errAndExit(errors.New(msg))
		}
	}
	// Set options
	opts := &tree.Options{
		// Required
		Fs:      new(fs),
		OutFile: outFile,
		// List
		All:        *a,
		DirsOnly:   *d,
		FullPath:   *f,
		DeepLevel:  *L,
		FollowLink: *l,
		Pattern:    *P,
		IPattern:   *I,
		IgnoreCase: *ignorecase,
		// Files
		ByteSize: *s,
		UnitSize: *h,
		FileMode: *p,
		ShowUid:  *u,
		ShowGid:  *g,
		LastMod:  *D,
		Quotes:   *Q,
		Inodes:   *inodes,
		Device:   *device,
		// Sort
		NoSort:    *U,
		ReverSort: *r,
		DirSort:   *dirsfirst,
		VerSort:   *v || *sort == "version",
		ModSort:   *t || *sort == "mtime",
		CTimeSort: *c || *sort == "ctime",
		NameSort:  *sort == "name",
		SizeSort:  *sort == "size",
		// Graphics
		NoIndent: *i,
		Colorize: *C,
	}
	for _, dir := range dirs {
		inf := tree.New(dir)
		if d, f := inf.Visit(opts); f != 0 {
			if d > 0 {
				d -= 1
			}
			nd, nf = nd+d, nf+f
		}
		inf.Print(opts)
	}
	// Print footer report
	if !*noreport {
		footer := fmt.Sprintf("\n%d directories", nd)
		if !opts.DirsOnly {
			footer += fmt.Sprintf(", %d files", nf)
		}
		fmt.Fprintln(outFile, footer)
	}
}