Module: Slimtimercli::Helper
- Included in:
- CommandLine
- Defined in:
- lib/slimtimercli/helper.rb
Instance Method Summary collapse
- #check_and_create_dir ⇒ Object
- #config_file ⇒ Object
- #current_file ⇒ Object
- #dump_to_file(object, file) ⇒ Object
- #load_config ⇒ Object
- #load_file(file) ⇒ Object
- #login ⇒ Object
- #parse(args) ⇒ Object
- #rm_current ⇒ Object
- #root ⇒ Object
- #save_config(config) ⇒ Object
- #tasks_file ⇒ Object
Instance Method Details
#check_and_create_dir ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/slimtimercli/helper.rb', line 32 def check_and_create_dir raise "Home DIR not set!" unless ENV["HOME"] unless File.directory?(root) FileUtils.mkdir(root) end end |
#config_file ⇒ Object
20 21 22 |
# File 'lib/slimtimercli/helper.rb', line 20 def config_file File.join(root, "config.yml") end |
#current_file ⇒ Object
28 29 30 |
# File 'lib/slimtimercli/helper.rb', line 28 def current_file File.join(root, "current.yml") end |
#dump_to_file(object, file) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/slimtimercli/helper.rb', line 59 def dump_to_file(object, file) check_and_create_dir File.open( file, 'w' ) do |out| YAML.dump(object, out ) end end |
#load_config ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/slimtimercli/helper.rb', line 40 def load_config check_and_create_dir unless File.exists?(config_file) File.open( config_file, 'w' ) do |out| YAML.dump({}, out ) end end load_file(config_file) end |
#load_file(file) ⇒ Object
55 56 57 |
# File 'lib/slimtimercli/helper.rb', line 55 def load_file(file) File.open( file ) { |yf| YAML::load( yf ) } end |
#login ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/slimtimercli/helper.rb', line 3 def login config = Helper::load_config st = SlimTimer.new(config["email"], config["password"], config["api_key"]) st.login st end |
#parse(args) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/slimtimercli/helper.rb', line 71 def parse(args) if !args || args.empty? warn "Need to specify arguments, run slimtimer -h for help" exit 2 end = OpenStruct.new .force = false opts = OptionParser.new do |opts| opts. = "Usage: slimtimer [options]" opts.on("-s TASK", "--start TASK", "Start a TASK given by the task name") do |t| .run = "start" .task_name = t end opts.on("-c TASK", "--create TASK", "Create a ne task by the given name") do |t| .run = "create" .task_name = t end opts.on("-e", "--end" ,"Stops time recording for the given task") do .run = "stop" end opts.on("-t", "--tasks", "Prints all available tasks") do .run = "tasks" end opts.on("-f", "--force", "Force deletion of tasks") do .force = true end opts.on("--setup", "Setup your account") do .run = "setup" end opts.on_tail("-h", "Shows this note") do puts opts exit end opts.on("--help", "Show verbose help") do @out.puts <<-HELP SlimTimer is a tool to record your time spend on a task. SlimTimer CLI allows you to controll your SlimTimer directly from where you spend most of your time - on the command line. To use SlimTimer proceed with the following steps: The first time you need to setup SlimTimer CLI with slimtimer setup Now it will ask for your email and password and API key to use with your account. These information will be stored in #{config_file} To create a task run slimtimer create_task my_shiny_task To spend some time on the task you have to make the timer run slimtimer start my_shiny_task When you finished working on a task, you can call slimtimer end This will write the time spend back to SlimTimer.com. Finally you can run slimtimer tasks To show all your tasks available. HELP exit end end begin opts.parse!(args) rescue puts $!. exit end end |
#rm_current ⇒ Object
66 67 68 69 |
# File 'lib/slimtimercli/helper.rb', line 66 def rm_current FileUtils.rm(current_file) if File.exists?(current_file) end |
#root ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/slimtimercli/helper.rb', line 12 def root @root ||= if ENV.key?("XDG_CONFIG_HOME") and File.exists?( ENV['XDG_CONFIG_HOME'] ) File.join(ENV['XDG_CONFIG_HOME'], "slimtimer") else File.join(ENV["HOME"], ".slimtimer") end end |
#save_config(config) ⇒ Object
51 52 53 |
# File 'lib/slimtimercli/helper.rb', line 51 def save_config(config) dump_to_file(config, config_file) end |
#tasks_file ⇒ Object
24 25 26 |
# File 'lib/slimtimercli/helper.rb', line 24 def tasks_file File.join(root, "tasks.yml") end |