Class: GistSweep::Sweep

Inherits:
Object
  • Object
show all
Defined in:
lib/gist/sweep.rb

Instance Method Summary collapse

Instance Method Details

#load_github_api(config, file_path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gist/sweep.rb', line 69

def load_github_api(config, file_path)
  if config["oauth_token"] and !config["oauth_token"].empty?
    Octokit::Client.new(:access_token => config["oauth_token"])
  else
    puts "You need to setup a 'Personal Access Token' to use with gist-sweep"
    puts "I'll wait for you to paste one in: "
    code = STDIN.gets.strip
    write_config_file(code, file_path)
    Octokit::Client.new(:access_token => code)
  end
end

#parse_argumentsObject



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
45
46
47
# File 'lib/gist/sweep.rb', line 9

def parse_arguments()
  options = {
    :verbose => false,
    :config_file => "~/.gist-sweep",
    :public => false,
    :days => 90
  }

  OptionParser.new do |opts|
    opts.banner = "Usage: gist-sweep [options]"

    opts.on("-v", "-s", "Print verbose messages") do |v|
      options[:verbose] = v
    end

    opts.on("-V", "Show the version") do |v|
      puts "gist-sweep: #{Gist::Sweep::VERSION}"
    end

    opts.on("-c", "Read config from a different file") do |c|
      options[:config_file] = c
    end

    opts.on("-u USERNAME", "Username to sweep gists on") do |u|
      options[:username] = u
    end

    opts.on("-d NUM", "Days to keep (default=90)", Integer) do |d|
      options[:days] = d.to_i
    end

    opts.on("-p", "Include public gists") do |p|
      options[:public] = p
    end

  end.parse!

  options
end

#promot_to_remove_gists(gists) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gist/sweep.rb', line 81

def promot_to_remove_gists(gists)
  gists.each do |g|
    puts "#{g[:updated_at]} (#{g[:id]}) -- #{g[:description]}"
  end
  print "Remove #{gists.size} gists? (y/n) "
  line = STDIN.gets.strip
  if line == 'y'
    true
  else
    false
  end
end

#read_config_from_file(path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gist/sweep.rb', line 49

def read_config_from_file(path)
  expanded_path = File.expand_path(path)
  if File.exists?(expanded_path)
    contents = File.read(expanded_path)
    if contents.chomp.empty?
      {}
    else
      JSON.parse(contents)
    end
  else
    {}
  end
end

#sweep(raw_args) ⇒ Object



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
# File 'lib/gist/sweep.rb', line 100

def sweep(raw_args)
  args = parse_arguments()
  config = read_config_from_file(args[:config_file])
  github = load_github_api(config, args[:config_file])

  # pattern will be the first item in ARGV after options are parsed
  pattern = ARGV[0]

  if args[:username]
    min_age = DateTime.now - args[:days]
    verbose_log(args, "Removing gists older than #{min_age}")

    pattern_matcher = Regexp.new(pattern || "")

    gists_to_remove = github.gists(args[:username]).select do |g|
      remove_already = (!g[:public] or args[:public]) && (g[:updated_at].to_datetime < min_age)
      if pattern
        remove_already && pattern_matcher.match(g[:description])
      else
        remove_already
      end
    end

    if gists_to_remove.empty?
      puts "No gists to remove"
    else
      if promot_to_remove_gists(gists_to_remove)
        gists_to_remove.each do |g|
          verbose_log(args, "Deleting gist #{g[:id]}")
          github.delete_gist(g[:id])
        end
        puts "Swept gists."
      end
    end
  else
    abort "No username defined (with -u flag)."
  end
end

#verbose_log(args, msg) ⇒ Object



94
95
96
97
98
# File 'lib/gist/sweep.rb', line 94

def verbose_log(args, msg)
  if args[:verbose]
    puts msg
  end
end

#write_config_file(oauth_token, path) ⇒ Object



63
64
65
66
67
# File 'lib/gist/sweep.rb', line 63

def write_config_file(oauth_token, path)
  expanded_path = File.expand_path(path)
  File.write(expanded_path, JSON.generate({:oauth_token => oauth_token}))
  FileUtils.chmod(0400, expanded_path)
end