Repository navigation
Expand file tree
/
Copy pathSysUtilAdmin.java
More file actions
175 lines (155 loc) · 4.91 KB
/
Copy pathSysUtilAdmin.java
File metadata and controls
175 lines (155 loc) · 4.91 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package act;
import act.app.CliContext;
import act.cli.Command;
import act.cli.Optional;
import act.di.Context;
import act.util.PropertySpec;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.io.File;
import java.io.IOException;
import java.util.List;
@SuppressWarnings("unused")
public class SysUtilAdmin {
private CliContext context;
public SysUtilAdmin() {
context = CliContext.current();
}
@Command(name = "act.version, act.ver", help = "Print actframework version")
public String version() {
return Version.fullVersion().replace("-S-", "-SNAPSHOT-");
}
@Command(name = "act.pwd", help = "Print name of the current working directory")
public String pwd() {
return pwd(context).getAbsolutePath();
}
@Command(name = "act.ls, act.dir", help = "List files in the current working directory")
@PropertySpec("context,path,size")
public List<FileInfo> ls(
@Optional String path,
@Context CliContext ctx
) {
if (S.blank(path)) {
return dir(curDir(), context);
} else {
File file = getFile(path);
if (!file.exists()) {
ctx.println("%s is not a file or directory", path);
return null;
} else {
if (file.isDirectory()) {
return dir(file, context);
} else {
return C.list(new FileInfo(file.getParentFile(), file));
}
}
}
}
@Command(name = "act.cd", help = "Change working directory")
public void cd(@Optional String path) {
if (S.blank(path)) {
context.println(pwd());
return;
}
File file = getFile(path);
if (path.contains("..")) {
try {
file = new File(file.getCanonicalPath());
} catch (IOException e) {
throw E.ioException(e);
}
}
if (!file.isDirectory()) {
context.println("path is not a directory");
return;
}
context.chDir(file);
context.println("current working directory changed to %s", file.getAbsolutePath());
}
private File getFile(String path) {
return context.getFile(path);
}
private File curDir() {
return pwd(context);
}
private static File pwd(CliContext context) {
return context.curDir();
}
private static List<FileInfo> dir(File file, CliContext context) {
C.List<FileInfo> list = C.newList();
File[] files = file.listFiles();
if (null == files) {
context.println("Invalid dir: %s", file.getAbsolutePath());
return C.list();
}
File parent = file.getAbsoluteFile();
for (File f0 : files) {
list.add(new FileInfo(parent, f0));
}
list = list.sorted();
return list;
}
public static class FileInfo implements Comparable<FileInfo> {
String context;
String path;
String size;
boolean isDir;
private FileInfo(File parent, File file) {
this.isDir = file.isDirectory();
this.context = null == parent ? "/" : parent.getAbsolutePath();
this.path = printPath(file);
this.size = printSize(file);
}
public String getContext() {
return context;
}
public String getPath() {
return path;
}
public String getSize() {
return size;
}
@Override
public int compareTo(FileInfo that) {
if (this.isDir) {
return that.isDir ? this.path.compareTo(that.path) : -1;
} else {
return that.isDir ? 1 : this.path.compareTo(that.path);
}
}
private String printPath(File file) {
if (isDir) {
return S.builder("[").append(file.getName()).append("]").toString();
} else {
return file.getName();
}
}
private String printSize(File file) {
String unit;
long len = file.length();
if (len < 1024L) {
unit = "B";
} else {
len /= 1024L;
if (len < 1024L) {
unit = "KB";
} else {
len /= 1024L;
if (len < 1024L) {
unit = "MB";
} else {
len /= 1024L;
if (len < 1024L) {
unit = "GB";
} else {
len /= 1024L;
unit = "TB";
}
}
}
}
return S.builder().append(len).append(unit).toString();
}
}
}