macos v flist fixes
This commit is contained in:
parent
e7d572f50e
commit
7304271e22
100
flist.v
100
flist.v
@ -2,7 +2,6 @@ import os
|
||||
import net.http
|
||||
import term
|
||||
import json
|
||||
import x.json2
|
||||
|
||||
const token_file = os.join_path(os.home_dir(), '.config', 'tfhubtoken')
|
||||
const docker_username_file = os.join_path(os.home_dir(), '.config', 'dockerusername')
|
||||
@ -173,21 +172,17 @@ fn uninstall() {
|
||||
|
||||
fn login() {
|
||||
mut token_exists := os.exists(token_file)
|
||||
|
||||
if !token_exists {
|
||||
$if windows {
|
||||
os.mkdir_all(config_dir) or { panic(err) }
|
||||
}
|
||||
if !token_exists {
|
||||
tfhub_token := os.input('Please enter your tfhub token: ')
|
||||
os.write_file(token_file, tfhub_token) or { panic(err) }
|
||||
success_message('Token saved in ' + token_file)
|
||||
success_message('TF Hub token saved in ' + token_file)
|
||||
} else {
|
||||
info_message('Your Flist Hub token is already saved.')
|
||||
info_message('Your TF Hub token is already saved.')
|
||||
}
|
||||
|
||||
mut result := 0
|
||||
|
||||
$if macos {
|
||||
mut dockername_exists := os.exists(docker_username_file)
|
||||
|
||||
if !dockername_exists {
|
||||
@ -197,9 +192,6 @@ fn login() {
|
||||
info_message('Enter your Docker password.')
|
||||
result = os.system('${docker_cmd} login -u ${docker_username}')
|
||||
}
|
||||
} $else {
|
||||
result = os.system('${docker_cmd} login')
|
||||
}
|
||||
|
||||
if result == 0 {
|
||||
info_message('\nYou are already logged in to Docker.')
|
||||
@ -211,9 +203,9 @@ fn login() {
|
||||
fn logout() {
|
||||
if os.exists(token_file) {
|
||||
os.rm(token_file) or { panic(err) }
|
||||
success_message('Your Flist Hub Token has been removed')
|
||||
success_message('Your TF Hub token has been removed')
|
||||
} else {
|
||||
info_message('Your Flist Hub Token was already not present.')
|
||||
info_message('Your TF Hub token was already not present.')
|
||||
}
|
||||
|
||||
if os.exists(docker_username_file) {
|
||||
@ -228,89 +220,21 @@ fn logout() {
|
||||
error_message('Failed to log out from Docker Hub.')
|
||||
}
|
||||
|
||||
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("${docker_cmd} 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')
|
||||
success_message('You are now logged out of Docker Hub and your TF Hub token has been removed.')
|
||||
}
|
||||
|
||||
fn push(tag string) {
|
||||
mut docker_user := ''
|
||||
$if macos {
|
||||
docker_user = os.read_file(docker_username_file) or {
|
||||
docker_user := os.read_file(docker_username_file) or {
|
||||
error_message("No Docker username found. Please run 'flist login' first.")
|
||||
exit(1)
|
||||
}
|
||||
} $else {
|
||||
docker_user = get_docker_credential() or {
|
||||
error_message('Failed to get Docker username: ${err}')
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
info_message('Docker username: ${docker_user}')
|
||||
|
||||
full_tag := '${docker_user}/${tag}'
|
||||
|
||||
tfhub_token := os.read_file(token_file) or {
|
||||
error_message("No token found. Please run 'flist login' first.")
|
||||
error_message("No TF Hub token found. Please run 'flist login' first.")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
@ -353,7 +277,7 @@ fn push(tag string) {
|
||||
|
||||
if response.status_code == 200 {
|
||||
hub_user := get_hub_username(tfhub_token) or {
|
||||
error_message('Failed to get hub username')
|
||||
error_message('Failed to get TF Hub username')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
@ -387,7 +311,7 @@ fn push(tag string) {
|
||||
|
||||
fn delete(flist_name string) {
|
||||
tfhub_token := os.read_file(token_file) or {
|
||||
error_message("No token found. Please run 'flist login' first.")
|
||||
error_message("No TF Hub token found. Please run 'flist login' first.")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
@ -413,7 +337,7 @@ fn delete(flist_name string) {
|
||||
|
||||
fn rename(flist_name string, new_flist_name string) {
|
||||
tfhub_token := os.read_file(token_file) or {
|
||||
error_message("No token found. Please run 'flist login' first.")
|
||||
error_message("No TF Hub token found. Please run 'flist login' first.")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
@ -474,7 +398,7 @@ fn get_hub_username(tfhub_token string) ?string {
|
||||
|
||||
fn ls(show_url bool) {
|
||||
tfhub_token := os.read_file(token_file) or {
|
||||
error_message("No token found. Please run 'flist login' first.")
|
||||
error_message("No TF Hub token found. Please run 'flist login' first.")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user