update_repo 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf4845833185e2067044f7580d23908be0d37348
4
- data.tar.gz: 77f8f8759f3935e8b05554b6ad9b0ccf2d3812da
3
+ metadata.gz: e3822c75393026d76642b13ef90987f7cfbf19d7
4
+ data.tar.gz: d09ea3b9e447eac5b1e0db00ad0535a09702f804
5
5
  SHA512:
6
- metadata.gz: 950adcd0cf990f8db0d05c1ca868ae331d17a87c4a406754ba3972ea78863c84346d2cae93cdc1dcbb2f3b787e2211122391ca1bbcb6ce9d8c7d09117d6fb476
7
- data.tar.gz: 595ff551349993782849041945d28d6a4a49b38f4cfe50aa58ab77ccd231f842f4abeb85be0321932e7fc4f114541ce1e58c14d1a23bfe93144bc892183a46dd
6
+ metadata.gz: 9f59a14c1b0639a846520f1283c13d6e5f6eb2b98ca271a65f48089ea3d49481286b79f61866858bc7e60aa750552aad4db97384d75e42c235eff1d022409298
7
+ data.tar.gz: 2a16193e25d7b68e13520b8cd6bfa83452de675b372f2d7864f900142fa72bcd5b8a917c66571855e29145adc33d95f5f5bdb7d0ea54ef4fe3dbea3fa829c81c
data/.gitignore CHANGED
File without changes
data/.reek CHANGED
@@ -7,3 +7,6 @@ UtilityFunction:
7
7
 
8
8
  RepeatedConditional:
9
9
  max_ifs: 3
10
+
11
+ DuplicateMethodCall:
12
+ max_calls: 3
data/.rubocop.yml CHANGED
File without changes
data/.travis.yml CHANGED
File without changes
data/README.md CHANGED
@@ -11,15 +11,23 @@ A Simple Gem to keep multiple locally-cloned Git Repositories up to date.
11
11
  This is the conversion to a Gem of one of my standalone Ruby scripts. Still very much a work in progress but the required basic functionality is there.
12
12
  The script will simply run `git pull` on every local clone of a git repository that it finds under the specified directory or directories.
13
13
 
14
- ## Usage
14
+ ## Installation
15
15
 
16
16
  #### Pre-requirements
17
17
 
18
- It goes without saying that at the very least a working copy of both [`Git`][git] and [`Ruby`][ruby] need to be installed on your machine. Also, the script has only been tested under Linux, not windows.
18
+ It goes without saying that at the very least a working copy of both [`Git`][git] and [`Ruby`][ruby] need to be installed on your machine. Also, the script has currently only been tested under Linux, not windows.
19
19
 
20
20
  [git]: http://git-scm.com
21
21
  [ruby]: http://www.ruby-lang.org
22
22
 
23
+ Install this from the shell prompt as you would any other Ruby Gem
24
+
25
+ ```
26
+ $ gem install update_repo
27
+ ```
28
+
29
+ ## Usage
30
+
23
31
  #### Quick start
