refactor: update SSH agent examples and module structure

- Refactor `gittools` to remove `sshagent` import
- Update `sshagent.loaded()` to use `ssh-add -l` command
- Relocate and expose `remote_copy` and `remote_auth` functions
- Improve SSH agent examples and remove Linux tests
- Optimize `sshagent` module and `play` function imports
This commit is contained in:
Mahmoud-Emad
2025-08-24 23:54:57 +03:00
parent 9945cfb52c
commit a37dbd2438
9 changed files with 263 additions and 245 deletions

View File

@@ -1,51 +0,0 @@
module main
import freeflowuniverse.herolib.osal.sshagent
import freeflowuniverse.herolib.osal.linux
fn do1() ! {
mut agent := sshagent.new()!
println(agent)
k := agent.get(name: 'kds') or { panic('notgound') }
println(k)
mut k2 := agent.get(name: 'books') or { panic('notgound') }
k2.load()!
println(k2.agent)
println(agent)
k2.forget()!
println(k2.agent)
// println(agent)
}
fn test_user_mgmt() ! {
mut lf := linux.new()!
// Test user creation
lf.user_create(
name: 'testuser'
sshkey: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM3/2K7R8A/l0kM0/d'
)!
// Test ssh key creation
lf.sshkey_create(
username: 'testuser'
sshkey_name: 'testkey'
)!
// Test ssh key deletion
lf.sshkey_delete(
username: 'testuser'
sshkey_name: 'testkey'
)!
// Test user deletion
lf.user_delete(name: 'testuser')!
}
fn main() {
do1() or { panic(err) }
test_user_mgmt() or { panic(err) }
}

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.osal.sshagent
import freeflowuniverse.herolib.ui.console
fn do_sshagent_example() ! {
console.print_header('SSH Agent Basic Example')
mut agent := sshagent.new()!
console.print_debug('SSH Agent created')
println(agent)
// Generate a test key if no keys exist
if agent.keys.len == 0 {
console.print_debug('No keys found, generating test key...')
mut test_key := agent.generate('test_example_key', '')!
test_key.load()!
console.print_debug('Test key generated and loaded')
}
// Try to get a specific key (this will fail if key doesn't exist)
console.print_debug('Looking for existing keys...')
if agent.keys.len > 0 {
// Work with the first available key
mut first_key := agent.keys[0]
console.print_debug('Found key: ${first_key.name}')
if !first_key.loaded {
console.print_debug('Loading key...')
first_key.load()!
console.print_debug('Key loaded')
}
console.print_debug('Key details:')
println(first_key)
// Show agent status after loading
console.print_debug('Agent status after loading:')
println(agent)
// Note: We don't call forget() in this example to avoid removing keys
// first_key.forget()!
} else {
console.print_debug('No keys available in agent')
}
}
fn main() {
do_sshagent_example() or {
console.print_debug('Error: ${err}')
panic(err)
}
console.print_header('SSH Agent example completed successfully!')
}

View File

@@ -0,0 +1,85 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.osal.sshagent
import freeflowuniverse.herolib.ui.console
console.print_header('SSH Agent Management Example')
// Create SSH agent with single instance guarantee
mut agent := sshagent.new_single()!
println('SSH Agent initialized and ensured single instance')
// Show diagnostics
diag := agent.diagnostics()
console.print_header('SSH Agent Diagnostics:')
for key, value in diag {
console.print_item('${key}: ${value}')
}
// Show current agent status
println(agent)
// Example: Generate a test key if no keys exist
if agent.keys.len == 0 {
console.print_header('No keys found, generating example key...')
mut key := agent.generate('example_key', '')!
console.print_debug('Generated key: ${key}')
// Load the generated key
key.load()!
console.print_debug('Key loaded into agent')
}
// Example: Working with existing keys
if agent.keys.len > 0 {
console.print_header('Working with existing keys...')
for i, key in agent.keys {
console.print_debug('Key ${i+1}: ${key.name}')
console.print_debug(' Type: ${key.cat}')
console.print_debug(' Loaded: ${key.loaded}')
console.print_debug(' Email: ${key.email}')
if !key.loaded {
console.print_debug(' Loading key...')
mut key_mut := key
key_mut.load() or {
console.print_debug(' Failed to load: ${err}')
continue
}
console.print_debug(' Key loaded successfully')
}
}
}
// Example: Add a key from private key content
console.print_header('Example: Adding a key from content...')
console.print_debug('Note: This would normally use real private key content')
console.print_debug('For security, we skip this in the example')
// Example: Generate and manage a new key
console.print_header('Example: Generate a new test key...')
test_key_name := 'test_key_example'
// Check if test key already exists
existing_key := agent.get(name: test_key_name) or {
console.print_debug('Test key does not exist, generating...')
// Generate new key
mut new_key := agent.generate(test_key_name, '')!
console.print_debug(' Generated new key: ${new_key.name}')
// Load it
new_key.load()!
console.print_debug(' Key loaded into agent')
new_key
}
console.print_debug('Test key exists: ${existing_key.name}')
// Show final agent status
console.print_header('Final SSH Agent Status:')
println(agent)
console.print_header('SSH Agent example completed successfully')