8 files changed +160
-0
lines changed Original file line number Diff line number Diff line change @@ -1069,6 +1069,35 @@ def template(
1069
1069
)
1070
1070
1071
1071
1072
+ @operation ()
1073
+ def move (src : str , dest : str , overwrite = False ):
1074
+ """
1075
+ Move remote file/directory/link into remote directory
1076
+
1077
+ + src: remote file/directory to move
1078
+ + dest: remote directory to move `src` into
1079
+ + overwrite: whether to overwrite dest, if present
1080
+ """
1081
+
1082
+ if host .get_fact (File , src ) is None :
1083
+ raise OperationError ("src {0} does not exist" .format (src ))
1084
+
1085
+ if not host .get_fact (Directory , dest ):
1086
+ raise OperationError ("dest {0} is not an existing directory" .format (dest ))
1087
+
1088
+ full_dest_path = os .path .join (dest , os .path .basename (src ))
1089
+ if host .get_fact (File , full_dest_path ) is not None :
1090
+ if overwrite :
1091
+ yield StringCommand ("rm" , "-rf" , QuoteString (full_dest_path ))
1092
+ else :
1093
+ raise OperationError (
1094
+ "dest {0} already exists and `overwrite` is unset" .format (full_dest_path )
1095
+ )
1096
+
1097
+ yield StringCommand ("mv" , QuoteString (src ), QuoteString (dest ))
1098
+ changed = True
1099
+
1100
+
1072
1101
def _validate_path (path ):
1073
1102
try :
1074
1103
return os .fspath (path )
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "facts" : {
4
+ "files.File" : {
5
+ "path=tmp/testfile" : false ,
6
+ "path=tmp2/testfile" : null
7
+ },
8
+ "files.Directory" : {
9
+ "path=tmp/testfile" : {
10
+ "mode" : 700
11
+ },
12
+ "path=tmp2" : {
13
+ "mode" : 700
14
+ }
15
+ }
16
+ },
17
+ "commands" : [" mv tmp/testfile tmp2" ]
18
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "facts" : {
4
+ "files.File" : {
5
+ "path=tmp/testfile" : {
6
+ "mode" : 600
7
+ }
8
+ },
9
+ "files.Directory" : {
10
+ "path=tmp2" : null
11
+ }
12
+ },
13
+ "exception" : {
14
+ "name" : " OperationError" ,
15
+ "message" : " dest tmp2 is not an existing directory"
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "facts" : {
4
+ "files.File" : {
5
+ "path=tmp/testfile" : null
6
+ },
7
+ "files.Directory" : {
8
+ "path=tmp2" : {
9
+ "mode" : 700
10
+ }
11
+ }
12
+ },
13
+ "exception" : {
14
+ "name" : " OperationError" ,
15
+ "message" : " src tmp/testfile does not exist"
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "facts" : {
4
+ "files.File" : {
5
+ "path=tmp/testfile" : false ,
6
+ "path=tmp2/testfile" : null
7
+ },
8
+ "files.Link" : {
9
+ "path=tmp/testfile" : {
10
+ "mode" : 600
11
+ }
12
+ },
13
+ "files.Directory" : {
14
+ "path=tmp2" : {
15
+ "mode" : 700
16
+ }
17
+ }
18
+ },
19
+ "commands" : [" mv tmp/testfile tmp2" ]
20
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "facts" : {
4
+ "files.File" : {
5
+ "path=tmp/testfile" : {
6
+ "mode" : 600
7
+ },
8
+ "path=tmp2/testfile" : null
9
+ },
10
+ "files.Directory" : {
11
+ "path=tmp2" : {
12
+ "mode" : 700
13
+ }
14
+ }
15
+ },
16
+ "commands" : [" mv tmp/testfile tmp2" ]
17
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "facts" : {
4
+ "files.File" : {
5
+ "path=tmp/testfile" : {
6
+ "mode" : 600
7
+ },
8
+ "path=tmp2/testfile" : {
9
+ "mode" : 600
10
+ }
11
+ },
12
+ "files.Directory" : {
13
+ "path=tmp2" : {
14
+ "mode" : 700
15
+ }
16
+ }
17
+ },
18
+ "exception" : {
19
+ "name" : " OperationError" ,
20
+ "message" : " dest tmp2/testfile already exists and `overwrite` is unset"
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "args" : [" tmp/testfile" , " tmp2" ],
3
+ "kwargs" : {"overwrite" : true },
4
+ "facts" : {
5
+ "files.File" : {
6
+ "path=tmp/testfile" : {
7
+ "mode" : 600
8
+ },
9
+ "path=tmp2/testfile" : {
10
+ "mode" : 600
11
+ }
12
+ },
13
+ "files.Directory" : {
14
+ "path=tmp2" : {
15
+ "mode" : 700
16
+ }
17
+ }
18
+ },
19
+ "commands" : [" rm -rf tmp2/testfile" , " mv tmp/testfile tmp2" ]
20
+ }
0 commit comments