1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/cfg/copy.go
Chris Cummer 0e141e03c8 Closes #217. Use XDG-compatible config directory
This change is largely experimental and it's entirely possible it could
wipe out your existing configuration. Be warned.

Old config path was: ~/.wtf/
New config path is:  ~/.config/wtf/

If an existing config directory already exists, this change attempts to
copy it to the new location.

Note that if your config file contains paths to files in the old config
directory, they won't work. You'll need to change them by hand.
2018-06-20 16:46:02 -07:00

76 lines
1.2 KiB
Go

// Copied verbatim from:
//
// https://github.com/otiai10/copy/blob/master/copy.go
package cfg
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
// Copy copies src to dest, doesn't matter if src is a directory or a file
func Copy(src, dest string) error {
info, err := os.Stat(src)
if err != nil {
return err
}
return copy(src, dest, info)
}
// "info" must be given here, NOT nil.
func copy(src, dest string, info os.FileInfo) error {
if info.IsDir() {
return dcopy(src, dest, info)
}
return fcopy(src, dest, info)
}
func fcopy(src, dest string, info os.FileInfo) error {
f, err := os.Create(dest)
if err != nil {
return err
}
defer f.Close()
if err = os.Chmod(f.Name(), info.Mode()); err != nil {
return err
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
_, err = io.Copy(f, s)
return err
}
func dcopy(src, dest string, info os.FileInfo) error {
if err := os.MkdirAll(dest, info.Mode()); err != nil {
return err
}
infos, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, info := range infos {
if err := copy(
filepath.Join(src, info.Name()),
filepath.Join(dest, info.Name()),
info,
); err != nil {
return err
}
}
return nil
}