Class: ConfigmonkeyCli::Application

Inherits:
Object
  • Object
show all
Includes:
Colorize, Configuration::AppHelper, Core, Dispatch, OutputHelper, Helper
Defined in:
lib/configmonkey_cli/application.rb,
lib/configmonkey_cli/application/core.rb,
lib/configmonkey_cli/application/colorize.rb,
lib/configmonkey_cli/application/dispatch.rb,
lib/configmonkey_cli/application/manifest.rb,
lib/configmonkey_cli/application/configuration.rb,
lib/configmonkey_cli/application/output_helper.rb,
lib/configmonkey_cli/application/manifest_actions/base.rb,
lib/configmonkey_cli/application/manifest_actions/copy.rb,
lib/configmonkey_cli/application/manifest_actions/link.rb,
lib/configmonkey_cli/application/manifest_actions/rtfm.rb,
lib/configmonkey_cli/application/manifest_actions/chmod.rb,
lib/configmonkey_cli/application/manifest_actions/mkdir.rb,
lib/configmonkey_cli/application/manifest_actions/rsync.rb,
lib/configmonkey_cli/application/manifest_actions/custom.rb,
lib/configmonkey_cli/application/manifest_actions/invoke.rb,
lib/configmonkey_cli/application/manifest_actions/remove.rb,
lib/configmonkey_cli/application/manifest_actions/inplace.rb,
lib/configmonkey_cli/application/manifest_actions/template.rb,
lib/configmonkey_cli/application/manifest_actions/sync_links.rb

Defined Under Namespace

Modules: Colorize, Core, Dispatch, ManifestAction, OutputHelper Classes: Configuration, Manifest

Constant Summary

Constants included from Colorize

Colorize::COLORMAP

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configuration::AppHelper

#cm_cfg_configfile, #cm_cfg_path, #generate_manifest, #load_and_execute_manifest, #load_appconfig

Methods included from Dispatch

#dispatch, #dispatch_generate_manifest, #dispatch_help, #dispatch_index, #dispatch_info

Methods included from Core

#fire, #haltpoint, #hook, #interruptable, #logger, #logger_filename, #release_signals, #trap_signals

Methods included from Colorize

#colorize

Methods included from OutputHelper

#abort, #debug, #error, #print, #puts, #warn

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

#initialize(env, argv) {|_self| ... } ⇒ Application

Returns a new instance of Application.

Yields:

  • (_self)

Yield Parameters:



32
33
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
# File 'lib/configmonkey_cli/application.rb', line 32

def initialize env, argv
  @boot = Time.current
  @env, @argv = env, argv
  @hooks = {}
  @connections = {}
  @monitor = Monitor.new
  @opts = {
    working_directory: Dir.pwd, # -i flag
    target_directory: "/",      # -o flag
    hostname: `hostname`.chomp, # -f flag
    diff_tool: nil,             # -D flag
    merge_tool: nil,            # -M flag
    bell: false,                 # -b flag
    default_accept: false,      # -a flag
    default_yes: false,         # -y flag
    dispatch: :index,           # (internal) action to dispatch
    simulation: false,          # -n flag
    check_for_updates: true,    # -z flag
    colorize: true,             # -m flag
    debug: false,               # -d flag
    # silent: false,              # -s flag
    # quiet: false,               # -q flag
    stdout: STDOUT,             # (internal) STDOUT redirect
  }
  init_params
  yield(self)
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



3
4
5
# File 'lib/configmonkey_cli/application.rb', line 3

def argv
  @argv
end

#connectionsObject (readonly)

Returns the value of attribute connections.



3
4
5
# File 'lib/configmonkey_cli/application.rb', line 3

def connections
  @connections
end

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/configmonkey_cli/application.rb', line 3

def env
  @env
end

#hooksObject (readonly)

Returns the value of attribute hooks.



3
4
5
# File 'lib/configmonkey_cli/application.rb', line 3

def hooks
  @hooks
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/configmonkey_cli/application.rb', line 3

def opts
  @opts
end

Class Method Details

.dispatch(*a) ⇒ Object

Setup =



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/configmonkey_cli/application.rb', line 14

def self.dispatch *a
  new(*a) do |app|
    app.load_appconfig
    app.parse_params
    begin
      app.dispatch
      app.haltpoint
    rescue Interrupt
      app.abort("Interrupted", 1)
    rescue SystemExit
      app.abort("Aborted", 2)
    ensure
      app.fire(:cm_shutdown)
      app.debug "#{Thread.list.length} threads remain..."
    end
  end
end

Instance Method Details

#find_diff_toolObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/configmonkey_cli/application.rb', line 60

