-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
157 lines (128 loc) · 4.18 KB
/
move.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (c) 2021 The cirocket Authors (Neil Hemming)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package builtin
import (
"context"
"fmt"
"os"
"github.com/mitchellh/mapstructure"
"github.com/nehemming/cirocket/pkg/loggee"
"github.com/nehemming/cirocket/pkg/rocket"
"github.com/pkg/errors"
)
type (
// Move task is used to move files. The tool can recursively move files.
// Directories cannot be specified as a source, the wild card ** pattern can
// be used to recursively move the contents of the directory.
// The directory structure is moved, but the source sub directories are not deleted.
Move struct {
Sources []string `mapstructure:"sources"`
Destination string `mapstructure:"destination"`
Overwrite string `mapstructure:"overwrite"`
Log bool `mapstructure:"log"`
}
moveType struct{}
)
func (moveType) Type() string {
return "move"
}
func (moveType) Description() string {
return "moves files matching the source glob specs to the destination folder."
}
// Prepare loads the tasks configuration and returns the operation function or an error.
func (moveType) Prepare(ctx context.Context, capComm *rocket.CapComm, task rocket.Task) (rocket.ExecuteFunc, error) {
moveCfg := &Move{}
if err := mapstructure.WeakDecode(task.Definition, moveCfg); err != nil {
return nil, errors.Wrap(err, "parsing template type")
}
fn := func(execCtx context.Context) error {
// get the destination
dest, err := capComm.ExpandString(ctx, "dir", moveCfg.Destination)
if err != nil {
return errors.Wrapf(err, "expanding dest %s", moveCfg.Destination)
}
// get the overrite value
overwrite, err := capComm.ExpandBool(ctx, "overrite", moveCfg.Overwrite)
if err != nil {
return errors.Wrapf(err, "expanding dest %s", moveCfg.Destination)
}
// get the destination spec from the raw path entered
destSpec, err := getDestSpec(dest)
if err != nil {
return errors.Wrapf(err, "dest %s", moveCfg.Destination)
}
// Expand files
rawSpecs := make([]string, 0, len(moveCfg.Sources))
for index, f := range moveCfg.Sources {
rawSpec, err := capComm.ExpandString(ctx, "source", f)
if err != nil {
return errors.Wrapf(err, "expanding file %d", index)
}
rawSpecs = append(rawSpecs, rawSpec)
}
// glob the files
files, err := globFileAbsRel(rawSpecs...)
if err != nil {
return err
}
// copy
return moveFiles(execCtx, files, destSpec, overwrite, getLogFromCapComm(capComm, moveCfg.Log))
}
return fn, nil
}
func moveFiles(ctx context.Context, sources []absRel, dest destSpec, allowOverwrite bool, log loggee.Logger) error {
for _, source := range sources {
if ctx.Err() != nil {
return ctx.Err()
}
if err := moveFile(source, dest, allowOverwrite, log); err != nil {
return err
}
}
return nil
}
func moveFile(source absRel, dest destSpec, allowOverwrite bool, log loggee.Logger) error {
// Get the source files permission
stat, err := os.Stat(source.Abs)
if err != nil {
return errors.Wrapf(err, "stat %s:", source.Abs)
}
if stat.IsDir() {
// cannot move dirs, move as dir/**
return fmt.Errorf("%s is a directory and cannot be moved, replace source with %[1]s/**", source.Rel)
}
destAbsRel, err := prepDestination(source, dest, allowOverwrite)
if err != nil {
return err
}
if destAbsRel == nil || source.Abs == destAbsRel.Abs {
// skipping
if log != nil {
log.Infof("skipping %s", source.Rel)
}
return nil
}
// Do the move
err = os.Rename(source.Abs, destAbsRel.Abs)
if err != nil {
return errors.Wrapf(err, "move %s => %s:", source.Rel, destAbsRel.Rel)
}
// log
if log != nil {
log.Infof("move %s => %s", source.Rel, destAbsRel.Rel)
}
return nil
}
func init() {
rocket.Default().RegisterTaskTypes(moveType{})
}