Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
feat: Set ssh-key on the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Oct 24, 2024
1 parent 4443a57 commit 95b9dfe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
26 changes: 23 additions & 3 deletions crystallib/develop/gittools/repository.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import freeflowuniverse.crystallib.ui.console
import freeflowuniverse.crystallib.osal
import os
import time
import crypto.md5

// GitRepo holds information about a single Git repository.
pub struct GitRepo {
Expand Down Expand Up @@ -230,7 +231,7 @@ pub fn (mut gs GitRepo) gitlocation_from_path(path string) !GitLocation {
}

// Check if repo path exists and validate fields
pub fn (repo GitRepo) init() ! {
pub fn (mut repo GitRepo) init() ! {
path_string := repo.get_path()!
if repo.gs.coderoot.path == '' {
return error('Coderoot cannot be empty')
Expand All @@ -248,11 +249,30 @@ pub fn (repo GitRepo) init() ! {
if !os.exists(path_string) {
return error('Path does not exist: ${path_string}')
}

//TODO: check tag or branch set on wanted, and not both

//TODO: check deploy key has been set in repo
// if not do git config core.sshCommand "ssh -i /path/to/deploy_key"
if repo.deploysshkey.len == 0 {
repo.set_sshkey()!
}
//TODO: check tag or branch set on wanted, and not both
}

// Set the ssh key on the repo
fn (mut repo GitRepo) set_sshkey() ! {
// TODO: We need to check what should be done here.
key := repo.exec('git config core.sshcommand') or {""}

hashed_key := md5.hexhash(key)
file_name := '${hashed_key}.pub'
sshkeys_path := os.join_path(os.home_dir(), 'hero/cfg/sshkeys')

if !os.exists(sshkeys_path) {
os.mkdir_all(sshkeys_path)!
}

osal.exec(cmd: "echo ${key} > ${sshkeys_path}/${file_name}")!
repo.deploysshkey = key
}


Expand Down
7 changes: 3 additions & 4 deletions crystallib/develop/gittools/repository_clone.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ pub fn (mut gitstructure GitStructure) clone(args GitCloneArgs) !&GitRepo {
mut repo := gitstructure.repo_new_from_gitlocation(git_location)!
repo.status_wanted.url = args.url

// if args.sshkey.len>0{
// //TODO: set the sshkey, save in ~/hero/cfg/sshkeys/$md5hash.pub then add to the git repo
// panic("implement")
// }
if args.sshkey.len>0{
repo.set_sshkey()!
}

parent_dir := repo.get_parent_dir(create: true)!

Expand Down
4 changes: 2 additions & 2 deletions crystallib/develop/gittools/tests/setup.v
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ fn setup_repo() !GittoolsTests {
repo_url: 'https://github.com/Mahmoud-Emad/repo2.git',
}

if os.exists(ts.repo_dir){
if os.exists(ts.coderoot){
ts.clean()!
}

os.mkdir_all(ts.repo_dir)!
os.mkdir_all(ts.coderoot)!
return ts
}

Expand Down

0 comments on commit 95b9dfe

Please sign in to comment.