1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package main
import ( "bufio" "flag" "fmt" "io" "os" "strings" )
func fileExists(filename string) bool { _, err := os.Stat(filename) return err == nil || os.IsExist(err) }
func copyFileAction(src, dst string, showProgress, force bool) { if !force { if fileExists(dst) { fmt.Println("%s exists override ? y/n\n", dst) reader := bufio.NewReader(os.Stdin) data, _, _ := reader.ReadLine()
if strings.TrimSpace(string(data)) != "y" { return } } } copyFile(src, dst)
if showProgress { fmt.Println("'%s' -> '%s'\n", src, dst) } }
func copyFile(src, dst string) (w int64, err error) { srcFile, err := os.Open(src) if err != nil { fmt.Println(err.Error()) return }
defer srcFile.Close()
dstFile, err := os.Create(dst) if err != nil { fmt.Println(err.Error()) return }
defer dstFile.Close()
return io.Copy(dstFile, srcFile) }
func main() { var showProgress, force bool
flag.BoolVar(&force, "f", false, "force copy when existing") flag.BoolVar(&showProgress, "v", false, "explain what is being done")
flag.Parse()
if flag.NArg() < 2 { flag.Usage() return }
copyFileAction(flag.Arg(0), flag.Arg(1), showProgress, force) }
|