Skip to content

Commit

Permalink
tools: use a safer way to initialise settings in vpm (#19755)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 5, 2023
1 parent d1f044d commit b734197
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/tools/vpm/common.v
Expand Up @@ -234,7 +234,7 @@ fn ensure_vcs_is_installed(vcs &VCS) ! {
}

fn increment_module_download_count(name string) ! {
if no_dl_count_increment {
if settings.no_dl_count_increment {
println('Skipping download count increment for "${name}".')
return
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/tools/vpm/install.v
Expand Up @@ -5,7 +5,7 @@ import v.vmod
import v.help
import net.urllib

fn vpm_install(requested_modules []string, opts []string) {
fn vpm_install(requested_modules []string) {
if settings.is_help {
help.print_and_exit('vpm')
}
Expand Down Expand Up @@ -34,7 +34,7 @@ fn vpm_install(requested_modules []string, opts []string) {
mut vpm_modules := modules.filter(it !in external_modules)
installed_modules := get_installed_modules()

if installed_modules.len > 0 && '--once' in opts {
if installed_modules.len > 0 && settings.is_once {
mut already_installed := []string{}
if external_modules.len > 0 {
mut i_deleted := []int{}
Expand Down Expand Up @@ -79,8 +79,7 @@ fn vpm_install(requested_modules []string, opts []string) {
vpm_install_from_vpm(vpm_modules)
}
if external_modules.len > 0 {
vcs := if '--hg' in opts { supported_vcs['hg'] } else { supported_vcs['git'] }
vpm_install_from_vcs(external_modules, vcs)
vpm_install_from_vcs(external_modules, supported_vcs[settings.vcs])
}
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/tools/vpm/settings.v
@@ -0,0 +1,30 @@
module main

import os
import os.cmdline

struct VpmSettings {
mut:
is_help bool
is_once bool
is_verbose bool
server_urls []string
vcs string
vmodules_path string
no_dl_count_increment bool
}

fn init_settings() VpmSettings {
args := os.args[1..]
opts := cmdline.only_options(args)
cmds := cmdline.only_non_options(args)
return VpmSettings{
is_help: '-h' in opts || '--help' in opts || 'help' in cmds
is_once: '--once' in opts
is_verbose: '-v' in opts
vcs: if '--hg' in opts { 'hg' } else { 'git' }
server_urls: cmdline.options(args, '--server-urls')
vmodules_path: os.vmodules_dir()
no_dl_count_increment: os.getenv('VPM_NO_INCREMENT') == '1'
}
}
26 changes: 2 additions & 24 deletions cmd/tools/vpm/vpm.v
Expand Up @@ -9,14 +9,6 @@ import rand
import v.help
import v.vmod

struct VpmSettings {
mut:
is_help bool
is_verbose bool
server_urls []string
vmodules_path string
}

struct VCS {
dir string
cmd string
Expand All @@ -29,8 +21,7 @@ struct VCS {
}

const (
settings = &VpmSettings{}
no_dl_count_increment = os.getenv('VPM_NO_INCREMENT') == '1'
settings = init_settings()
default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com']
vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly
valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated', 'list',
Expand Down Expand Up @@ -61,12 +52,10 @@ const (
)

fn main() {
init_settings()
// This tool is intended to be launched by the v frontend,
// which provides the path to V inside os.getenv('VEXE')
// args are: vpm [options] SUBCOMMAND module names
params := cmdline.only_non_options(os.args[1..])
options := cmdline.only_options(os.args[1..])
// dump(params)
if params.len < 1 {
help.print_and_exit('vpm', exit_code: 5)
Expand All @@ -82,7 +71,7 @@ fn main() {
vpm_search(requested_modules)
}
'install' {
vpm_install(requested_modules, options)
vpm_install(requested_modules)
}
'update' {
vpm_update(requested_modules)
Expand Down Expand Up @@ -113,17 +102,6 @@ fn main() {
}
}

fn init_settings() {
mut s := &VpmSettings(unsafe { nil })
unsafe {
s = settings
}
s.is_help = '-h' in os.args || '--help' in os.args || 'help' in os.args
s.is_verbose = '-v' in os.args
s.server_urls = cmdline.options(os.args, '-server-url')
s.vmodules_path = os.vmodules_dir()
}

fn vpm_upgrade() {
outdated := get_outdated() or { exit(1) }
if outdated.len > 0 {
Expand Down

0 comments on commit b734197

Please sign in to comment.