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

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/cyverse/go-irodsclient/fs"
    10  	"github.com/cyverse/go-irodsclient/irods/types"
    11  	"github.com/cyverse/go-irodsclient/irods/util"
    12  
    13  	log "github.com/sirupsen/logrus"
    14  )
    15  
    16  func main() {
    17  	logger := log.WithFields(log.Fields{
    18  		"package":  "main",
    19  		"function": "main",
    20  	})
    21  
    22  	// Parse cli parameters
    23  	flag.Parse()
    24  	args := flag.Args()
    25  
    26  	if len(args) != 2 {
    27  		fmt.Fprintf(os.Stderr, "Give an iRODS source path and a local destination path!\n")
    28  		os.Exit(1)
    29  	}
    30  
    31  	srcPath := args[0]
    32  	destPath := args[1]
    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 := "download"
    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  	// convert dest path into absolute path
    60  	destPath, err = filepath.Abs(destPath)
    61  	if err != nil {
    62  		logger.Error(err)
    63  		panic(err)
    64  	}
    65  
    66  	err = filesystem.DownloadFileResumable(srcPath, "", destPath, nil)
    67  	if err != nil {
    68  		logger.Error(err)
    69  		panic(err)
    70  	}
    71  
    72  	fsinfo, err := os.Stat(destPath)
    73  	if err != nil {
    74  		logger.Error(err)
    75  		panic(err)
    76  	}
    77  
    78  	if fsinfo.IsDir() {
    79  		// dir
    80  		srcFileName := util.GetIRODSPathFileName(srcPath)
    81  		destFilePath := util.MakeIRODSPath(destPath, srcFileName)
    82  
    83  		fsinfo2, err := os.Stat(destFilePath)
    84  		if err != nil {
    85  			logger.Error(err)
    86  			panic(err)
    87  		}
    88  
    89  		if !fsinfo2.IsDir() {
    90  			fmt.Printf("Successfully downloaded a file %s to %s, size = %d\n", srcPath, destPath, fsinfo2.Size())
    91  		} else {
    92  			logger.Errorf("Unkonwn file type - %s", fsinfo2.Mode())
    93  		}
    94  	} else {
    95  		fmt.Printf("Successfully downloaded a file %s to %s, size = %d\n", srcPath, destPath, fsinfo.Size())
    96  	}
    97  }