001/**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.List;
022
023public class UnixCmds {
024        private static final String SU = "su";
025        private static final String MKDIR = "mkdir";
026        private static final String RM = "rm";
027        private static final String CHOWN = "chown";
028        private static final String CHMOD = "chmod";
029        private static final String HOSTNAME = "hostname";
030        private static final String PS = "ps";
031        private static final String KILL = "kill";
032        private static final String NOHUP = "nohup";
033        private static final String CP = "cp";
034
035        public String cp(String src, String dst) {
036                return cp(null, src, dst, false);
037        }
038
039        public String cp(String src, String dst, boolean bypassAnyAliases) {
040                return cp(null, src, dst, bypassAnyAliases);
041        }
042
043        public String cp(List<String> options, String src, String dst, boolean bypassAnyAliases) {
044                Assert.hasText(src, "src has no text");
045                Assert.hasText(dst, "dst has no text");
046                List<String> args = new ArrayList<String>();
047                args.addAll(CollectionUtils.toEmptyList(options));
048                args.add(src);
049                args.add(dst);
050                return cmd(CP, args, bypassAnyAliases);
051        }
052
053        public String nohup(String command) {
054                return nohup(command, new ArrayList<String>());
055        }
056
057        public String nohup(String command, List<String> nohupArgs) {
058                Assert.hasText(command);
059                List<String> args = new ArrayList<String>();
060                args.add(command);
061                args.addAll(nohupArgs);
062                return cmd(NOHUP, args);
063        }
064
065        public String kill(int pid) {
066                return kill(Arrays.asList(pid));
067        }
068
069        public String kill(List<Integer> pids) {
070                Assert.notEmpty(pids);
071                List<String> args = new ArrayList<String>();
072                for (Integer pid : pids) {
073                        args.add(pid.toString());
074                }
075                return cmd(KILL, args);
076        }
077
078        public String ps() {
079                return cmd(PS);
080        }
081
082        public String ps(String user) {
083                Assert.hasText(user);
084                return cmd(PS, Arrays.asList("-u", user));
085        }
086
087        public String psf(String user) {
088                Assert.hasText(user);
089                List<String> args = new ArrayList<String>();
090                args.add("-u");
091                args.add(user);
092                args.add("-f");
093                return cmd(PS, args);
094        }
095
096        /**
097         * Returns the current hostname
098         */
099        public String hostname() {
100                return cmd(HOSTNAME);
101        }
102
103        /**
104         * Sets the current hostname
105         */
106        public String hostname(String hostname) {
107                return cmd(HOSTNAME, Arrays.asList(hostname));
108        }
109
110        public String chmod(String mode, String path) {
111                Assert.notBlank(path);
112                return chmod(mode, Collections.singletonList(path));
113        }
114
115        public String chmod(String mode, List<String> paths) {
116                return chmod(null, mode, paths);
117
118        }
119
120        public String chmod(List<String> options, String mode, List<String> paths) {
121                Assert.hasLength(mode);
122                Assert.notEmpty(paths);
123                return cmd(CHMOD, CollectionUtils.combineStrings(options, mode, paths));
124        }
125
126        public String mkdirp(String path) {
127                Assert.notBlank(path);
128                return mkdirp(null, Collections.singletonList(path));
129        }
130
131        public String mkdirp(List<String> paths) {
132                return mkdirp(null, paths);
133        }
134
135        public String mkdirp(List<String> options, List<String> paths) {
136                List<String> parents = Arrays.asList("-p");
137                if (options == null) {
138                        return mkdir(parents, paths);
139                } else {
140                        return mkdir(CollectionUtils.combineStringsUniquely(options, parents), paths);
141                }
142
143        }
144
145        public String mkdir(String path) {
146                Assert.notBlank(path);
147                return mkdir(null, Collections.singletonList(path));
148        }
149
150        public String mkdir(List<String> options, List<String> paths) {
151                Assert.notEmpty(paths);
152                return cmd(MKDIR, CollectionUtils.combineStrings(options, paths));
153        }
154
155        public String su(String login, String command) {
156                return su(null, login, command);
157        }
158
159        public String su(List<String> options, String login, String command) {
160                return su(options, login, Arrays.asList("--command", command));
161        }
162
163        public String su(List<String> options, String login, List<String> args) {
164                List<String> list2 = login == null ? null : Arrays.asList("-", login);
165                return cmd(SU, CollectionUtils.combineStrings(options, list2, args));
166        }
167
168        public String rmrf(List<String> paths) {
169                return rmrf(null, paths);
170        }
171
172        public String rmrf(String path) {
173                Assert.notBlank(path);
174                return rmrf(null, Collections.singletonList(path));
175        }
176
177        public String rmrf(List<String> options, List<String> paths) {
178                List<String> recursiveSilent = Arrays.asList("-r", "-f");
179                if (options == null) {
180                        return rm(recursiveSilent, paths);
181                } else {
182                        return rm(CollectionUtils.combineStringsUniquely(options, recursiveSilent), paths);
183                }
184        }
185
186        public String rm(List<String> paths) {
187                return rm(null, paths);
188        }
189
190        public String rm(String path) {
191                Assert.notBlank(path);
192                return rm(null, Collections.singletonList(path));
193        }
194
195        public String rm(List<String> options, List<String> paths) {
196                Assert.notEmpty(paths);
197                return cmd(RM, CollectionUtils.combineStrings(options, paths));
198        }
199
200        public String chownr(String owner, String group, String path) {
201                Assert.notBlank(path);
202                return chownr(owner, group, Collections.singletonList(path));
203        }
204
205        public String chownr(String owner, String group, List<String> paths) {
206                return chownr(null, owner, group, paths);
207        }
208
209        public String chownr(List<String> options, String owner, String group, List<String> paths) {
210                List<String> recursive = Arrays.asList("-R");
211                if (options == null) {
212                        return chown(recursive, owner, group, paths);
213                } else {
214                        return chown(CollectionUtils.combineStringsUniquely(options, recursive), owner, group, paths);
215                }
216        }
217
218        public String chown(List<String> options, String owner, String group, String path) {
219                Assert.notBlank(path);
220                return chown(options, owner, group, Collections.singletonList(path));
221        }
222
223        public String chown(List<String> options, String owner, String group, List<String> paths) {
224                Assert.notEmpty(paths);
225                Assert.noBlanks(owner, group);
226                return cmd(CHOWN, CollectionUtils.combineStrings(options, owner + ":" + group, paths));
227        }
228
229        public String chown(String owner, String group, String path) {
230                Assert.notBlank(path);
231                return chown(null, owner, group, Collections.singletonList(path));
232        }
233
234        public String cmd(String executable) {
235                return cmd(executable, null);
236        }
237
238        public String cmd(String executable, List<String> args) {
239                return cmd(executable, args, false);
240        }
241
242        public String cmd(String executable, List<String> args, boolean bypassAnyAliases) {
243                StringBuilder sb = new StringBuilder();
244                if (bypassAnyAliases) {
245                        sb.append("\"");
246                }
247                sb.append(executable);
248                if (!CollectionUtils.isEmpty(args)) {
249                        sb.append(" ");
250                        sb.append(CollectionUtils.getSpaceSeparatedString(args));
251                }
252                return sb.toString();
253        }
254}