1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: gen_vc - add purge flag

This commit is contained in:
joe-conigliaro 2019-09-01 02:00:40 +10:00 committed by Alexander Medvednikov
parent b737eb1a87
commit e1dd8716fb

View File

@ -6,6 +6,7 @@
// available command line flags: // available command line flags:
// --work-dir gen_vc's working directory // --work-dir gen_vc's working directory
// --purge force purge the local repositories
// --serve run in webhook server mode // --serve run in webhook server mode
// --port port for http server to listen on // --port port for http server to listen on
// --log-to either 'file' or 'terminal' // --log-to either 'file' or 'terminal'
@ -59,7 +60,7 @@ const(
// default options (overridden by flags) // default options (overridden by flags)
const( const(
// gen_vc working directory // gen_vc working directory
work_dir = '/home/user/dev/gen_vc' work_dir = '/tmp/gen_vc'
// dont push anything to remote repo // dont push anything to remote repo
dry_run = false dry_run = false
// server port // server port
@ -98,6 +99,7 @@ pub mut:
// storage for flag options // storage for flag options
struct FlagOptions { struct FlagOptions {
work_dir string work_dir string
purge bool
serve bool serve bool
port int port int
log_to string log_to string
@ -120,7 +122,6 @@ fn main() {
println(fp.usage()) println(fp.usage())
return return
} }
// webhook server mode // webhook server mode
if flag_options.serve { if flag_options.serve {
vweb.run<WebhookServer>(flag_options.port) vweb.run<WebhookServer>(flag_options.port)
@ -128,6 +129,7 @@ fn main() {
// cmd mode // cmd mode
else { else {
mut gen_vc := new_gen_vc(flag_options) mut gen_vc := new_gen_vc(flag_options)
gen_vc.init()
gen_vc.generate() gen_vc.generate()
} }
} }
@ -138,8 +140,8 @@ fn new_gen_vc(flag_options FlagOptions) &GenVC {
// options // options
options: flag_options options: flag_options
// logger // logger
logger: if log_to == 'file' { logger: if flag_options.log_to == 'file' {
&log.Log{log.DEBUG, log_file} &log.Log{log.DEBUG, flag_options.log_file}
} else { } else {
&log.Log{log.DEBUG, 'terminal'} &log.Log{log.DEBUG, 'terminal'}
} }
@ -151,6 +153,7 @@ pub fn (ws mut WebhookServer) init() {
mut fp := flag.new_flag_parser(os.args.clone()) mut fp := flag.new_flag_parser(os.args.clone())
flag_options := parse_flags(mut fp) flag_options := parse_flags(mut fp)
ws.gen_vc = new_gen_vc(flag_options) ws.gen_vc = new_gen_vc(flag_options)
ws.gen_vc.init()
} }
// gen webhook // gen webhook
@ -167,15 +170,24 @@ pub fn (ws mut WebhookServer) genhook() {
// parse flags to FlagOptions struct // parse flags to FlagOptions struct
fn parse_flags(fp mut flag.FlagParser) FlagOptions { fn parse_flags(fp mut flag.FlagParser) FlagOptions {
return FlagOptions{ return FlagOptions{
serve : fp.bool('serve', false, 'run as a server for webhook when passed') serve : fp.bool('serve', false, 'run in webhook server mode')
work_dir : fp.string('work-dir', work_dir, 'gen_vc working directory') work_dir : fp.string('work-dir', work_dir, 'gen_vc working directory')
purge : fp.bool('purge', false, 'force purge the local repositories')
port : fp.int('port', int(server_port), 'port for web server to listen on') port : fp.int('port', int(server_port), 'port for web server to listen on')
log_to : fp.string('log-to', log_to, 'log to is \'file\' or \'termainl\'') log_to : fp.string('log-to', log_to, 'log to is \'file\' or \'terminal\'')
log_file : fp.string('log_file', log_file, 'log file to use when log-to is \'file\'') log_file : fp.string('log_file', log_file, 'log file to use when log-to is \'file\'')
dry_run : fp.bool('dry-run', dry_run, 'when specified dont push anything to remote repo') dry_run : fp.bool('dry-run', dry_run, 'when specified dont push anything to remote repo')
} }
} }
// init
fn (gen_vc mut GenVC) init() {
// purge repos if flag is passed
if gen_vc.options.purge {
gen_vc.purge_repos()
}
}
// regenerate // regenerate
fn (gen_vc mut GenVC) generate() { fn (gen_vc mut GenVC) generate() {
// set errors to false // set errors to false
@ -196,7 +208,7 @@ fn (gen_vc mut GenVC) generate() {
// cd to gen_vc dir // cd to gen_vc dir
os.chdir(gen_vc.options.work_dir) os.chdir(gen_vc.options.work_dir)
// if we are running not running in serve (webhook) mode // if we are not running with the --serve flag (webhook server)
// rather than deleting and re-downloading the repo each time // rather than deleting and re-downloading the repo each time
// first check to see if the local v repo is behind master // first check to see if the local v repo is behind master
// if it isn't behind theres no point continuing further // if it isn't behind theres no point continuing further
@ -212,7 +224,7 @@ fn (gen_vc mut GenVC) generate() {
} }
// delete repos // delete repos
gen_vc.delete_repos() gen_vc.purge_repos()
// clone repos // clone repos
gen_vc.cmd_exec('git clone --depth 1 https://$git_repo_v $git_repo_dir_v') gen_vc.cmd_exec('git clone --depth 1 https://$git_repo_v $git_repo_dir_v')
@ -308,7 +320,7 @@ fn (gen_vc mut GenVC) command_execute(cmd string, dry bool) string {
gen_vc.logger.error('$err_msg_cmd_x: "$cmd" could not start.') gen_vc.logger.error('$err_msg_cmd_x: "$cmd" could not start.')
gen_vc.logger.error( err ) gen_vc.logger.error( err )
// something went wrong, better start fresh next time // something went wrong, better start fresh next time
gen_vc.delete_repos() gen_vc.purge_repos()
gen_vc.gen_error = true gen_vc.gen_error = true
return '' return ''
} }
@ -316,7 +328,7 @@ fn (gen_vc mut GenVC) command_execute(cmd string, dry bool) string {
gen_vc.logger.error('$err_msg_cmd_x: "$cmd" failed.') gen_vc.logger.error('$err_msg_cmd_x: "$cmd" failed.')
gen_vc.logger.error(r.output) gen_vc.logger.error(r.output)
// something went wrong, better start fresh next time // something went wrong, better start fresh next time
gen_vc.delete_repos() gen_vc.purge_repos()
gen_vc.gen_error = true gen_vc.gen_error = true
return '' return ''
} }
@ -330,10 +342,18 @@ fn (gen_vc mut GenVC) command_execute_dry(cmd string) string {
} }
// delete repo directories // delete repo directories
fn (gen_vc mut GenVC) delete_repos() { fn (gen_vc mut GenVC) purge_repos() {
// delete old repos (better to be fully explicit here, since these are destructive operations) // delete old repos (better to be fully explicit here, since these are destructive operations)
gen_vc.cmd_exec('rm -rf $gen_vc.options.work_dir/$git_repo_dir_v/') mut repo_dir := '$gen_vc.options.work_dir/$git_repo_dir_v'
gen_vc.cmd_exec('rm -rf $gen_vc.options.work_dir/$git_repo_dir_vc/') if os.dir_exists(repo_dir) {
gen_vc.logger.info('purging local repo: "$repo_dir"')
gen_vc.cmd_exec('rm -rf $repo_dir')
}
repo_dir = '$gen_vc.options.work_dir/$git_repo_dir_vc'
if os.dir_exists(repo_dir) {
gen_vc.logger.info('purging local repo: "$repo_dir"')
gen_vc.cmd_exec('rm -rf $repo_dir')
}
} }
// check if file size is too short // check if file size is too short