From 735f3f2c84f0c0663ec7a513af6ed4cc71f32071 Mon Sep 17 00:00:00 2001 From: mik-tf Date: Thu, 3 Oct 2024 10:56:49 -0400 Subject: [PATCH 1/3] 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}' From 6f49278e755eae59e83bd1517d4145d9d974c141 Mon Sep 17 00:00:00 2001 From: mik-tf Date: Thu, 3 Oct 2024 11:17:58 -0400 Subject: [PATCH 2/3] updated credential docker --- flist.v | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/flist.v b/flist.v index 49755c2..865251d 100644 --- a/flist.v +++ b/flist.v @@ -124,6 +124,20 @@ fn logout() { } fn get_docker_credential() !string { + // Try to get the Docker credential automatically + credential := get_docker_credential_auto() or { + // If automatic retrieval fails, prompt the user for input + println(term.yellow('\nCouldn\'t find your Docker username automatically.')) + username := os.input('Please enter your Docker username and press ENTER: ') + if username.trim_space() == '' { + return error('No Docker username provided') + } + return username.trim_space() + } + return credential +} + +fn get_docker_credential_auto() !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 { From c3c9b4bc75c95ab612cb3128f49eca7c41fb1056 Mon Sep 17 00:00:00 2001 From: mik-tf Date: Thu, 3 Oct 2024 11:25:09 -0400 Subject: [PATCH 3/3] added one method to fetch docker credentials --- flist.v | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/flist.v b/flist.v index 865251d..d8be717 100644 --- a/flist.v +++ b/flist.v @@ -138,6 +138,13 @@ fn get_docker_credential() !string { } fn get_docker_credential_auto() !string { + // First, try to get the Docker username using the system info command + system_info_result := os.execute('sudo docker system info | grep \'Username\' | cut -d \' \' -f 3') + if system_info_result.exit_code == 0 && system_info_result.output.trim_space() != '' { + return system_info_result.output.trim_space() + } + + // If the above method fails, proceed with the current method // Read the Docker config file config_path := os.join_path(os.home_dir(), '.docker', 'config.json') config_content := os.read_file(config_path) or {