From 735f3f2c84f0c0663ec7a513af6ed4cc71f32071 Mon Sep 17 00:00:00 2001 From: mik-tf Date: Thu, 3 Oct 2024 10:56:49 -0400 Subject: [PATCH] added docker username function fetcher and updated push --- flist.v | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/flist.v b/flist.v index d544d75..49755c2 100644 --- a/flist.v +++ b/flist.v @@ -2,6 +2,7 @@ import os import net.http import term import json +import x.json2 const ( 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.') } +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) { - 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') - if docker_user_result.exit_code != 0 || docker_user_result.output.trim_space() == '' { - error_message('Failed to get Docker username.') + docker_user := get_docker_credential() or { + error_message('Failed to get Docker username: $err') exit(1) } - docker_user := docker_user_result.output.trim_space() info_message('Docker username: $docker_user') full_tag := '${docker_user}/${tag}'