github.com/cyverse/go-irodsclient@v0.13.2/examples/delete_file/delete_file.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 := "delete_file"
    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  	err = filesystem.RemoveFile(inputPath, true)
    57  	if err != nil {
    58  		logger.Error(err)
    59  		panic(err)
    60  	}
    61  
    62  	if !filesystem.ExistsFile(inputPath) {
    63  		fmt.Printf("Successfully deleted file %s\n", inputPath)
    64  	} else {
    65  		fmt.Printf("Could not delete file %s\n", inputPath)
    66  	}
    67  }