copy

package
v0.0.0-...-81a2a17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2020 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CopyCmd = &cobra.Command{
	Use:     "copy",
	Short:   "copies batch of file to a new location",
	Long:    "given a csv with header 'Ur-Pfad+Ur-Datei' and 'Zielpfad' this tool will copy all named files",
	Args:    cobra.ExactArgs(1),
	Example: "copy some.csv",
	Run: func(cmd *cobra.Command, args []string) {
		path := args[0]

		f, err := os.Open(path)
		if err != nil {
			fmt.Printf("could not find csv: %s\n", path)
			os.Exit(1)
		}
		r := transform.NewReader(f, charmap.ISO8859_1.NewDecoder())

		var copies []FileMove
		err = csvtag.LoadFromReader(r, &copies, csvtag.CsvOptions{Separator: rune(viper.GetString("seperator")[0])})
		if err != nil {
			fmt.Printf("error reading csv file %q: %s\n", path, err)
		}

		var missing []FileMove
		var existing []FileMove
		if viper.GetBool("verbose") {
			fmt.Println("looking for these files:")
		}
		for _, r := range copies {
			if viper.GetBool("verbose") {
				fmt.Println(r.Data)
			}
			_, err := os.Stat(r.Data)
			if err != nil {
				missing = append(missing, r)
				continue
			}

			existing = append(existing, r)
		}

		fmt.Printf("found [%d/%d] files\n", len(existing), len(copies))

		if viper.GetBool("verbose") {
			if len(missing) > 0 {
				fmt.Println("missing files:")
				for _, m := range missing {
					fmt.Printf("\t%s\n", m.Data)
				}
			}
		}

		fmt.Printf("start copying ? (y|N): ")
		var y string
		_, err = fmt.Scanln(&y)
		if err != nil || y != "y" {
			fmt.Println("aborting")
			os.Exit(0)
		}

		errors := make(map[string]error)
		for _, e := range existing {
			err := os.MkdirAll(e.CopyPath, os.ModePerm)
			if err != nil {
				errors[e.Data] = err
				continue
			}

			if viper.GetBool("no-copy") {
				continue
			}

			f, err := os.Open(e.Data)
			if err != nil {
				errors[e.Data] = err
				continue
			}

			dst, err := os.Create(e.CopyPath + "/" + filepath.Base(e.Data))
			if err != nil {
				errors[e.Data] = err
				continue
			}

			_, err = io.Copy(dst, f)
			if err != nil {
				errors[e.Data] = err
				continue
			}
		}

		if len(errors) > 0 {
			fmt.Println("copy errors:")
			for f, err := range errors {
				fmt.Println(">>")
				fmt.Printf("File:\t%s\n", f)
				fmt.Printf("Error:\t%s\n", err)
			}
		}
	},
}

Functions

This section is empty.

Types

type FileMove

type FileMove struct {
	Data     string `csv:"Ur-Pfad+Ur-Datei"`
	CopyPath string `csv:"Zielpfad"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL