1 package org.kuali.common.util;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 public class UnixCmds {
8 private static final String SU = "su";
9 private static final String MKDIR = "mkdir";
10 private static final String RM = "rm";
11 private static final String CHOWN = "chown";
12 private static final String CHMOD = "chmod";
13
14 public String chmod(String mode, String path) {
15 Assert.notBlank(path);
16 return chmod(mode, Collections.singletonList(path));
17 }
18
19 public String chmod(String mode, List<String> paths) {
20 return chmod(null, mode, paths);
21
22 }
23
24 public String chmod(List<String> options, String mode, List<String> paths) {
25 Assert.hasLength(mode);
26 Assert.notEmpty(paths);
27 return cmd(CHMOD, CollectionUtils.combineStrings(options, mode, paths));
28 }
29
30 public String mkdirp(String path) {
31 Assert.notBlank(path);
32 return mkdirp(null, Collections.singletonList(path));
33 }
34
35 public String mkdirp(List<String> paths) {
36 return mkdirp(null, paths);
37 }
38
39 public String mkdirp(List<String> options, List<String> paths) {
40 List<String> parents = Arrays.asList("-p");
41 if (options == null) {
42 return mkdir(parents, paths);
43 } else {
44 return mkdir(CollectionUtils.combineStringsUniquely(options, parents), paths);
45 }
46
47 }
48
49 public String mkdir(String path) {
50 Assert.notBlank(path);
51 return mkdir(null, Collections.singletonList(path));
52 }
53
54 public String mkdir(List<String> options, List<String> paths) {
55 Assert.notEmpty(paths);
56 return cmd(MKDIR, CollectionUtils.combineStrings(options, paths));
57 }
58
59 public String su(String login, String command) {
60 return su(null, login, command);
61 }
62
63 public String su(List<String> options, String login, String command) {
64 return su(options, login, Arrays.asList("--command", command));
65 }
66
67 public String su(List<String> options, String login, List<String> args) {
68 List<String> list2 = login == null ? null : Arrays.asList("-", login);
69 return cmd(SU, CollectionUtils.combineStrings(options, list2, args));
70 }
71
72 public String rmrf(List<String> paths) {
73 return rmrf(null, paths);
74 }
75
76 public String rmrf(String path) {
77 Assert.notBlank(path);
78 return rmrf(null, Collections.singletonList(path));
79 }
80
81 public String rmrf(List<String> options, List<String> paths) {
82 List<String> recursiveSilent = Arrays.asList("-r", "-f");
83 if (options == null) {
84 return rm(recursiveSilent, paths);
85 } else {
86 return rm(CollectionUtils.combineStringsUniquely(options, recursiveSilent), paths);
87 }
88 }
89
90 public String rm(List<String> paths) {
91 return rm(null, paths);
92 }
93
94 public String rm(String path) {
95 Assert.notBlank(path);
96 return rm(null, Collections.singletonList(path));
97 }
98
99 public String rm(List<String> options, List<String> paths) {
100 Assert.notEmpty(paths);
101 return cmd(RM, CollectionUtils.combineStrings(options, paths));
102 }
103
104 public String chownr(String owner, String group, String path) {
105 Assert.notBlank(path);
106 return chownr(owner, group, Collections.singletonList(path));
107 }
108
109 public String chownr(String owner, String group, List<String> paths) {
110 return chownr(null, owner, group, paths);
111 }
112
113 public String chownr(List<String> options, String owner, String group, List<String> paths) {
114 List<String> recursive = Arrays.asList("-R");
115 if (options == null) {
116 return chown(recursive, owner, group, paths);
117 } else {
118 return chown(CollectionUtils.combineStringsUniquely(options, recursive), owner, group, paths);
119 }
120 }
121
122 public String chown(List<String> options, String owner, String group, String path) {
123 Assert.notBlank(path);
124 return chown(options, owner, group, Collections.singletonList(path));
125 }
126
127 public String chown(List<String> options, String owner, String group, List<String> paths) {
128 Assert.notEmpty(paths);
129 Assert.notBlank(owner, group);
130 return cmd(CHOWN, CollectionUtils.combineStrings(options, owner + ":" + group, paths));
131 }
132
133 public String chown(String owner, String group, String path) {
134 Assert.notBlank(path);
135 return chown(null, owner, group, Collections.singletonList(path));
136 }
137
138 public String cmd(String executable, List<String> args) {
139 StringBuilder sb = new StringBuilder();
140 sb.append(executable);
141 if (!CollectionUtils.isEmpty(args)) {
142 sb.append(" ");
143 sb.append(CollectionUtils.getSpaceSeparatedString(args));
144 }
145 return sb.toString();
146 }
147
148 }