Class: GitHooks::CLI::Config

Inherits:
Thor
  • Object
show all
Defined in:
lib/githooks/cli/config.rb

Constant Summary collapse

VALID_CONFIG_OPTIONS =
Repository::Config::OPTIONS.keys.freeze

Instance Method Summary collapse

Instance Method Details

#get(option) ⇒ Object

rubocop:disable MethodLength, AbcSize



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/githooks/cli/config.rb', line 25

def get(option) # rubocop:disable MethodLength, AbcSize
  unless VALID_CONFIG_OPTIONS.include? option
    puts "Invalid option '#{option}': expected one of #{VALID_CONFIG_OPTIONS.join(', ')}"
    return 1
  end

  GitHooks.verbose = !!options['verbose']
  GitHooks.debug   = !!options['debug']

  repository  = Repository.new(options['repo'])
  config_data = repository.config.get(option, global: options['global'])
  config_data ||= 'not set'

  puts "Repository [#{repository.path.basename}]"
  Array(config_data).flatten.each do |value|
    puts "  #{option} = #{value}"
  end
end

#listObject

rubocop:disable AbcSize



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/githooks/cli/config.rb', line 77

def list # rubocop:disable AbcSize
  GitHooks.verbose = !!options['verbose']
  GitHooks.debug   = !!options['debug']

  repository = Repository.new(options['repo'])
  githooks   = repository.config.list(global: options['global'])['githooks']
  return unless githooks

  githooks.each do |path, data|
    key_size, value_size = data.keys.collect(&:size).maximum, data.values.collect(&:size).maximum
    display_format = "    %-#{key_size}s = %-#{value_size}s\n"

    puts "Repository [#{File.basename(path)}]"
    printf display_format, 'Repo Path', path

    data.each { |key, value|
      Array(value).flatten.each do |v|
        printf display_format, key.tr('-', ' ').titleize, v
      end
    }
  end
end

#set(option, value) ⇒ Object

rubocop:disable AbcSize



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/githooks/cli/config.rb', line 51

def set(option, value) # rubocop:disable AbcSize
  GitHooks.verbose = !!options['verbose']
  GitHooks.debug   = !!options['debug']

  Repository.new(options['repo']).config.set(
    option, value,
    global:    options['global'],
    overwrite: options['overwrite-all']
  ).status.success?
rescue ArgumentError => e
  puts e.message
end

#unset(option, value = nil) ⇒ Object

rubocop:disable AbcSize



65
66
67
68
69
70
71
72
73
74
# File 'lib/githooks/cli/config.rb', line 65

def unset(option, value = nil) # rubocop:disable AbcSize
  GitHooks.verbose = !!options['verbose']
  GitHooks.debug   = !!options['debug']

  Repository.new(options['repo']).config.unset(
    option, value, global: options['global']
  )
rescue ArgumentError => e
  puts e.message
end