github.com/cyverse/go-irodsclient@v0.13.2/examples/list_dir/list_dir.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/cyverse/go-irodsclient/fs"
     9  	"github.com/cyverse/go-irodsclient/irods/types"
    10  
    11  	log "github.com/sirupsen/logrus"
    12  )
    13  
    14  func main() {
    15  	logger := log.WithFields(log.Fields{
    16  		"package":  "main",
    17  		"function": "main",
    18  	})
    19  
    20  	// Parse cli parameters
    21  	flag.Parse()
    22  	args := flag.Args()
    23  
    24  	if len(args) != 1 {
    25  		fmt.Fprintf(os.Stderr, "Give an iRODS path!\n")
    26  		os.Exit(1)
    27  	}
    28  
    29  	inputPath := args[0]
    30  
    31  	// Read account configuration from YAML file
    32  	yaml, err := os.ReadFile("account.yml")
    33  	if err != nil {
    34  		logger.Error(err)
    35  		panic(err)
    36  	}
    37  
    38  	account, err := types.CreateIRODSAccountFromYAML(yaml)
    39  	if err != nil {
    40  		logger.Error(err)
    41  		panic(err)
    42  	}
    43  
    44  	logger.Debugf("Account : %v", account.MaskSensitiveData())
    45  
    46  	// Create a file system
    47  	appName := "list_dir"
    48  	filesystem, err := fs.NewFileSystemWithDefault(account, appName)
    49  	if err != nil {
    50  		logger.Error(err)
    51  		panic(err)
    52  	}
    53  
    54  	defer filesystem.Release()
    55  
    56  	entries, err := filesystem.List(inputPath)
    57  	if err != nil {
    58  		logger.Error(err)
    59  		panic(err)
    60  	}
    61  
    62  	if len(entries) == 0 {
    63  		fmt.Printf("Found no entries in the directory - %s\n", inputPath)
    64  	} else {
    65  		fmt.Printf("DIR: %s\n", inputPath)
    66  		for _, entry := range entries {
    67  			if entry.Type == fs.FileEntry {
    68  				fmt.Printf("> FILE:\t%d\t%s\t%d\n", entry.ID, entry.Path, entry.Size)
    69  			} else {
    70  				// dir
    71  				fmt.Printf("> DIRECTORY:\t%d\t%s\n", entry.ID, entry.Path)
    72  			}
    73  
    74  		}
    75  	}
    76  }