Skip to content

Commit 870f6c9

Browse files
committed
forgot some files
1 parent 78bd24f commit 870f6c9

5 files changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sandwich.koan.cmdline.behavior;
2+
3+
import java.io.IOException;
4+
5+
import com.sandwich.util.FileUtils;
6+
7+
public class Backup extends KoanFileCopying{
8+
9+
@Override
10+
protected void copy(String backupSrcDirectory, String appSrcDirectory)
11+
throws IOException {
12+
FileUtils.copy(appSrcDirectory, backupSrcDirectory);
13+
}
14+
15+
@Override
16+
protected String getErrorMessage() {
17+
return "An issue was encountered saving a backup copy. Check that the directory exists and try again.";
18+
}
19+
20+
@Override
21+
protected String getSuccessMessage() {
22+
return "Working koans were backed up successfully";
23+
}
24+
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.sandwich.koan.cmdline.behavior;
2+
3+
import com.sandwich.koan.constant.KoanConstants;
4+
5+
public class Debug implements ArgumentBehavior{
6+
7+
public void run(String arg){
8+
KoanConstants.DEBUG = KoanConstants.DEBUG ||
9+
"true".equalsIgnoreCase(arg) ||
10+
arg == null ||
11+
arg.trim().length() == 0;
12+
}
13+
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.sandwich.koan.cmdline.behavior;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.FILESYSTEM_SEPARATOR;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
public abstract class KoanFileCopying implements ArgumentBehavior{
9+
10+
@Override
11+
public void run(String value) {
12+
File baseDirectoryFile = new File(ClassLoader.getSystemResource(".").getFile()).getParentFile();
13+
String baseDirectory = baseDirectoryFile.getAbsolutePath();
14+
String backupSrcDirectory = new StringBuilder(baseDirectory).append(FILESYSTEM_SEPARATOR)
15+
.append("data")
16+
.append(FILESYSTEM_SEPARATOR)
17+
.append("src")
18+
.append(FILESYSTEM_SEPARATOR).toString();
19+
String appSrcDirectory = new StringBuilder(baseDirectory).append(FILESYSTEM_SEPARATOR)
20+
.append("src")
21+
.append(FILESYSTEM_SEPARATOR).toString();
22+
try {
23+
copy(backupSrcDirectory, appSrcDirectory);
24+
} catch (IOException e) {
25+
System.out.println(getErrorMessage());
26+
System.exit(-1);
27+
}
28+
System.out.println(getSuccessMessage());
29+
}
30+
31+
protected abstract String getSuccessMessage();
32+
protected abstract String getErrorMessage();
33+
protected abstract void copy(String backupSrcDirectory, String appSrcDirectory) throws IOException;
34+
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sandwich.koan.cmdline.behavior;
2+
3+
import java.io.IOException;
4+
5+
import com.sandwich.util.FileUtils;
6+
7+
public class Reset extends KoanFileCopying{
8+
9+
@Override
10+
protected void copy(String backupSrcDirectory, String appSrcDirectory)
11+
throws IOException {
12+
FileUtils.copy(backupSrcDirectory, appSrcDirectory);
13+
}
14+
15+
@Override
16+
protected String getErrorMessage() {
17+
return "There was an unanticipated error encountered restoring the koan files. " +
18+
"You're best bet is to start with a fresh copy from your downloads.";
19+
}
20+
21+
@Override
22+
protected String getSuccessMessage() {
23+
return "Koans restored successfully.";
24+
}
25+
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.sandwich.util;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
10+
public class FileUtils {
11+
12+
public static void copy(String fileName0, String fileName1) throws IOException{
13+
copy(new File(fileName0), new File(fileName1));
14+
}
15+
16+
public static void copy(File file0, File file1) throws IOException{
17+
if((file0 == null || !file0.exists()) && file1 == null){
18+
throw new IllegalArgumentException("Both path's must actually exist");
19+
}
20+
if (file0.isDirectory()) {
21+
if (!file1.exists()) {
22+
file1.mkdir();
23+
}
24+
String files[] = file0.list();
25+
for (int i = 0; i < files.length; i++) {
26+
copy(new File(file0, files[i]), new File(file1, files[i]));
27+
}
28+
} else {
29+
if (!file0.exists()) {
30+
System.out.println("File or directory does not exist: "+file0);
31+
System.exit(0);
32+
} else {
33+
InputStream in = new FileInputStream(file0);
34+
OutputStream out = new FileOutputStream(file1);
35+
36+
// Transfer bytes from in to out
37+
byte[] buf = new byte[1024];
38+
int len;
39+
40+
while ((len = in.read(buf)) > 0) {
41+
out.write(buf, 0, len);
42+
}
43+
44+
in.close();
45+
out.close();
46+
}
47+
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)