forked from vieter-v/vieter
				
			Split Git client code into separate module
							parent
							
								
									b31b4cbd7a
								
							
						
					
					
						commit
						fc1d4480dc
					
				| 
						 | 
					@ -2,9 +2,6 @@ module git
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cli
 | 
					import cli
 | 
				
			||||||
import env
 | 
					import env
 | 
				
			||||||
import net.http
 | 
					 | 
				
			||||||
import json
 | 
					 | 
				
			||||||
import response
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Config {
 | 
					struct Config {
 | 
				
			||||||
	address string [required]
 | 
						address string [required]
 | 
				
			||||||
| 
						 | 
					@ -55,18 +52,8 @@ pub fn cmd() cli.Command {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn get_repos(conf Config) ?map[string]GitRepo {
 | 
					 | 
				
			||||||
	mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ?
 | 
					 | 
				
			||||||
	req.add_custom_header('X-API-Key', conf.api_key) ?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	res := req.do() ?
 | 
					 | 
				
			||||||
	data := json.decode(response.Response<map[string]GitRepo>, res.text) ?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return data.data
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn list(conf Config) ? {
 | 
					fn list(conf Config) ? {
 | 
				
			||||||
	repos := get_repos(conf) ?
 | 
						repos := get_repos(conf.address, conf.api_key) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for id, details in repos {
 | 
						for id, details in repos {
 | 
				
			||||||
		println('${id[..8]}\t$details.url\t$details.branch\t$details.arch')
 | 
							println('${id[..8]}\t$details.url\t$details.branch\t$details.arch')
 | 
				
			||||||
| 
						 | 
					@ -74,17 +61,13 @@ fn list(conf Config) ? {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn add(conf Config, url string, branch string, arch []string) ? {
 | 
					fn add(conf Config, url string, branch string, arch []string) ? {
 | 
				
			||||||
	mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch&arch=${arch.join(',')}',
 | 
						res := add_repo(conf.address, conf.api_key, url, branch, arch) ?
 | 
				
			||||||
		'') ?
 | 
					 | 
				
			||||||
	req.add_custom_header('X-API-Key', conf.api_key) ?
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res := req.do() ?
 | 
						println(res.message)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	println(res.text)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn remove(conf Config, id_prefix string) ? {
 | 
					fn remove(conf Config, id_prefix string) ? {
 | 
				
			||||||
	repos := get_repos(conf) ?
 | 
						repos := get_repos(conf.address, conf.api_key) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mut to_remove := []string{}
 | 
						mut to_remove := []string{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -104,11 +87,7 @@ fn remove(conf Config, id_prefix string) ? {
 | 
				
			||||||
		exit(1)
 | 
							exit(1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mut req := http.new_request(http.Method.delete, '$conf.address/api/repos/${to_remove[0]}',
 | 
						res := remove_repo(conf.address, conf.api_key, to_remove[0]) ?
 | 
				
			||||||
		'') ?
 | 
					 | 
				
			||||||
	req.add_custom_header('X-API-Key', conf.api_key) ?
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res := req.do() ?
 | 
						println(res.message)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	println(res.text)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,36 @@
 | 
				
			||||||
 | 
					module git
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import response { Response }
 | 
				
			||||||
 | 
					import net.http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn get_repos(address string, api_key string) ?map[string]GitRepo {
 | 
				
			||||||
 | 
						mut req := http.new_request(http.Method.get, '$address/api/repos', '') ?
 | 
				
			||||||
 | 
						req.add_custom_header('X-API-Key', api_key) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res := req.do() ?
 | 
				
			||||||
 | 
						data := json.decode(Response<map[string]GitRepo>, res.text) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data.data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn add_repo(address string, api_key string, url string, branch string, arch []string) ?Response<string> {
 | 
				
			||||||
 | 
						mut req := http.new_request(http.Method.post, '$address/api/repos?url=$url&branch=$branch&arch=${arch.join(',')}',
 | 
				
			||||||
 | 
							'') ?
 | 
				
			||||||
 | 
						req.add_custom_header('X-API-Key', api_key) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res := req.do() ?
 | 
				
			||||||
 | 
						data := json.decode(Response<string>, res.text) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn remove_repo(address string, api_key string, id string) ?Response<string> {
 | 
				
			||||||
 | 
						mut req := http.new_request(http.Method.delete, '$address/api/repos/$id', '') ?
 | 
				
			||||||
 | 
						req.add_custom_header('X-API-Key', api_key) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res := req.do() ?
 | 
				
			||||||
 | 
						data := json.decode(Response<string>, res.text) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
module response
 | 
					module response
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Response<T> {
 | 
					pub struct Response<T> {
 | 
				
			||||||
 | 
					pub:
 | 
				
			||||||
	message string
 | 
						message string
 | 
				
			||||||
	data    T
 | 
						data    T
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue