added ls function

This commit is contained in:
mik-tf 2024-10-02 12:00:45 -04:00
parent aadf417ad0
commit 3628e4f733

96
flist.v
View File

@ -1,6 +1,7 @@
import os
import net.http
import term
import json
const (
token_file = os.join_path(os.home_dir(), '.config', 'tfhubtoken')
@ -11,6 +12,18 @@ const (
}
)
struct FlistItem {
name string
}
struct Payload {
username string
}
struct Response {
payload Payload
}
fn error_message(msg string) {
println(term.bold(term.red('Error: ')) + msg)
println(term.yellow('Run \'flist help\' for usage information.'))
@ -146,7 +159,6 @@ fn push(tag string) {
)
}
// Add Content-Type header
config.header.add_custom('Content-Type', 'application/x-www-form-urlencoded') or {
eprintln('Add custom failed: $err')
exit(1)
@ -166,7 +178,6 @@ fn push(tag string) {
eprintln(response.body)
exit(1)
}
}
fn delete(flist_name string) {
@ -221,6 +232,84 @@ fn rename(flist_name string, new_flist_name string) {
}
}
fn get_hub_username(tfhub_token string) ?string {
url := 'https://hub.grid.tf/api/flist/me'
config := http.FetchConfig{
url: url
method: .get
header: http.new_header(
key: .authorization
value: 'bearer $tfhub_token'
)
}
response := http.fetch(config) or {
error_message('Failed to fetch hub username: $err')
return none
}
if response.status_code != 200 {
error_message('Failed to fetch hub username. Status code: $response.status_code')
return none
}
parsed_response := json.decode(Response, response.body) or {
error_message('Failed to parse JSON response: $err')
return none
}
if parsed_response.payload.username != '' {
return parsed_response.payload.username
}
error_message('Username not found in response')
return none
}
fn ls() {
tfhub_token := os.read_file(token_file) or {
error_message('No token found. Please run \'flist login\' first.')
exit(1)
}
hub_user := get_hub_username(tfhub_token) or {
error_message('Failed to get hub username')
exit(1)
}
url := 'https://hub.grid.tf/api/flist/$hub_user'
config := http.FetchConfig{
url: url
method: .get
header: http.new_header(
key: .authorization
value: 'bearer $tfhub_token'
)
}
response := http.fetch(config) or {
error_message('Failed to fetch data: $err')
exit(1)
}
if response.status_code != 200 {
error_message('Failed to fetch data. Status code: $response.status_code')
exit(1)
}
data := json.decode([]FlistItem, response.body) or {
error_message('Failed to parse JSON: $err')
exit(1)
}
println('Flists for user $hub_user:')
for item in data {
println(item.name)
}
}
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')
@ -232,6 +321,7 @@ fn help() {
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(' help ') + '- Display this help message\n')
println(term.bold('Usage:'))
println(' flist install')
@ -241,6 +331,7 @@ fn help() {
println(' flist push <image>:<tag>')
println(' flist delete <flist_name>')
println(' flist rename <flist_name> <new_flist_name>')
println(' flist ls')
println(' flist help')
}
@ -279,6 +370,7 @@ fn main() {
exit(1)
}
}
'ls' { ls() }
'help' { help() }
else {
error_message('Unknown command: ' + os.args[1])