github.com/cyverse/go-irodsclient@v0.13.2/examples/delete_dir/delete_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  	recurse := false
    21  	// Parse cli parameters
    22  	flag.BoolVar(&recurse, "recurse", false, "recursive")
    23  	flag.BoolVar(&recurse, "r", false, "recursive")
    24  	flag.Parse()
    25  	args := flag.Args()
    26  
    27  	if len(args) != 1 {
    28  		fmt.Fprintf(os.Stderr, "Give an iRODS path!\n")
    29  		os.Exit(1)
    30  	}
    31  
    32  	inputPath := args[0]
    33  
    34  	// Read account configuration from YAML file
    35  	yaml, err := os.ReadFile("account.yml")
    36  	if err != nil {
    37  		logger.Error(err)
    38  		panic(err)
    39  	}
    40  
    41  	account, err := types.CreateIRODSAccountFromYAML(yaml)
    42  	if err != nil {
    43  		logger.Error(err)
    44  		panic(err)
    45  	}
    46  
    47  	logger.Debugf("Account : %v", account.MaskSensitiveData())
    48  
    49  	// Create a file system
    50  	appName := "delete_dir"
    51  	filesystem, err := fs.NewFileSystemWithDefault(account, appName)
    52  	if err != nil {
    53  		logger.Error(err)
    54  		panic(err)
    55  	}
    56  
    57  	defer filesystem.Release()
    58  
    59  	err = filesystem.RemoveDir(inputPath, recurse, true)
    60  	if err != nil {
    61  		logger.Error(err)
    62  		panic(err)
    63  	}
    64  
    65  	if !filesystem.ExistsDir(inputPath) {
    66  		fmt.Printf("Successfully deleted dir %s\n", inputPath)
    67  	} else {
    68  		fmt.Printf("Could not delete dir %s\n", inputPath)
    69  	}
    70  }