def find_diff_tool
  [
    opts[:diff_tool],
    ENV["CM_DIFF"],
    ENV["THOR_DIFF"],
    ENV["RAILS_DIFF"],
    "colordiff",
    "git diff --no-index",
    "vim -d",
    "diff -u",
  ].compact.each do |cmd|
    if hit = `which #{cmd.split(" ").first}`.chomp.presence
      debug "§diff-using:(#{hit})#{cmd}"
      return cmd
    else
      debug "§diff-not-found:#{cmd}", 105
    end
  end
end

#find_merge_toolObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/configmonkey_cli/application.rb', line 80

def find_merge_tool
  [
    opts[:merge_tool],
    ENV["CM_MERGE"],
    ENV["THOR_MERGE"],
    (`git config merge.tool`.chomp rescue nil),
    "vim -d",
    "diff -u",
  ].compact.each do |cmd|
    if hit = `which #{cmd.split(" ").first}`.chomp.presence
      debug "§merge-using:(#{hit})#{cmd}"
      return cmd
    else
      debug "§merge-not-found:#{cmd}", 105
    end
  end
end

#init_paramsObject



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
# File 'lib/configmonkey_cli/application.rb', line 102

def init_params
  @optparse = OptionParser.new do |opts|
    opts.banner = "Usage: configmonkey [options]"

    opts.separator(c "# Application options", :blue)
    opts.on("--generate-manifest", "Generates an example manifest in current directory") { @opts[:dispatch] = :generate_manifest }
    opts.on("-a", "--accept", "accept all defaults") { @opts[:default_accept] = true }
    opts.on("-b", "--bell", "dont ring a bell when asked") { @opts[:bell] = true }
    opts.on("-D", "--diff", "change default diff tool") {|s| @opts[:diff_tool] = s }
    opts.on("-f", "--fake-host HOST", "override hostname") {|s| @opts[:hostname] = s }
    opts.on("-i", "--in DIR", "operate from this source directory instead of pwd") {|s| @opts[:working_directory] = s }
    opts.on("-o", "--out DIR", "operate on this target directory instead of /") {|s| @opts[:target_directory] = s }
    opts.on("-l", "--log [file]", "Log changes to file, defaults to ~/.configmonkey/logs/configmonkey.log") {|s| @opts[:logfile] = s || logger_filename }
    opts.on("-M", "--merge", "change default merge tool") {|s| @opts[:merge_tool] = s }
    opts.on("-n", "--dry-run", "Simulate changes only, does not perform destructive operations") { @opts[:simulation] = true }
    opts.on("-y", "--yes", "accept all prompts with yes") { @opts[:default_yes] = true }
    opts.on(      "--dev-dump-actions", "Dump actions and exit") { @opts[:dev_dump_actions] = true }
    # opts.on("-q", "--quiet", "Only print errors") { @opts[:quiet] = true }

    opts.separator("\n" << c("# General options", :blue))
    opts.on("-d", "--debug [lvl=1]", Integer, "Enable debug output") {|l| @opts[:debug] = l || 1 }
    opts.on("-m", "--monochrome", "Don't colorize output") { @opts[:colorize] = false }
    opts.on("-h", "--help", "Shows this help") { @opts[:dispatch] = :help }
    opts.on("-v", "--version", "Shows version and other info") { @opts[:dispatch] = :info }
    opts.on("-z", "Do not check for updates on GitHub (with -v/--version)") { @opts[:check_for_updates] = false }
  end
end

#parse_paramsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/configmonkey_cli/application.rb', line 130

def parse_params
  @optparse.parse!(@argv)

  # resolve diff/merge tool
  @opts[:diff_tool] = find_diff_tool
  @opts[:merge_tool] = find_merge_tool

  # thor bell
  ENV['THOR_ASK_BELL'] = "true" if @opts[:bell]

  # thor no-colors
  ENV['NO_COLOR'] = "true" if !@opts[:colorize]

  # thor diff-tool
  ENV['THOR_DIFF'] = opts[:diff_tool] if @opts[:diff_tool]
rescue OptionParser::ParseError => e
  abort(e.message)
  dispatch(:help)
  exit 1
end

#running?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/configmonkey_cli/application.rb', line 155

def running?
  @running
end

#sync(&block) ⇒ Object



151
152
153
# File 'lib/configmonkey_cli/application.rb', line 151

def sync &block
  @monitor.synchronize(&block)
end

#to_sObject



98
99
100
# File 'lib/configmonkey_cli/application.rb', line 98

def to_s
  "#<ConfigmonkeyCli::Application @boot=#{@boot} @opts=#{@opts} @running=#{@running}>"
end