diff --git a/flist.v b/flist.v index d544d75..d8be717 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,72 @@ 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 { + // 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 { + // 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 { + 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}'