CopyFile copies a file from the src to dest
(src, dest string)
| 119 | |
| 120 | // CopyFile copies a file from the src to dest |
| 121 | func CopyFile(src, dest string) error { |
| 122 | in, err := os.Open(src) |
| 123 | if err != nil { |
| 124 | return errors.Wrap(err, "opening the input file") |
| 125 | } |
| 126 | defer in.Close() |
| 127 | |
| 128 | out, err := os.Create(dest) |
| 129 | if err != nil { |
| 130 | return errors.Wrap(err, "creating the output file") |
| 131 | } |
| 132 | |
| 133 | if _, err = io.Copy(out, in); err != nil { |
| 134 | return errors.Wrap(err, "copying the file content") |
| 135 | } |
| 136 | |
| 137 | if err = out.Sync(); err != nil { |
| 138 | return errors.Wrap(err, "flushing the output file to disk") |
| 139 | } |
| 140 | |
| 141 | fi, err := os.Stat(src) |
| 142 | if err != nil { |
| 143 | return errors.Wrap(err, "getting the file info for the input file") |
| 144 | } |
| 145 | |
| 146 | if err = os.Chmod(dest, fi.Mode()); err != nil { |
| 147 | return errors.Wrap(err, "copying permission to the output file") |
| 148 | } |
| 149 | |
| 150 | // Close the output file |
| 151 | if err = out.Close(); err != nil { |
| 152 | return errors.Wrap(err, "closing the output file") |
| 153 | } |
| 154 | |
| 155 | return nil |
| 156 | } |
no test coverage detected