-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestore.go
More file actions
39 lines (32 loc) · 919 Bytes
/
restore.go
File metadata and controls
39 lines (32 loc) · 919 Bytes
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
package dl
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
var _ cmd = (*restoreCmd)(nil)
type restoreCmd struct{}
func newRestoreCmd() *restoreCmd {
return &restoreCmd{}
}
// restoreCmd restores raw files from .dl directory
func (r *restoreCmd) Run(ctx context.Context, baseDir string) error {
dlDirPath := filepath.Join(baseDir, dlDir)
if _, err := os.Stat(dlDirPath); os.IsNotExist(err) {
return fmt.Errorf(".dl directory doesn't exist. Please execute $ dl init .: %s", dlDirPath)
}
// check files under .dl directory recursively
return walkDirWithValidation(ctx, baseDir, func(ctx context.Context, path string, info fs.DirEntry) error {
idx := strings.Index(path, dlDir)
if idx < 0 {
return nil
}
// copies ".go" files to raw places.
dstFilePath := path[:idx] + path[idx+len(dlDir)+1:]
srcFilePath := path
return copyFile(ctx, dstFilePath, srcFilePath)
})
}