package main
import (
"flag"
"fmt"
"bufio"
"os"
"strings"
"io"
)
func FileExists(dst string)bool{
_,err := os.Stat(dst);
return err == nil || os.IsExist(err);
}
func copyFile(src,dst string) (w int64, err error){
srcFile,err := os.Open(src);
if err != nil {
fmt.Println(err)
}
defer srcFile.Close();
dstFile,err := os.Create(dst)
if err != nil{
fmt.Println(err);
}
return io.Copy(dstFile,srcFile);
}
func copyfile(src, dst string, force,showdetail bool){
if !force {
if(FileExists(dst)){
fmt.Println("exist override? y/n",dst)
reader := bufio.NewReader(os.Stdin);
data,_ ,_ := reader.ReadLine()
if strings.TrimSpace(string(data)) != "y" {
return
}
}
}
copyFile(src,dst)
if showdetail {
fmt.Println("copy success");
}
}
func main(){
var force, showdetail bool
flag.BoolVar(&force, "f", false, "force copy when existing");
flag.BoolVar(&showdetail, "v", false, "remain copy when existing")
flag.Parse();
if flag.NArg() < 2 {
flag.Usage()
return
}
fmt.Println(force)
copyfile(flag.Arg(0),flag.Arg(1),force,showdetail);
}