added docker username function fetcher and updated push

This commit is contained in:
mik-tf 2024-10-03 10:56:49 -04:00
parent 9f4d33f8fc
commit 735f3f2c84

46
flist.v
View File

@ -2,6 +2,7 @@ import os
import net.http import net.http
import term import term
import json import json
import x.json2
const ( const (
token_file = os.join_path(os.home_dir(), '.config', 'tfhubtoken') token_file = os.join_path(os.home_dir(), '.config', 'tfhubtoken')
@ -122,14 +123,51 @@ fn logout() {
success_message('You are now logged out of Docker Hub and your Flist Hub token has been removed.') success_message('You are now logged out of Docker Hub and your Flist Hub token has been removed.')
} }
fn get_docker_credential() !string {
// Read the Docker config file
config_path := os.join_path(os.home_dir(), '.docker', 'config.json')
config_content := os.read_file(config_path) or {
return error('Failed to read Docker config file: $err')
}
// Parse the JSON content
config := json2.raw_decode(config_content) or {
return error('Failed to parse Docker config: $err')
}
// Extract the credsStore value
creds_store := config.as_map()['credsStore'] or {
return error('credsStore not found in Docker config')
}.str()
// Execute the docker-credential command
cred_helper := 'docker-credential-$creds_store'
cred_output := os.execute('$cred_helper list')
if cred_output.exit_code != 0 {
return error('Failed to execute $cred_helper: ${cred_output.output}')
}
// Parse the credential list
cred_list := json2.raw_decode(cred_output.output) or {
return error('Failed to parse credential list: $err')
}
// Find the first docker.io entry
for key, value in cred_list.as_map() {
if key.contains('docker.io') {
return value.str()
}
}
return error('No docker.io credential found')
}
fn push(tag string) { fn push(tag string) {
docker_user_result := os.execute('docker-credential-$(jq -r .credsStore ~/.docker/config.json) list | jq -r \'. | to_entries[] | select(.key | contains("docker.io")) | last(.value)\' | head -n 1') docker_user := get_docker_credential() or {
if docker_user_result.exit_code != 0 || docker_user_result.output.trim_space() == '' { error_message('Failed to get Docker username: $err')
error_message('Failed to get Docker username.')
exit(1) exit(1)
} }
docker_user := docker_user_result.output.trim_space()
info_message('Docker username: $docker_user') info_message('Docker username: $docker_user')
full_tag := '${docker_user}/${tag}' full_tag := '${docker_user}/${tag}'