Class: Rawfeed::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/rawfeed/post.rb

Class Method Summary collapse

Class Method Details

.move_file(file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rawfeed/post.rb', line 46

def self.move_file(file)
  filename = File.basename(file)
  destination = File.join(Rawfeed::CONFIG['POSTS_DIR'], filename)

  if File.exist?(destination)
    puts "[!] The file #{filename} already exists in #{Rawfeed::CONFIG['POSTS_DIR']}".yellow
    return
  end

  FileUtils.mv(file, destination)
  puts "[*] #{filename} moved to #{Rawfeed::CONFIG['POSTS_DIR']}".green

end

.postObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rawfeed/post.rb', line 8

def self.post
  drafts = Dir.glob(File.join(Rawfeed::CONFIG['DRAFTS_DIR'], "*.#{Rawfeed::CONFIG['markdown_extension']}"))

  if drafts.empty?
    puts "No files found in #{DRAFTS_DIR}".yellow
    return
  end

  puts "Select the file to move:\n".cyan

  drafts.each_with_index do |file, index|
    puts "#{index + 1} - #{File.basename(file)}"
  end
  puts "#{drafts.size + 1} - move all"
  puts "#{drafts.size + 2} - cancel"
  print "\nEnter option number: ".cyan

  # capture ctrl+c
  trap("INT") do
    puts "\n[!] Operation canceled by user (Ctrl+C).".yellow
    exit!
  end

  choice = STDIN.gets.strip.to_i

  if choice == drafts.size + 1
    drafts.each { |file| move_file(file) }
    puts "\n[*] All drafts have been moved to #{Rawfeed::CONFIG['POSTS_DIR']}".green
  elsif choice == drafts.size + 2
    puts "\n[!] Operation canceled by user.".yellow
    exit!
  elsif choice.between?(1, drafts.size)
    move_file(drafts[choice - 1])
  else
    puts "\n[x] Invalid option.".red
  end
end