vget fixes

* refactor(tools): remove trailing spaces in vget.v

* refactor(tools): fix mix tabs and spaces

* fix(tools): vget should exit non-zero when call with invalid arguments

* refactor(tools): extract vmodules directory create and chdir logic

* feat(tools): add travis-ci check to vget
pull/2121/head
Dongliang Ma 2019-09-28 19:21:52 +08:00 committed by Alexander Medvednikov
parent 4b03abdaff
commit 46ac22f3e5
2 changed files with 46 additions and 25 deletions

View File

@ -87,3 +87,10 @@ script:
git clone https://github.com/vlang/vid git clone https://github.com/vlang/vid
cd vid && ../v -debug -o vid . cd vid && ../v -debug -o vid .
fi fi
- |
if [[ "${TRAVIS_OS_NAME}" != "windows" ]]; then
v install nedpals.args
# ensure nedpals.args is installed
[[ -d ~/.vmodules/nedpals/args]]
git remote -v | grep 'https://github.com/nedpals/v-args (fetch)'
fi

View File

@ -1,62 +1,76 @@
module main module main
import ( import (
http http
os os
json json
) )
const ( const (
//url = 'http://localhost:8089' //url = 'http://localhost:8089'
url = 'https://vpm.best' url = 'https://vpm.best'
) )
struct Mod { struct Mod {
id int id int
name string name string
url string url string
nr_downloads int nr_downloads int
}
fn get_vmodules_dir_path() string {
home := os.home_dir()
return '${home}.vmodules'
}
fn ensure_vmodules_dir_exist() {
home_vmodules := get_vmodules_dir_path()
if !os.dir_exists( home_vmodules ) {
println('Creating $home_vmodules/ ...')
os.mkdir(home_vmodules)
}
}
fn change_to_vmodules_dir() {
os.chdir(get_vmodules_dir_path())
} }
fn main() { fn main() {
if os.args.len <= 1 { if os.args.len <= 1 {
println('usage: vget module [module] [module] [...]') println('usage: vget module [module] [module] [...]')
return exit(2)
}
home := os.home_dir()
home_vmodules := '${home}.vmodules'
if !os.dir_exists( home_vmodules ) {
println('Creating $home_vmodules/ ...')
os.mkdir(home_vmodules)
} }
os.chdir(home_vmodules)
ensure_vmodules_dir_exist()
change_to_vmodules_dir()
mut errors := 0 mut errors := 0
names := os.args.slice(1, os.args.len) names := os.args.slice(1, os.args.len)
for name in names { for name in names {
modurl := url + '/jsmod/$name' modurl := url + '/jsmod/$name'
r := http.get(modurl) or { panic(err) } r := http.get(modurl) or { panic(err) }
if r.status_code == 404 { if r.status_code == 404 {
println('Skipping module "$name", since $url reported that "$name" does not exist.') println('Skipping module "$name", since $url reported that "$name" does not exist.')
errors++ errors++
continue continue
} }
if r.status_code != 200 { if r.status_code != 200 {
println('Skipping module "$name", since $url responded with $r.status_code http status code. Please try again later.') println('Skipping module "$name", since $url responded with $r.status_code http status code. Please try again later.')
errors++ errors++
continue continue
} }
s := r.text s := r.text
mod := json.decode(Mod, s) or { mod := json.decode(Mod, s) or {
errors++ errors++
println('Skipping module "$name", since its information is not in json format.') println('Skipping module "$name", since its information is not in json format.')
continue continue
} }
if( '' == mod.url || '' == mod.name ){ if( '' == mod.url || '' == mod.name ){
errors++ errors++
// a possible 404 error, which means a missing module? // a possible 404 error, which means a missing module?
@ -64,7 +78,7 @@ fn main() {
continue continue
} }
final_module_path := '$home_vmodules/' + mod.name.replace('.', '/') final_module_path := get_vmodules_dir_path() + '/' + mod.name.replace('.', '/')
println('Installing module "$name" from $mod.url to $final_module_path ...') println('Installing module "$name" from $mod.url to $final_module_path ...')
_ = os.exec('git clone --depth=1 $mod.url $final_module_path') or { _ = os.exec('git clone --depth=1 $mod.url $final_module_path') or {