Skip to content

Commit 767dce8

Browse files
committed
WIP: MkDir Command implementation
1 parent 7131e15 commit 767dce8

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

filesystem/src/main/scala/com/codingmaniacs/scala/exercises/fs/State.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ package com.codingmaniacs.scala.exercises.fs
2424
import com.codingmaniacs.scala.exercises.fs.directories.Directory
2525

2626
class State(val root: Directory, val workingDir: Directory, val output: String) {
27-
def show(): Unit = print(State.SHELL_PROMPT)
27+
28+
def show(): Unit =
29+
println(output)
30+
print(State.SHELL_PROMPT)
2831

2932
def setMessage(message: String): State = State(root, workingDir, message)
3033
}

filesystem/src/main/scala/com/codingmaniacs/scala/exercises/fs/commands/Command.scala

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,29 @@ trait Command {
2929

3030
object Command {
3131

32+
val MKDIR = "mkdir"
33+
34+
def emptyCommand: Command = new Command {
35+
override def apply(state: State): State = state
36+
}
37+
38+
def incompleteCommand(name: String): Command = new Command {
39+
override def apply(state: State): State =
40+
state.setMessage(s"$name is an incomplete command")
41+
}
42+
3243
def from(input: String): Command = {
33-
print(input)
34-
new UnknownCommand
44+
val tokens = input.split(" ")
45+
if (input.isEmpty || tokens.isEmpty) {
46+
emptyCommand
47+
} else if (MKDIR.equals(tokens(0))) {
48+
if (tokens.length < 2) {
49+
incompleteCommand(MKDIR)
50+
} else {
51+
new MkDir(tokens(1))
52+
}
53+
} else {
54+
new UnknownCommand
55+
}
3556
}
3657
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2019 Geektimus
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package com.codingmaniacs.scala.exercises.fs.commands
23+
import com.codingmaniacs.scala.exercises.fs.State
24+
import com.codingmaniacs.scala.exercises.fs.directories.Directory
25+
26+
class MkDir(name: String) extends Command {
27+
override def apply(state: State): State = {
28+
val wd = state.workingDir
29+
if (wd.hasEntry(name)) {
30+
state.setMessage(s"$name already exists")
31+
} else if (name.contains(Directory.SEPARATOR)) {
32+
state.setMessage(s"$name must not contain separators")
33+
} else if (checkIllegal(name)) {
34+
state.setMessage(s"$name: illegal entry name!")
35+
} else {
36+
performMkDir(state, name)
37+
}
38+
}
39+
40+
def checkIllegal(str: String): Boolean = {
41+
name.contains(".")
42+
}
43+
44+
def performMkDir(state: State, str: String) : State = {
45+
val wd = state.workingDir
46+
47+
val fullPath = wd.path
48+
49+
50+
}
51+
}

filesystem/src/main/scala/com/codingmaniacs/scala/exercises/fs/directories/DirEntry.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121

2222
package com.codingmaniacs.scala.exercises.fs.directories
2323

24-
abstract class DirEntry(val parentPath: String, val name: String)
24+
abstract class DirEntry(val parentPath: String, val name: String) {
25+
def path: String = s"$parentPath${Directory.SEPARATOR}$name"
26+
}

filesystem/src/main/scala/com/codingmaniacs/scala/exercises/fs/directories/Directory.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class Directory(
2525
override val parentPath: String,
2626
override val name: String,
2727
val contents: List[DirEntry]
28-
) extends DirEntry(parentPath, name)
28+
) extends DirEntry(parentPath, name) {
29+
def hasEntry(name: String): Boolean = ???
30+
31+
}
2932

3033
object Directory {
3134
val SEPARATOR = "/"

0 commit comments

Comments
 (0)