aboutsummaryrefslogtreecommitdiff
path: root/adopt.go
blob: 9be0a7712ee35985f18d375fd74add909e2d44fc (plain)
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
package main

import (
	"errors"
	"io"
	"os"
	"path/filepath"
)

func adopt(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(name); err != nil {
			return err
		}
	}
	return nil
}

func adoptFile(name string) error {
	fromPath := filepath.Join(dest, name)
	toPath := filepath.Join(base, "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()
	to, err := os.OpenFile(toPath, os.O_CREATE|os.O_WRONLY, 0600)
	if err != nil {
		return err
	}
	defer to.Close()
	if _, err = io.Copy(to, from); err != nil {
		return err
	}
	return nil
}