Class: ModuleSync::CLI

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/modulesync/cli.rb

Constant Summary

Constants included from Constants

ModuleSync::Constants::CONF_FILE, ModuleSync::Constants::GLOBAL_DEFAULTS_KEY, ModuleSync::Constants::HOOK_FILE, ModuleSync::Constants::MODULESYNC_CONF_FILE, ModuleSync::Constants::MODULE_CONF_FILE, ModuleSync::Constants::MODULE_FILES_DIR

Instance Method Summary collapse

Instance Method Details

#commands_availableObject



21
22
23
24
25
26
# File 'lib/modulesync/cli.rb', line 21

def commands_available
  [
    'update',
    'hook',
  ]
end

#defaultsObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/modulesync/cli.rb', line 9

def defaults
  {
    :namespace            => 'puppetlabs',
    :branch               => 'master',
    :git_base             => '[email protected]:',
    :managed_modules_conf => 'managed_modules.yml',
    :configs              => '.',
    :tag_pattern          => '%s',
    :project_root         => './modules',
  }
end

#fail(message) ⇒ Object



28
29
30
31
32
# File 'lib/modulesync/cli.rb', line 28

def fail(message)
  puts @options[:help]
  puts message
  exit
end

#optionsObject



120
121
122
# File 'lib/modulesync/cli.rb', line 120

def options
  @options
end

#parse_opts(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/modulesync/cli.rb', line 34

def parse_opts(args)
  @options = defaults
  @options.merge!(Hash.transform_keys_to_symbols(Util.parse_config(MODULESYNC_CONF_FILE)))
  @options[:command] = args[0] if commands_available.include?(args[0])
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: msync update [-m <commit message>] [-c <directory> ] [--offline] [--noop] [--bump] [--changelog] [--tag] [--tag-pattern <tag_pattern>] [-p <project_root> [-n <namespace>] [-b <branch>] [-r <branch>] [-f <filter>] | hook activate|deactivate [-c <directory> ] [-n <namespace>] [-b <branch>]"
    opts.on('-m', '--message <msg>',
            'Commit message to apply to updated modules') do |msg|
      @options[:message] = msg
    end
    opts.on('-n', '--namespace <url>',
            'Remote github namespace (user or organization) to clone from and push to. Defaults to puppetlabs') do |namespace|
      @options[:namespace] = namespace
    end
    opts.on('-c', '--configs <directory>',
            'The local directory or remote repository to define the list of managed modules, the file templates, and the default values for template variables.') do |configs|
      @options[:configs] = configs
    end
    opts.on('-b', '--branch <branch>',
            'Branch name to make the changes in. Defaults to "master"') do |branch|
      @options[:branch] = branch
    end
    opts.on('-p', '--project-root <path>',
            'Path used by git to clone modules into. Defaults to "modules"') do |project_root|
      @options[:project_root] = project_root
    end
    opts.on('-r', '--remote-branch <branch>',
            'Remote branch name to push the changes to. Defaults to the branch name') do |branch|
      @options[:remote_branch] = branch
    end
    opts.on('-f', '--filter <filter>',
            'A regular expression to filter repositories to update.') do |filter|
      @options[:filter] = filter
    end
    opts.on('--amend',
            'Amend previous commit') do |msg|
      @options[:amend] = true
    end
    opts.on('--force',
            'Force push amended commit') do |msg|
      @options[:force] = true
    end
    opts.on('--noop',
            'No-op mode') do |msg|
      @options[:noop] = true
    end
    opts.on('--offline',
            'Do not run git command. Helpful if you have existing repositories locally.') do |msg|
      @options[:offline] = true
    end
    opts.on('--bump',
            'Bump module version to the next minor') do |msg|
      @options[:bump] = true
    end
    opts.on('--changelog',
            'Update CHANGELOG.md if version was bumped') do |msg|
      @options[:changelog] = true
    end
    opts.on('--tag',
            'Git tag with the current module version') do |msg|
      @options[:tag] = true
    end
    opts.on('--tag-pattern',
            'The pattern to use when tagging releases.') do |pattern|
      @options[:tag_pattern] = pattern
    end
    @options[:help] = opts.help
  end.parse!

  @options.fetch(:message) do
    if @options[:command] == 'update' && ! @options[:noop] && ! @options[:amend] && ! @options[:offline]
      fail("A commit message is required unless using noop or offline.")
    end
  end

  @options.fetch(:command) do
    fail("A command is required.")
  end

  if @options[:command] == 'hook' &&
       (! args.include?('activate') && ! args.include?('deactivate'))
    fail("You must activate or deactivate the hook.")
  end

end