Flist CLI Updated for Linux, MacOS, Windows #7
146
flist.v
146
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,32 +172,25 @@ fn uninstall() {
|
||||
|
||||
fn login() {
|
||||
mut token_exists := os.exists(token_file)
|
||||
|
||||
os.mkdir_all(config_dir) or { panic(err) }
|
||||
if !token_exists {
|
||||
$if windows {
|
||||
os.mkdir_all(config_dir) or { panic(err) }
|
||||
}
|
||||
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)
|
||||
mut dockername_exists := os.exists(docker_username_file)
|
||||
|
||||
if !dockername_exists {
|
||||
docker_username := os.input('Please enter your Docker username: ')
|
||||
os.write_file(docker_username_file, docker_username) or { panic(err) }
|
||||
success_message('Docker username saved in ' + docker_username_file)
|
||||
info_message('Enter your Docker password.')
|
||||
result = os.system('${docker_cmd} login -u ${docker_username}')
|
||||
}
|
||||
} $else {
|
||||
result = os.system('${docker_cmd} login')
|
||||
if !dockername_exists {
|
||||
docker_username := os.input('Please enter your Docker username: ')
|
||||
os.write_file(docker_username_file, docker_username) or { panic(err) }
|
||||
success_message('Docker username saved in ' + docker_username_file)
|
||||
info_message('Enter your Docker password.')
|
||||
result = os.system('${docker_cmd} login -u ${docker_username}')
|
||||
}
|
||||
|
||||
if result == 0 {
|
||||
@ -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,81 +220,13 @@ 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 {
|
||||
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)
|
||||
}
|
||||
docker_user := os.read_file(docker_username_file) or {
|
||||
error_message("No Docker username found. Please run 'flist login' first.")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
info_message('Docker username: ${docker_user}')
|
||||
@ -310,7 +234,7 @@ fn push(tag string) {
|
||||
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)
|
||||
}
|
||||
|
||||
@ -371,9 +295,9 @@ fn push(tag string) {
|
||||
'',
|
||||
'You can access your Flist using the URL above.',
|
||||
'To manage your Flists, use the following commands:',
|
||||
term.yellow(' flist ls ') + '- List all your Flists',
|
||||
term.yellow(' flist delete') + '- Delete an Flist',
|
||||
term.yellow(' flist rename') + '- Rename an Flist',
|
||||
term.yellow(' flist ls ') + ' - List all your Flists',
|
||||
term.yellow(' flist delete') + ' - Delete an Flist',
|
||||
term.yellow(' flist rename') + ' - Rename an Flist',
|
||||
]
|
||||
|
||||
println(create_box(success_content, 2))
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -529,18 +453,18 @@ fn help() {
|
||||
println(term.cyan(info_msg))
|
||||
println(term.bold('Available commands:'))
|
||||
$if !windows {
|
||||
println(term.cyan(' install ') + '- Install the Flist CLI')
|
||||
println(term.cyan(' uninstall ') + '- Uninstall the Flist CLI')
|
||||
println(term.cyan(' install ') + ' - Install the Flist CLI')
|
||||
println(term.cyan(' uninstall') + ' - Uninstall the Flist CLI')
|
||||
}
|
||||
println(term.cyan(' login ') + '- Log in to Docker Hub and save the Flist Hub token')
|
||||
println(term.cyan(' logout ') + '- Log out of Docker Hub and remove the Flist Hub token')
|
||||
println(term.cyan(' push ') +
|
||||
'- Build and push a Docker image to Docker Hub, then convert and push it as an Flist to Flist Hub')
|
||||
println(term.cyan(' delete ') + '- Delete an Flist from Flist Hub')
|
||||
println(term.cyan(' rename ') + '- Rename an Flist in Flist Hub')
|
||||
println(term.cyan(' ls ') + '- List all Flists of the current user')
|
||||
println(term.cyan(' ls url ') + '- List all Flists of the current user with full URLs')
|
||||
println(term.cyan(' help ') + '- Display this help message\n')
|
||||
println(term.cyan(' login ') + ' - Log in to Docker Hub and save the Flist Hub token')
|
||||
println(term.cyan(' logout ') + ' - Log out of Docker Hub and remove the Flist Hub token')
|
||||
println(term.cyan(' push ') +
|
||||
' - Build and push a Docker image to Docker Hub, then convert and push it as an Flist to Flist Hub')
|
||||
println(term.cyan(' delete ') + ' - Delete an Flist from Flist Hub')
|
||||
println(term.cyan(' rename ') + ' - Rename an Flist in Flist Hub')
|
||||
println(term.cyan(' ls ') + ' - List all Flists of the current user')
|
||||
println(term.cyan(' ls url ') + ' - List all Flists of the current user with full URLs')
|
||||
println(term.cyan(' help ') + ' - Display this help message\n')
|
||||
println(term.bold('Usage:'))
|
||||
$if linux {
|
||||
println(term.yellow(' sudo ./flist install'))
|
||||
|
Loading…
Reference in New Issue
Block a user