Class: TaskLoop::Env

Inherits:
Command
  • Object
show all
Defined in:
lib/taskloop/command/env.rb

Constant Summary

Constants inherited from Command

Command::DOLPHIN, Command::LOGO

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#create_dir_if_needed, #create_file_if_needed, #create_taskloop_file_structure_if_needed, #taskloop_cron_log_path, #taskloop_cron_tab_path, #taskloop_data_dir, #taskloop_data_proj_dirs, #taskloop_dir, #taskloop_environments_path, #taskloop_proj_list_dirs, #taskloop_proj_list_path, #taskloop_taskfile_paths, #tasklooprc_path

Constructor Details

#initialize(argv) ⇒ Env

Returns a new instance of Env.



22
23
24
25
26
# File 'lib/taskloop/command/env.rb', line 22

def initialize(argv)
  @import = argv.option('import')
  @remove = argv.option('remove')
  super
end

Class Method Details

.optionsObject



15
16
17
18
19
20
# File 'lib/taskloop/command/env.rb', line 15

def self.options
  [
    ['--import=VAR1,VAR2...', 'Import one or more global environment variables into taskloop.'],
    ["--remove=VAR1,VAR2...", 'Remove one or more global environment variables from taskloop.']
  ].concat(super)
end

Instance Method Details

#import_environment_variablesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/taskloop/command/env.rb', line 54

def import_environment_variables
  env_list = @import.split(',')
  unless env_list.length > 0
    puts "Warning: the global environment variables you import is empty. Please check the option arguments again.".ansi.yellow
    return
  end
  env_file = File.open(taskloop_environments_path, "a")
  env_list.each do |var|
    puts "importing #{var} ...".ansi.blue
    puts "    #{var}=#{ENV[var]}".ansi.blue
    env_file.puts "export #{var}=#{ENV[var]}"
  end
  puts ""
  puts "import global environment variables complete.".ansi.blue
  env_file.close
end

#list_environment_variablesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/taskloop/command/env.rb', line 96

def list_environment_variables
  env_list = {}
  env_file = File.open(taskloop_environments_path, "r")
  pattern = /\Aexport /
  env_file.each_line do |line|
    if line.match(pattern)
      left = line.index(" ")
      right = line.index("=")
      if left && right
        name = line[left+1..right-1]
        value = line[left+1..-1]
        env_list[name] = value
      end
    end
  end
  if env_list.empty?
    puts "Warning: no global environment variable imported in taskloop.".ansi.blue
    return
  end
  puts "There are the global environments variables imported in taskloop:".ansi.blue
  env_list.each do |k, v|
    puts v
  end
end

#remove_environment_variablesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/taskloop/command/env.rb', line 71

def remove_environment_variables
  env_list = @remove.split(',')
  unless env_list.length > 0
    puts "Warning: the global environment variables you import is empty. Please check the option arguments again.".ansi.yellow
    return
  end

  file_content = []
  File.open(taskloop_environments_path, "r") do |file|
    file.each_line do |line|
      name = get_environment_variable_from_line(line)
      unless env_list.include?(name)
        file_content.push(line)
      end
    end
  end

  File.open(taskloop_environments_path, "w") do |file|
    file_content.each do |line|
      file.puts line
    end
  end
  puts "remove global environment variables complete.".ansi.blue
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/taskloop/command/env.rb', line 28

def run
  super
  if @import == nil && @remove == nil
    list_environment_variables
    return
  end

  if @import
    import_environment_variables
  end

  if @remove
    remove_environment_variables
  end
end

#validate!Object



44
45
46
47
48
49
50
51
52
# File 'lib/taskloop/command/env.rb', line 44

def validate!
  super
  if @export && @list
    help! "The --global-export option and the --global-list option cannot be used simultaneously."
  end
  if @remove && @list
    help! "The --global-remove option and the --global-list option cannot be used simultaneously."
  end
end