github.com/pluswing/datasync@v1.1.1-0.20240218052257-9077f6fc4ae3/dump/dump_file/file.go (about)

     1  package dump_file
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/briandowns/spinner"
    10  	cp "github.com/otiai10/copy"
    11  	"github.com/pluswing/datasync/data"
    12  	"github.com/pluswing/datasync/file"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func Dump(dumpDir string, cfg data.TargetFileType) {
    17  	s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
    18  	s.Suffix = fmt.Sprintf(" dump file(s) ... (path: %s)", cfg.Path)
    19  	s.Start()
    20  
    21  	cwd, err := file.FindCurrentDir()
    22  	cobra.CheckErr(err)
    23  	src := filepath.Join(cwd, cfg.Path)
    24  	stat, err := os.Stat(src)
    25  	cobra.CheckErr(err)
    26  	dest := filepath.Join(dumpDir, cfg.Path)
    27  	destDir := dest
    28  	if !stat.IsDir() {
    29  		destDir = filepath.Dir(dest)
    30  	}
    31  	err = os.MkdirAll(destDir, os.ModePerm)
    32  	cobra.CheckErr(err)
    33  	err = cp.Copy(src, dest)
    34  	cobra.CheckErr(err)
    35  
    36  	s.Stop()
    37  	fmt.Printf("✔︎ dump file(s) completed. (path: %s)", cfg.Path)
    38  }
    39  
    40  func Expand(dumpDir string, cfg data.TargetFileType) {
    41  	s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
    42  	s.Suffix = fmt.Sprintf(" restore file(s) ... (path: %s)\n", cfg.Path)
    43  	s.Start()
    44  
    45  	cwd, err := file.FindCurrentDir()
    46  	cobra.CheckErr(err)
    47  	src := filepath.Join(dumpDir, cfg.Path)
    48  	stat, err := os.Stat(src)
    49  	cobra.CheckErr(err)
    50  	dest := filepath.Join(cwd, cfg.Path)
    51  	destDir := dest
    52  	if !stat.IsDir() {
    53  		destDir = filepath.Dir(dest)
    54  	}
    55  	err = os.MkdirAll(destDir, os.ModePerm)
    56  	cobra.CheckErr(err)
    57  	err = cp.Copy(src, dest)
    58  	cobra.CheckErr(err)
    59  
    60  	s.Stop()
    61  	fmt.Printf("✔︎ restore file(s) completed. (path: %s)\n", cfg.Path)
    62  }