From 65ad59bcb903d7929b6d3474dcf8fefecee362d2 Mon Sep 17 00:00:00 2001 From: mik-tf Date: Wed, 2 Oct 2024 16:43:25 -0400 Subject: [PATCH] updated UIUX --- flist.v | 160 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 58 deletions(-) diff --git a/flist.v b/flist.v index 8bc6e2c..44db5b4 100644 --- a/flist.v +++ b/flist.v @@ -25,19 +25,51 @@ struct Response { } fn error_message(msg string) { - println(term.bold(term.red('Error: ')) + msg) - println(term.yellow('Run \'flist help\' for usage information.')) + println(term.red('\nError: ') + msg) + println(term.yellow('Run \'flist help\' for usage information.\n')) +} + +fn success_message(msg string) { + println(term.green('\n' + msg + '\n')) +} + +fn info_message(msg string) { + println(term.blue('\n' + msg + '\n')) +} + +fn create_box(content []string, padding int) string { + mut max_width := 0 + for line in content { + clean_line := term.strip_ansi(line) + if clean_line.len > max_width { + max_width = clean_line.len + } + } + max_width += padding * 2 + + separator := '━'.repeat(max_width + 2) // +2 for left and right borders + mut box_content := term.blue('┏$separator┓') + '\n' + + for line in content { + clean_line := term.strip_ansi(line) + padding_left := ' '.repeat(padding) + padding_right := ' '.repeat(max_width - clean_line.len) + box_content += term.blue('┃') + padding_left + line + padding_right + term.blue('┃') + '\n' + } + + box_content += term.blue('┗$separator┛') + return box_content } fn install() { - println('Installing Flist CLI...') + info_message('Installing Flist CLI...') current_exe := os.executable() if os.exists(current_exe) { os.mkdir_all(os.dir(binary_location)) or { panic(err) } os.cp(current_exe, binary_location) or { panic(err) } os.chmod(binary_location, 0o755) or { panic(err) } - println('Flist CLI has been installed to ' + binary_location) - println('You can now use it by running \'flist help\'') + success_message('Flist CLI has been installed to ' + binary_location) + info_message('You can now use it by running \'flist help\'') } else { error_message('Cannot find the executable file') exit(1) @@ -45,12 +77,12 @@ fn install() { } fn uninstall() { - println('Uninstalling Flist CLI...') + info_message('Uninstalling Flist CLI...') if os.exists(binary_location) { os.rm(binary_location) or { panic(err) } - println('Flist CLI has been removed from ' + binary_location) + success_message('Flist CLI has been removed from ' + binary_location) } else { - println('Flist CLI is not installed at ' + binary_location) + info_message('Flist CLI is not installed at ' + binary_location) } } @@ -64,32 +96,32 @@ fn login() { } if token_exists && docker_logged_in { - println('You are already logged in to Docker Hub and your Flist Hub token is present.') + info_message('You are already logged in to Docker Hub and your Flist Hub token is present.') return } if !token_exists { tfhub_token := os.input('Please enter your tfhub token: ') os.write_file(token_file, tfhub_token) or { panic(err) } - println('Token saved in ' + token_file) + success_message('Token saved in ' + token_file) } else { - println('Your Flist Hub token is already saved.') + info_message('Your Flist Hub token is already saved.') } if !docker_logged_in { - println('Logging in to Docker Hub...') + info_message('Logging in to Docker Hub...') exit_code := os.system('sudo docker login') if exit_code == 0 { - println('Successfully logged in to Docker Hub.') + success_message('Successfully logged in to Docker Hub.') } else { error_message('Failed to log in to Docker Hub.') return } } else { - println('Already logged in to Docker Hub.') + info_message('Already logged in to Docker Hub.') } - println('Login process completed.') + success_message('Login process completed.') } fn logout() { @@ -100,17 +132,16 @@ fn logout() { os.rm(token_file) or { panic(err) } - println('Logging out from Docker Hub...') + info_message('Logging out from Docker Hub...') exit_code := os.system('sudo docker logout') if exit_code != 0 { error_message('Failed to log out from Docker Hub.') } - println('You are now logged out of Docker Hub and your Flist Hub token has been removed.') + success_message('You are now logged out of Docker Hub and your Flist Hub token has been removed.') } fn push(tag string) { - docker_user_result := os.execute("sudo docker system info | grep 'Username' | cut -d ' ' -f 3") if docker_user_result.exit_code != 0 || docker_user_result.output.trim_space() == '' { error_message('Failed to get Docker username. Please ensure you are logged in to Docker.') @@ -118,7 +149,7 @@ fn push(tag string) { } docker_user := docker_user_result.output.trim_space() - println('Docker username: $docker_user') + info_message('Docker username: $docker_user') full_tag := '${docker_user}/${tag}' @@ -127,19 +158,19 @@ fn push(tag string) { exit(1) } - println('Starting Docker build') + info_message('Starting Docker build') if os.system('sudo docker buildx build -t ${full_tag} .') != 0 { error_message('Docker build failed') exit(1) } - println('Finished local Docker build, now pushing to Docker Hub') + info_message('Finished local Docker build, now pushing to Docker Hub') if os.system('sudo docker push ${full_tag}') != 0 { error_message('Docker push failed') exit(1) } - println('Converting Docker image to flist now...') + info_message('Converting Docker image to Flist now...') url := 'https://hub.grid.tf/api/flist/me/docker' data := 'image=$full_tag' @@ -155,35 +186,44 @@ fn push(tag string) { } config.header.add_custom('Content-Type', 'application/x-www-form-urlencoded') or { - eprintln('Add custom failed: $err') + error_message('Add custom failed: $err') exit(1) } response := http.fetch(config) or { - eprintln('HTTP POST request failed: $err') + error_message('HTTP POST request failed: $err') exit(1) } if response.status_code == 200 { - println('Request successful. Response body:') - println(response.body) - - // Get the hub username to construct the flist URL hub_user := get_hub_username(tfhub_token) or { error_message('Failed to get hub username') exit(1) } - // Construct the flist URL flist_name := tag.replace(':', '-') + '.flist' flist_url := 'https://hub.grid.tf/$hub_user/$flist_name' - println('\nFlist URL:') - println(flist_url) + success_content := [ + term.bold(term.green('Success!') + ' Your Flist has been created and pushed to the TF Hub.'), + '', + term.bold('Flist Details:'), + term.yellow('Name: ') + flist_name, + term.yellow('User: ') + hub_user, + term.yellow('URL: ') + flist_url, + '', + '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' + ] + + println(create_box(success_content, 2)) } else { - eprintln('Request failed with status code: ${response.status_code}') - eprintln('Response body:') - eprintln(response.body) + error_message('Request failed with status code: ${response.status_code}') + println('Response body:') + println(response.body) exit(1) } } @@ -194,7 +234,7 @@ fn delete(flist_name string) { exit(1) } - println('Deleting flist: ' + flist_name) + info_message('Deleting Flist: ' + flist_name) url := 'https://hub.grid.tf/api/flist/me/' + flist_name config := http.FetchConfig{ url: url @@ -208,7 +248,7 @@ fn delete(flist_name string) { } if response.status_code == 200 { - println('Deletion request sent successfully.') + success_message('Deletion request sent successfully.') } else { error_message('Deletion request failed with status code: ' + response.status_code.str()) } @@ -220,7 +260,7 @@ fn rename(flist_name string, new_flist_name string) { exit(1) } - println('Renaming flist: ' + flist_name + ' to ' + new_flist_name) + info_message('Renaming Flist: ' + flist_name + ' to ' + new_flist_name) url := 'https://hub.grid.tf/api/flist/me/' + flist_name + '/rename/' + new_flist_name config := http.FetchConfig{ url: url @@ -234,7 +274,7 @@ fn rename(flist_name string, new_flist_name string) { } if response.status_code == 200 { - println('Rename request sent successfully.') + success_message('Rename request sent successfully.') } else { error_message('Rename request failed with status code: ' + response.status_code.str()) } @@ -312,41 +352,45 @@ fn ls(show_url bool) { exit(1) } - println('Flists for user $hub_user:') + mut content := [term.bold('Flists for user ' + term.green(hub_user) + ':')] for item in data { if show_url { - println('https://hub.grid.tf/$hub_user/${item.name}') + content << term.yellow('> ') + 'https://hub.grid.tf/' + hub_user + '/' + item.name } else { - println(item.name) + content << term.yellow('> ') + item.name } } + + println(create_box(content, 2)) } fn help() { - println(term.bold(term.green('\n Welcome to the Flist CLI!'))) - println('This tool turns Dockerfiles and Docker images directly into Flist on the TF Flist Hub, passing by the Docker Hub.\n') + welcome_msg := term.bold(term.green('Welcome to the Flist CLI!')) + println(create_box([welcome_msg], 2)) + + println('This tool turns Dockerfiles and Docker images directly into Flists on the TF Flist Hub, passing by the Docker Hub.\n') println(term.bold('Available commands:')) println(term.blue(' install ') + '- Install the Flist CLI') println(term.blue(' uninstall ') + '- Uninstall the Flist CLI') println(term.blue(' login ') + '- Log in to Docker Hub and save the Flist Hub token') println(term.blue(' logout ') + '- Log out of Docker Hub and remove the Flist Hub token') - println(term.blue(' push ') + '- Build and push a Docker image to Docker Hub, then convert and push it as an flist to Flist Hub') - println(term.blue(' delete ') + '- Delete an flist from Flist Hub') - println(term.blue(' rename ') + '- Rename an flist in Flist Hub') - println(term.blue(' ls ') + '- List all flists of the current user') - println(term.blue(' ls url ') + '- List all flists of the current user with full URLs') + println(term.blue(' push ') + '- Build and push a Docker image to Docker Hub, then convert and push it as an Flist to Flist Hub') + println(term.blue(' delete ') + '- Delete an Flist from Flist Hub') + println(term.blue(' rename ') + '- Rename an Flist in Flist Hub') + println(term.blue(' ls ') + '- List all Flists of the current user') + println(term.blue(' ls url ') + '- List all Flists of the current user with full URLs') println(term.blue(' help ') + '- Display this help message\n') println(term.bold('Usage:')) - println(' sudo ./flist install') - println(' sudo flist uninstall') - println(' flist login') - println(' flist logout') - println(' flist push :') - println(' flist delete ') - println(' flist rename ') - println(' flist ls') - println(' flist ls url') - println(' flist help') + println(term.yellow(' sudo ./flist install')) + println(term.yellow(' sudo flist uninstall')) + println(term.yellow(' flist login')) + println(term.yellow(' flist logout')) + println(term.yellow(' flist push :')) + println(term.yellow(' flist delete ')) + println(term.yellow(' flist rename ')) + println(term.yellow(' flist ls')) + println(term.yellow(' flist ls url')) + println(term.yellow(' flist help\n')) } fn main() {