forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackriper.kt
249 lines (201 loc) · 7.88 KB
/
blackriper.kt
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.RemoteAddCommand
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.transport.RefSpec
import org.eclipse.jgit.transport.URIish
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
import java.io.File
import kotlin.io.path.Path
/*
para no depender del cliente del http client y tener mejor modularizacion
usaremos la siguiente libreria:
-Git : https://git-scm.com/book/es/v2/Ap%C3%A9ndice-B:-Integrando-Git-en-tus-Aplicaciones-JGit
*/
data class GithubAuth(val username: String, val password: String)
// acciones que se pueden realizar desde el repositorio remoto y con las diferentes operaciones con las branches
enum class BranchOptions{
NEW,
CHANGE,
DELETE
}
enum class GitAction{
PULL,
PUSH
}
// definir funcionamiento de la aplicacion
interface CliRepository{
fun workDirectory(src: String)
fun actionBranch(nameBranch: String, option:BranchOptions)
fun gitStatus()
fun createCommit(message : String)
fun addingRemoteRepo(url: String)
fun actionRemote(action:GitAction,remoteBranchName: String,user:GithubAuth)
fun existRemoteRepository(): Boolean
}
// implementar logica con jgit y kotlin
class GitRespository:CliRepository{
private lateinit var git: Git
private lateinit var uriRemote: String
override fun workDirectory(src: String) {
val aboslutePath= Path(src)
val result= runCatching { File("${aboslutePath.toAbsolutePath()}") }
if (result.isFailure) println("directory not found ${result.exceptionOrNull()}")
if (result.isSuccess){
git= Git.init().apply { setDirectory(result.getOrNull())}.call()
println("created a new repository at ${git.repository.directory}")
}
}
override fun actionBranch(nameBranch: String,option: BranchOptions) {
val result= runCatching {
when (option) {
BranchOptions.NEW -> git.branchCreate().apply {
setName(nameBranch)
}.call()
BranchOptions.CHANGE -> git.checkout().apply {
setName(nameBranch)
}.call()
BranchOptions.DELETE -> git.branchDelete().apply {
setBranchNames(nameBranch)
setForce(true)
}.call()
}
}
if (result.isSuccess) println("the ${nameBranch} executed operation ${option.name}")
if (result.isFailure) println("branch operation error ${result.exceptionOrNull()}")
}
override fun gitStatus() {
var status=""
git.status().call().run {
status="""
Git Status branch ${git.repository.branch}
Added: ${this.added}
Changed: ${this.changed}
Conflicting: ${this.conflicting}
Missing: ${this.missing}
Modified: ${this.modified}
Removed: ${this.removed}
Untracked: ${this.untracked}
UntrackedFolders: ${this.untrackedFolders}
""".trimIndent()
println(status)
}
}
override fun createCommit(message: String) {
git.run {
val result= runCatching {
add().apply { addFilepattern(".") }.call()
commit().apply { setMessage(message) }.call()
}
if (result.isSuccess) println("commit added sucessfully")
if (result.isFailure) println("error to create commit ${result.exceptionOrNull()}")
}
}
override fun addingRemoteRepo(url: String) {
val result= runCatching {
git.remoteAdd().apply {
setName("origin")
setUri(URIish(url))
}.call()
}
if (result.isSuccess) {
println("remote repo ${url} added sucessfully")
uriRemote=url
}
if (result.isFailure) println("error to adding remote repository ${result.exceptionOrNull()}")
}
override fun actionRemote(action: GitAction,remoteBranchName: String,user:GithubAuth) {
val result= runCatching {
when (action) {
GitAction.PULL -> git.pull().apply {
setRemoteBranchName(remoteBranchName)
setRebase(true)
}.call()
GitAction.PUSH -> git.push().apply {
setCredentialsProvider(UsernamePasswordCredentialsProvider(user.username,user.password))
setRefSpecs(RefSpec(remoteBranchName))
setForce(true)
}.call()
}
}
if (result.isSuccess) println("operation ${action.name} execute sucessfully")
if (result.isFailure) println("error to execute ${action.name} ${result.exceptionOrNull()}")
}
override fun existRemoteRepository(): Boolean = git.remoteList().call().isNotEmpty()
}
class CliApp(private val repository: CliRepository){
private lateinit var userGithub:GithubAuth
fun showMenuOptions(){
var option=0
while (option!=6){
println("KGit App")
println("""
1.- Create new Repository
2.- Create Commit
3.- Repo Status
4.- Actions Branch
5.- Actions Remote
6.- Exit
""".trimIndent())
option= readLine()?.toInt() ?:0
when(option){
1-> createRepository()
2-> createCommit()
3-> repository.gitStatus()
4-> operationsBranch()
5-> operationsRemote()
6-> break
}
}
}
private fun createRepository(){
println("introduce path where the project is located: ")
val src= readLine().toString()
repository.workDirectory(src)
}
private fun operationsBranch(){
println("Select operation branch to execute 1.-New, 2.-Change , 3.-Delete")
var action= readLine()?.toInt() ?:0
println("name the branch to ${if (action==1) "New" else if (action==2) "Change" else "Delete"} ?")
var nameBranch= readLine()?:""
val actionBranch= when(action){
1-> BranchOptions.NEW
2->BranchOptions.CHANGE
3->BranchOptions.DELETE
else -> BranchOptions.NEW
}
repository.actionBranch(nameBranch,actionBranch)
}
private fun operationsRemote(){
if (repository.existRemoteRepository().not()){
println("There is no remote repository copy the url of one")
val remoteUrl= readLine().toString()
repository.addingRemoteRepo(remoteUrl)
}
println("selected operation remote to execute 1.-Pull , 2.-Push")
val action=readLine()?.toInt()?:0
println("name the remote branch to work ")
var remoteBranch= readLine()?:""
println("username to use in github")
val name=readLine()?:""
println("generate github token and paste this ")
val password=readLine()?:""
userGithub=GithubAuth(username = name,password=password)
val actionRemote= when(action){
1 -> GitAction.PULL
2 -> GitAction.PUSH
else -> GitAction.PUSH
}
repository.actionRemote(actionRemote,remoteBranch,userGithub)
}
private fun createCommit(){
println("Adding message commit ")
val message= readLine()?:"first commit".let { if (it.isEmpty()) "first commit" else it }
repository.createCommit(message)
}
}
fun main() {
val repoGit =GitRespository()
val appCli= CliApp(repoGit)
appCli.showMenuOptions()
}