Skip to content

Commit

Permalink
feat(copy): implement copy command
Browse files Browse the repository at this point in the history
closes #18
  • Loading branch information
Irineu333 committed Jan 29, 2024
1 parent 40c13d8 commit 9f004ab
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/neo/envmanager/Envm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Envm : CliktCommand(
Rename(),
Setter(),
Remove(),
Rollback()
Rollback(),
Copy()
)
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/com/neo/envmanager/command/Copy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.neo.envmanager.command

import com.github.ajalt.clikt.parameters.arguments.argument
import com.neo.envmanager.core.Command
import com.neo.envmanager.model.Environment
import com.neo.envmanager.util.extension.requireInstall

class Copy : Command(
help = "Copy an environment"
) {
private val tag by argument(
help = "Environment tag"
)

private val newTag by argument(
help = "New environment tag"
)

override fun run() {

requireInstall()

Environment.get(
paths.environmentsDir,
tag
).copyTo(newTag)
}
}
11 changes: 7 additions & 4 deletions src/main/kotlin/com/neo/envmanager/command/Rename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Rename : Command(
help = "Rename an environment"
) {

private val oldTag by argument(
help = "Old environment tag"
private val tag by argument(
help = "Environment tag"
)

private val newTag by argument(
Expand All @@ -22,9 +22,12 @@ class Rename : Command(

val config = requireInstall()

Environment.get(paths.environmentsDir, oldTag).renameTo(newTag)
Environment.get(
paths.environmentsDir,
tag
).renameTo(newTag)

if (oldTag == config.currentEnv) {
if (tag == config.currentEnv) {
config.update {
it.copy(
currentEnv = newTag
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/neo/envmanager/model/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ value class Environment(val file: File) {
return Environment(newFile)
}

fun copyTo(tag: String) : Environment {

val newFile = file.parentFile.resolve(tag.json)

if (newFile.exists()) throw EnvironmentAlreadyExists(tag)

file.copyTo(newFile)

return Environment(newFile)
}

fun add(properties: Map<*, *>) {
write(read() + properties)
}
Expand Down

0 comments on commit 9f004ab

Please sign in to comment.