package main import ( "errors" "io" "os" "path/filepath" ) func adopt(conf *Conf, args []string) error { if len(args) < 1 { return errors.New("must specify at least one file to adopt") } for _, name := range args { if err := adoptFile(conf, name); err != nil { return err } } return nil } func adoptFile(conf *Conf, name string) error { fromPath := filepath.Join(conf.Dest, name) toPath := filepath.Join(conf.Source, "templates", name) if err := os.MkdirAll(filepath.Dir(toPath), 0700); err != nil { return err } from, err := os.Open(fromPath) if err != nil { return err } defer from.Close() fromInfo, err := os.Stat(fromPath) if err != nil { return err } to, err := os.OpenFile(toPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fromInfo.Mode()) if err != nil { return err } defer to.Close() if _, err = io.Copy(to, from); err != nil { return err } return nil }