script_new_user.R~ 1.30 KiB
#!/usr/bin/env Rscript
library(here)
library(argparser, quietly=TRUE) 
library(stringr)
setwd(here())
# Create a parser
p = arg_parser("Add a new user to the environment with is work and data path.")
# Add command line arguments
p = add_argument(p, '--data_dir',
                 type="character",
                 help="Data directory.")
p = add_argument(p, '--work_dir',
                 type="character",
                 help="Working directory.")
# Parse the command line arguments
argv = parse_args(p)
# Create path file
user = Sys.getenv('USER')
computer_data_path = c(argv$data_dir)
computer_work_path = c(argv$work_dir)
if (file.exists('path.txt')) {
     computer_path = read.table('path.txt', sep=';')
     if (!any(computer_path$user == user)) {
         computer_path = rbind(computer_path, c(user, 
                                                computer_data_path,
                                                computer_work_path))
         } else {
             stop("User already register")            
} else {
    computer_path = data.frame(user=user,
                               computer_data_path=computer_data_path,
                               computer_work_path=computer_work_path)
    print(paste('New user:', user, sep=' '))
write.table(computer_path, file='path.txt', sep=';')