001    package org.kuali.common.util;
002    
003    import java.util.Arrays;
004    import java.util.Collections;
005    import java.util.List;
006    
007    public class UnixCmds {
008            private static final String SU = "su";
009            private static final String MKDIR = "mkdir";
010            private static final String RM = "rm";
011            private static final String CHOWN = "chown";
012            private static final String CHMOD = "chmod";
013    
014            public String chmod(String mode, String path) {
015                    Assert.notBlank(path);
016                    return chmod(mode, Collections.singletonList(path));
017            }
018    
019            public String chmod(String mode, List<String> paths) {
020                    return chmod(null, mode, paths);
021    
022            }
023    
024            public String chmod(List<String> options, String mode, List<String> paths) {
025                    Assert.hasLength(mode);
026                    Assert.notEmpty(paths);
027                    return cmd(CHMOD, CollectionUtils.combineStrings(options, mode, paths));
028            }
029    
030            public String mkdirp(String path) {
031                    Assert.notBlank(path);
032                    return mkdirp(null, Collections.singletonList(path));
033            }
034    
035            public String mkdirp(List<String> paths) {
036                    return mkdirp(null, paths);
037            }
038    
039            public String mkdirp(List<String> options, List<String> paths) {
040                    List<String> parents = Arrays.asList("-p");
041                    if (options == null) {
042                            return mkdir(parents, paths);
043                    } else {
044                            return mkdir(CollectionUtils.combineStringsUniquely(options, parents), paths);
045                    }
046    
047            }
048    
049            public String mkdir(String path) {
050                    Assert.notBlank(path);
051                    return mkdir(null, Collections.singletonList(path));
052            }
053    
054            public String mkdir(List<String> options, List<String> paths) {
055                    Assert.notEmpty(paths);
056                    return cmd(MKDIR, CollectionUtils.combineStrings(options, paths));
057            }
058    
059            public String su(String login, String command) {
060                    return su(null, login, command);
061            }
062    
063            public String su(List<String> options, String login, String command) {
064                    return su(options, login, Arrays.asList("--command", command));
065            }
066    
067            public String su(List<String> options, String login, List<String> args) {
068                    List<String> list2 = login == null ? null : Arrays.asList("-", login);
069                    return cmd(SU, CollectionUtils.combineStrings(options, list2, args));
070            }
071    
072            public String rmrf(List<String> paths) {
073                    return rmrf(null, paths);
074            }
075    
076            public String rmrf(String path) {
077                    Assert.notBlank(path);
078                    return rmrf(null, Collections.singletonList(path));
079            }
080    
081            public String rmrf(List<String> options, List<String> paths) {
082                    List<String> recursiveSilent = Arrays.asList("-r", "-f");
083                    if (options == null) {
084                            return rm(recursiveSilent, paths);
085                    } else {
086                            return rm(CollectionUtils.combineStringsUniquely(options, recursiveSilent), paths);
087                    }
088            }
089    
090            public String rm(List<String> paths) {
091                    return rm(null, paths);
092            }
093    
094            public String rm(String path) {
095                    Assert.notBlank(path);
096                    return rm(null, Collections.singletonList(path));
097            }
098    
099            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    }