|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require 'thor'
|
4 |
| -require 'colored' |
5 |
| -require 'fileutils' |
6 |
| -require 'apt/spy2/writer' |
7 |
| -require 'apt/spy2/country' |
8 |
| -require 'apt/spy2/downloader' |
9 |
| -require 'apt/spy2/ubuntu_mirrors' |
10 |
| -require 'apt/spy2/launchpad' |
11 |
| -require 'apt/spy2/request' |
12 |
| -require 'apt/spy2/url' |
| 4 | +require 'apt/spy2/command/fix' |
| 5 | +require 'apt/spy2/command/list' |
| 6 | +require 'apt/spy2/command/check' |
| 7 | +require 'apt/spy2/version' |
13 | 8 |
|
14 | 9 | # apt-spy2 command interface
|
15 | 10 | class AptSpy2 < Thor
|
16 | 11 | package_name 'apt-spy2'
|
17 | 12 | class_option :country, default: 'mirrors'
|
18 |
| - class_option :launchpad, type: :boolean, banner: "Use launchpad's mirror list" |
19 |
| - |
20 |
| - desc 'fix', 'Set the closest/fastest mirror' |
21 |
| - option :commit, type: :boolean |
22 |
| - option :strict, type: :boolean |
23 |
| - def fix |
24 |
| - mirrors = retrieve(options[:country], use_launchpad?(options)) |
25 |
| - working = filter(mirrors, options[:strict], false) |
26 |
| - print 'The closest mirror is: ' |
27 |
| - puts (working[0]).to_s.bold.magenta |
28 |
| - unless options[:commit] |
29 |
| - puts 'Run with --commit to adjust /etc/apt/sources.list'.yellow |
30 |
| - return |
31 |
| - end |
32 |
| - |
33 |
| - puts 'Updating /etc/apt/sources.list'.yellow |
34 |
| - update(working[0]) |
35 |
| - end |
| 13 | + class_option :launchpad, type: :boolean, banner: 'Use launchpad\'s mirror list' |
36 | 14 |
|
37 | 15 | desc 'check', 'Evaluate mirrors'
|
38 |
| - option :output, type: :boolean, default: true |
39 |
| - option :format, default: 'shell' |
40 |
| - option :strict, type: :boolean |
41 |
| - def check |
42 |
| - @writer = Apt::Spy2::Writer.new(options[:format]) |
| 16 | + subcommand 'check', Apt::Spy2::Command::Check |
43 | 17 |
|
44 |
| - mirrors = retrieve(options[:country], use_launchpad?(options)) |
45 |
| - filter(mirrors, options[:strict], options[:output]) |
46 |
| - |
47 |
| - puts @writer.to_json if @writer.json? |
48 |
| - end |
| 18 | + desc 'fix', 'Update sources' |
| 19 | + subcommand 'fix', Apt::Spy2::Command::Fix |
49 | 20 |
|
50 | 21 | desc 'list', 'List the currently available mirrors'
|
51 |
| - option :format, default: 'shell' |
52 |
| - def list |
53 |
| - mirrors = retrieve(options[:country], use_launchpad?(options)) |
54 |
| - |
55 |
| - @writer = Apt::Spy2::Writer.new(options[:format]) |
56 |
| - |
57 |
| - @writer.complete(mirrors) |
58 |
| - |
59 |
| - puts @writer.to_json if @writer.json? |
60 |
| - puts mirrors unless @writer.json? |
61 |
| - end |
| 22 | + subcommand 'list', Apt::Spy2::Command::List |
62 | 23 |
|
63 | 24 | desc 'version', 'Show which version of apt-spy2 is installed'
|
64 | 25 | def version
|
65 | 26 | puts Apt::Spy2::VERSION
|
66 | 27 | exit
|
67 | 28 | end
|
68 |
| - |
69 |
| - private |
70 |
| - |
71 |
| - def retrieve(country = 'mirrors', launchpad = false) |
72 |
| - downloader = Apt::Spy2::Downloader.new |
73 |
| - |
74 |
| - if launchpad |
75 |
| - csv_path = File.expand_path("#{File.dirname(__FILE__)}/../../var/country-names.txt") |
76 |
| - country = Apt::Spy2::Country.new(csv_path) |
77 |
| - name = country.to_country_name(options[:country]) |
78 |
| - |
79 |
| - launchpad = Apt::Spy2::Launchpad.new(downloader.do_download('https://launchpad.net/ubuntu/+archivemirrors')) |
80 |
| - return launchpad.mirrors(name) |
81 |
| - end |
82 |
| - |
83 |
| - country.upcase! if country.length == 2 |
84 |
| - |
85 |
| - ubuntu_mirrors = Apt::Spy2::UbuntuMirrors.new(downloader.do_download("http://mirrors.ubuntu.com/#{country}.txt")) |
86 |
| - ubuntu_mirrors.mirrors(country) |
87 |
| - end |
88 |
| - |
89 |
| - def filter(mirrors, strict = false, output = true) |
90 |
| - # f me :) |
91 |
| - |
92 |
| - working_mirrors = [] |
93 |
| - |
94 |
| - url = Apt::Spy2::Url.new(strict) |
95 |
| - |
96 |
| - mirrors.each do |mirror| |
97 |
| - data = { 'mirror' => mirror } |
98 |
| - |
99 |
| - check = url.adjust!(mirror) |
100 |
| - |
101 |
| - status = broken?(check) |
102 |
| - |
103 |
| - data['status'] = status |
104 |
| - |
105 |
| - working_mirrors << mirror if status == 'up' |
106 |
| - |
107 |
| - @writer.echo(data) if output |
108 |
| - end |
109 |
| - |
110 |
| - working_mirrors |
111 |
| - end |
112 |
| - |
113 |
| - def broken?(url) |
114 |
| - begin |
115 |
| - req = Apt::Spy2::Request.new(url) |
116 |
| - response = req.head |
117 |
| - return 'up' if response.code == '200' |
118 |
| - |
119 |
| - return 'broken' if response.code == '404' |
120 |
| - rescue StandardError |
121 |
| - # connection errors, ssl errors, etc. |
122 |
| - end |
123 |
| - |
124 |
| - 'down' |
125 |
| - end |
126 |
| - |
127 |
| - def update(mirror) |
128 |
| - t = Time.now |
129 |
| - r = `lsb_release -c`.split(' ')[1] |
130 |
| - sources = "## Updated on #{t} by apt-spy2\n" |
131 |
| - sources += "deb #{mirror} #{r} main restricted universe multiverse\n" |
132 |
| - sources += "deb #{mirror} #{r}-updates main restricted universe multiverse\n" |
133 |
| - sources += "deb #{mirror} #{r}-backports main restricted universe multiverse\n" |
134 |
| - sources += "deb #{mirror} #{r}-security main restricted universe multiverse\n" |
135 |
| - |
136 |
| - apt_sources = '/etc/apt/sources.list' |
137 |
| - |
138 |
| - begin |
139 |
| - File.rename apt_sources, "#{apt_sources}.#{t.to_i}" |
140 |
| - File.open(apt_sources, 'w') do |f| |
141 |
| - f.write(sources) |
142 |
| - end |
143 |
| - rescue StandardError |
144 |
| - msg = "Failed updating #{apt_sources}!" |
145 |
| - msg += 'You probably need sudo!' |
146 |
| - raise msg |
147 |
| - end |
148 |
| - |
149 |
| - puts "Updated '#{apt_sources}' with #{mirror}".green |
150 |
| - puts 'Run `apt-get update` to update'.black_on_yellow |
151 |
| - end |
152 |
| - |
153 |
| - def use_launchpad?(options) |
154 |
| - return false unless options[:launchpad] |
155 |
| - |
156 |
| - if options[:country] && options[:country] == 'mirrors' |
157 |
| - raise 'Please supply a `--country=foo`. Launchpad cannot guess!' |
158 |
| - end |
159 |
| - |
160 |
| - true |
161 |
| - end |
162 | 29 | end
|
0 commit comments