24
32
  Create a [YAML](http://yaml.org/)-formatted configuration file `.updaterepo` **in your home directory** that contains at least a 'location' tag pointing to the directory containing the git repositories you wish to have updated :
25
33
  ```yaml
@@ -57,6 +65,16 @@ exceptions:
57
65
  - update_repo
58
66
  ```
59
67
 
68
+ `log:` - Log all output to the file `./.updaterepo`, defaults to FALSE (optional)
69
+ ```yaml
70
+ log: true
71
+ ```
72
+
73
+ `timestamp:` - timestamp the output files instead of overwriting them, defaults to FALSE (optional)
74
+ ```yaml
75
+ timestamp: true
76
+ ```
77
+
60
78
  #### Command line switches
61
79
  Options are not required. If none are specified then the program will read from the standard configuration file (~/.updaterepo) and automatically update the specified Repositories.
62
80
 
@@ -67,8 +85,6 @@ Options:
67
85
  -d, --dump Dump a list of Directories and Git URL's to STDOUT in CSV format
68
86
  -p, --prune=<i> Number of directory levels to remove from the --dump output.
69
87
  Only valid when --dump or -d specified (Default: 0)
70
- -i, --import Import a previous dump of directories and Git repository URL's,
71
- (created using --dump) then proceed to clone them locally.
72
88
  -l, --log Create a logfile of all program output to './update_repo.log'.
73
89
  Any older logs will be overwritten.
74
90
  -t, --timestamp Timestamp the logfile instead of overwriting. Does nothing unless the
@@ -78,14 +94,9 @@ Options:
78
94
  ```
79
95
 
80
96
  ## To-Do
81
- Not in any specific order :
97
+ Add functionality, not in any specific order :
82
98
 
83
- - Improve error-checking and recovery while parsing the configuration file
84
- * Ignore and report invalid or missing directories
85
99
  - Either add an option 'variants' or similar to allow non-standard git pull commands (eg Ubuntu kernel), or update the 'exceptions' option to do same.
86
- - Error checking and reporting for the git processes `[IN PROGRESS]`
87
- * Add more failure cases, not all git errors fail with "fatal:"
88
- - retry for connection issues etc (config setting).
89
100
  - Improve the stats / info at end-of-job :
90
101
  * errors or connection problems `[IN PROGRESS]`
91
102
  * _more..._
@@ -100,7 +111,16 @@ Not in any specific order :
100
111
  * re-import the above dump on a different machine or after reinstall
101
112
  - Add option to use alternative git command if required, either globally or on a case-by-case basis (see also comments on 'variants' above). Currently the script just uses a blanket `git pull` command on all repositories.
102
113
  - Document configuration file format and options.
114
+
115
+ Internal Changes and refactoring :
103
116
  - Add testing!
117
+ - Refactor all command-line handling into a separate class and file
118
+ - Error checking and reporting for the git processes `[IN PROGRESS]`
119
+ - Improve error-checking and recovery while parsing the configuration file
120
+ * Ignore and report invalid or missing directories
121
+ * Add more failure cases, not all git errors fail with "fatal:"
122
+ - Retry for connection issues etc (config setting).
123
+ - Fix functionality so that command line options will override those in the config file
104
124
 
105
125
  [confoog]: http://confoog.seapagan.net
106
126
 
data/Rakefile CHANGED
File without changes
data/lib/update_repo.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'update_repo/version'
2
2
  require 'update_repo/helpers'
3
+ require 'update_repo/cmd_config'
3
4
  require 'yaml'
4
5
  require 'colorize'
5
6
  require 'confoog'
@@ -24,18 +25,10 @@ module UpdateRepo
24
25
  start_time: 0 }
25
26
  @summary = { processed: 'green', updated: 'cyan', skipped: 'yellow',
26
27
  failed: 'red' }
27
- # read the options from Trollop and store in temp variable.
28
- # we do it this way around otherwise if configuration file is missing it
29
- # gives the error messages even on '--help' and '--version'
30
- temp_opt = set_options
31
- # @config - Class. Reads the configuration from a file in YAML format and
32
- # allows easy access to the configuration data
33
- @config = Confoog::Settings.new(filename: CONFIG_FILE,
34
- prefix: 'update_repo',
35
- autoload: true, autosave: false)
36
- # store the command line variables in a configuration variable
37
- @config['cmd'] = temp_opt
38
- config_error unless @config.status[:errors] == Status::INFO_FILE_LOADED
28
+ # create a new instance of the CmdConfig class then read the config var
29
+ @cmd = CmdConfig.new
30
+ # set up the logfile if needed
31
+ setup_logfile if cmd(:log)
39
32
  end
40
33
 
41
34
  # This function will perform the required actions to traverse the Repo.
@@ -43,86 +36,40 @@ module UpdateRepo
43
36
  # walk_repo = UpdateRepo::WalkRepo.new
44
37
  # walk_repo.start
45
38
  def start
46
- String.disable_colorization = true unless param_set('color')
39
+ String.disable_colorization = !cmd(:color)
47
40
  # make sure we dont have bad cmd-line parameter combinations ...
48
- check_params
49
- # print out our header ...
50
- show_header unless dumping? || importing?
51
- @config['location'].each do |loc|
41
+ @cmd.check_params
42
+ # print out our header unless we are dumping / importing ...
43
+ no_header = cmd(:dump) || cmd(:import)
44
+ show_header unless no_header
45
+ config['location'].each do |loc|
52
46
  recurse_dir(loc)
53
47
  end
54
- # print out an informative footer ...
55
- footer unless dumping? || importing?
48
+ # print out an informative footer unless dump / import ...
49
+ footer unless no_header
56
50
  end
57
51
 
58
- private
52
+ # private
59
53
 
60
- def check_params
61
- setup_logfile if logging?
62
- if dumping? && importing?
63
- Trollop.die 'Sorry, you cannot specify both --dump and --import '.red
64
- end
65
- if importing?
66
- Trollop.die "Sorry 'Import' functionality is not implemented yet".red
67
- end
54
+ # returns the Confoog class which can then be used to access any config var
55
+ def config
56
+ @cmd.getconfig
57
+ end
58
+
59
+ def cmd(command)
60
+ @cmd.true_cmd(command.to_sym)
68
61
  end
69
62
 
70
63
  def setup_logfile
71
- filename = if param_set('timestamp')
64
+ filename = if cmd(:timestamp)
72
65
  'updaterepo-' + Time.new.strftime('%y%m%d-%H%M%S') + '.log'
73
66
  else
74
67
  'updaterepo.log'
75
68
  end
76
- # filename = 'updaterepo' + prefix + '.log'
77
69
  @logfile = File.open(filename, 'w')
78
70
  @logfile.sync = true
79
71
  end
80
72
 
81
- # Determine options from the command line and configuration file. Command
82
- # line takes precedence
83
- def param_set(option)
84
- @config['cmd'][option.to_sym] || @config[option]
85
- end
86
-
87
- def config_error
88
- if @config.status[:errors] == Status::ERR_CANT_LOAD
89
- print_log 'Note that the the default configuration file was '.red,
90
- "changed to ~/#{CONFIG_FILE} from v0.4.0 onwards\n\n".red
91
- end
92
- exit 1
93
- end
94
-
95
- # rubocop:disable Metrics/MethodLength
96
- # rubocop:disable Metrics/LineLength
97
- def set_options
98
- Trollop.options do
99
- version "update_repo version #{VERSION} (C)2016 G. Ramsay\n"
100
- banner <<-EOS
101
-
102
- Keep multiple local Git-Cloned Repositories up to date with one command.
103
-
104
- Usage:
105
- update_repo [options]
106
-
107
- Options are not required. If none are specified then the program will read from
108
- the standard configuration file (~/#{CONFIG_FILE}) and automatically update the
109
- specified Repositories.
110
-
111
- Options:
112
- EOS
113
- opt :color, 'Use colored output', default: true
114
- opt :dump, 'Dump a list of Directories and Git URL\'s to STDOUT in CSV format', default: false
115
- opt :prune, "Number of directory levels to remove from the --dump output.\nOnly valid when --dump or -d specified", default: 0
116
- opt :import, "Import a previous dump of directories and Git repository URL's,\n(created using --dump) then proceed to clone them locally.", default: false
117
- opt :log, "Create a logfile of all program output to './update_repo.log'. Any older logs will be overwritten.", default: false
118
- opt :timestamp, 'Timestamp the logfile instead of overwriting. Does nothing unless the --log option is also specified.', default: false
119
- # opt :quiet, 'Only minimal output to the terminal', default: false
120
- # opt :silent, 'Completely silent, no output to terminal at all.', default: false
121
- end
122
- end
123
- # rubocop:enable Metrics/MethodLength
124
- # rubocop:enable Metrics/LineLength
125
-
126
73
  # take each directory contained in the Repo directory, if it is detected as
127
74
  # a Git repository then update it (or as directed by command line)
128
75
  # @param dirname [string] Contains the directory to search for Git repos.
@@ -130,7 +77,7 @@ EOS
130
77
  Dir.chdir(dirname) do
131
78
  Dir['**/'].each do |dir|
132
79
  next unless gitdir?(dir)
133
- if dumping?
80
+ if cmd(:dump)
134
81
  dump_repo(File.join(dirname, dir))
135
82
  else
136
83
  notexception?(dir) ? update_repo(dir) : skip_repo(dir)
@@ -143,7 +90,7 @@ EOS
143
90
  # @param dir [string] Directory to be checked
144
91
  # @return [boolean] True if this is NOT an exception, False otherwise
145
92
  def notexception?(dir)
146
- !@config['exceptions'].include?(File.basename(dir))
93
+ !config['exceptions'].include?(File.basename(dir))
147
94
  end
148
95
 
149
96
  # Display a simple header to the console
@@ -152,11 +99,10 @@ EOS
152
99
  # @return [void]
153
100
  def show_header
154
101
  # print an informative header before starting
155
- # unless we are dumping the repo information
156
102
  print_log "\nGit Repo update utility (v", VERSION, ')',
157
103
  " \u00A9 Grant Ramsay <seapagan@gmail.com>\n"
158
- print_log "Using Configuration from '#{@config.config_path}'\n"
159
- # print_log "Command line is : #{@config['cmd']}\n"
104
+ print_log "Using Configuration from '#{config.config_path}'\n"
105
+ print_log "Command line is : #{config['cmd']}\n"
160
106
  # list out the locations that will be searched
161
107
  list_locations
162
108
  # list any exceptions that we have from the config file
@@ -186,7 +132,7 @@ EOS
186
132
  end
187
133
 
188
134
  def list_exceptions
189
- exceptions = @config['exceptions']
135
+ exceptions = config['exceptions']
190
136
  if exceptions
191
137
  print_log "\nExclusions:".underline, ' ',
192
138
  exceptions.join(', ').yellow, "\n"
@@ -195,7 +141,7 @@ EOS
195
141
 
196
142
  def list_locations
197
143
  print_log "\nRepo location(s):\n".underline
198
- @config['location'].each do |loc|
144
+ config['location'].each do |loc|
199
145
  print_log '-> ', loc.cyan, "\n"
200
146
  end
201
147
  end
@@ -244,7 +190,7 @@ EOS
244
190
  def dump_repo(dir)
245
191
  Dir.chdir(dir.chomp!('/')) do
246
192
  repo_url = `git config remote.origin.url`.chomp
247
- print_log "#{trunc_dir(dir, @config['cmd'][:prune])},#{repo_url}\n"
193
+ print_log "#{trunc_dir(dir, config['cmd'][:prune])},#{repo_url}\n"
248
194
  end
249
195
  end
250
196
  end
@@ -0,0 +1,99 @@
1
+ require 'update_repo/version'
2
+ require 'update_repo/helpers'
3
+ require 'confoog'
4
+ require 'trollop'
5
+ require 'yaml'
6
+
7
+ # this class will encapsulate the command line fucntionality and processing
8
+ # as well as reading from the config file. It will also merge the 2 sources
9
+ # into the final definitive option hash.
10
+ module UpdateRepo
11
+ # This class takes care of reading and parsing the command line and conf file
12
+ class CmdConfig
13
+ include Helpers
14
+
15
+ def initialize
16
+ # read the options from Trollop and store in temp variable.
17
+ # we do it this way around otherwise if configuration file is missing it
18
+ # gives the error messages even on '--help' and '--version'
19
+ temp_opt = set_options
20
+ @conf = Confoog::Settings.new(filename: CONFIG_FILE,
21
+ prefix: 'update_repo',
22
+ autoload: true, autosave: false)
23
+ @conf['cmd'] = temp_opt
24
+ config_error unless @conf.status[:errors] == Status::INFO_FILE_LOADED
25
+ end
26
+
27
+ # return the configuration hash variable
28
+ def getconfig
29
+ @conf
30
+ end
31
+
32
+ # This will return the 'true' version of a command, taking into account
33
+ # both command line (given preference) and the configuration file.
34
+ # parameter is a :symbol
35
+ def true_cmd(command)
36
+ cmd_given = @conf['cmd'][(command.to_s + '_given').to_sym]
37
+ cmd_line = @conf['cmd'][command.to_sym]
38
+
39
+ if cmd_given
40
+ # if we specify something on the cmd line, that takes precedence
41
+ cmd_line
42
+ elsif !@conf[command.to_s].nil?
43
+ # if we have a value in the config file we use that.
44
+ @conf[command.to_s]
45
+ else
46
+ # this will catch any 'default' values in the cmd setup.
47
+ cmd_line
48
+ end
49
+ end
50
+
51
+ # make sure the parameter combinations are valid
52
+ def check_params
53
+ if true_cmd(:dump) && true_cmd(:import)
54
+ Trollop.die 'Sorry, you cannot specify both --dump and --import '.red
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def config_error
61
+ if @conf.status[:errors] == Status::ERR_CANT_LOAD
62
+ print_log 'Note that the the default configuration file was '.red,
63
+ "changed to ~/#{CONFIG_FILE} from v0.4.0 onwards\n\n".red
64
+ end
65
+ exit 1
66
+ end
67
+
68
+ # rubocop:disable Metrics/MethodLength
69
+ # rubocop:disable Metrics/LineLength
70
+ def set_options
71
+ Trollop.options do
72
+ version "update_repo version #{VERSION} (C)2016 G. Ramsay\n"
73
+ banner <<-EOS
74
+
75
+ Keep multiple local Git-Cloned Repositories up to date with one command.
76
+
77
+ Usage:
78
+ update_repo [options]
79
+
80
+ Options are not required. If none are specified then the program will read from
81
+ the standard configuration file (~/#{CONFIG_FILE}) and automatically update the
82
+ specified Repositories.
83
+
84
+ Options:
85
+ EOS
86
+ opt :color, 'Use colored output', default: true
87
+ opt :dump, 'Dump a list of Directories and Git URL\'s to STDOUT in CSV format', default: false
88
+ opt :prune, "Number of directory levels to remove from the --dump output.\nOnly valid when --dump or -d specified", default: 0
89
+ # opt :import, "Import a previous dump of directories and Git repository URL's,\n(created using --dump) then proceed to clone them locally.", default: false
90
+ opt :log, "Create a logfile of all program output to './update_repo.log'. Any older logs will be overwritten.", default: false
91
+ opt :timestamp, 'Timestamp the logfile instead of overwriting. Does nothing unless the --log option is also specified.', default: false
92
+ # opt :quiet, 'Only minimal output to the terminal', default: false
93
+ # opt :silent, 'Completely silent, no output to terminal at all.', default: false
94
+ end
95
+ end
96
+ # rubocop:enable Metrics/MethodLength
97
+ # rubocop:enable Metrics/LineLength
98
+ end
99
+ end
@@ -14,28 +14,13 @@ module Helpers
14
14
  File.join(path_array)
15
15
  end
16
16
 
17
- # true if we are dumping the file structure and git urls instead of updating.
18
- def dumping?
19
- param_set('dump')
20
- end
21
-
22
- # true if we are importing a previously dumped list of Git repos.
23
- def importing?
24
- param_set('import')
25
- end
26
-
27
- # true if we are logging to file.
28
- def logging?
29
- param_set('log')
30
- end
31
-
32
17
  # this function will simply pass the given string to 'print', and also
33
18
  # log to file if that is specified.
34
19
  def print_log(*string)
35
20
  # log to screen regardless
36
21
  print(*string)
37
22
  # log to file if that has been enabled
38
- @logfile.write(string.join('').gsub(/\e\[(\d+)(;\d+)*m/, '')) if logging?
23
+ @logfile.write(string.join('').gsub(/\e\[(\d+)(;\d+)*m/, '')) if cmd('log')
39
24
  end
40
25
 
41
26
  # mark these as private simply so that 'reek' wont flag as utility function.
@@ -50,9 +35,4 @@ module Helpers
50
35
  time_taken = Time.at(duration).utc
51
36
  time_taken.strftime('%-H hours, %-M Minutes and %-S seconds')
52
37
  end
53
-
54
- # we cant use --dump and --import on the same command line
55
- def no_import_export
56
- Trollop.die 'update_repo : Cannot specify both --dump and --import'
57
- end
58
38
  end
@@ -1,4 +1,4 @@
1
1
  module UpdateRepo
2
2
  # constant, current version of this Gem
3
- VERSION = '0.7.1'.freeze
3
+ VERSION = '0.7.2'.freeze
4
4
  end
data/update_repo.gemspec CHANGED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: update_repo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Ramsay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-30 00:00:00.000000000 Z
11
+ date: 2016-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -285,6 +285,7 @@ files:
285
285
  - bin/update_repo
286
286
  - exe/update_repo
287
287
  - lib/update_repo.rb
288
+ - lib/update_repo/cmd_config.rb
288
289
  - lib/update_repo/helpers.rb
289
290
  - lib/update_repo/version.rb
290
291
  - update_repo.gemspec