ssh-config 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e2019230b1e51982769d29450d10d98ba7c0b92
4
+ data.tar.gz: 71cd2369a39509b6d623fd50e3ced45dbdc30d15
5
+ SHA512:
6
+ metadata.gz: f71ad2cd574d6f86a97f7aad4ebf18833067d659c7a59613b0854ec8eecc58f0dafc6da2f611ea459487a6fed3cdc67b943f6d610df399afe20d09e058e61abf
7
+ data.tar.gz: 3902d9a8d5a979793b5fbd9d9cee7bda0c73bfc9a9b790c844ffa0c9ada72dc37df587539dea66a86fad5a7fb058a4116270926c1cd28490478d9a59a5258e3d
data/CHANGES CHANGED
@@ -1,6 +1,16 @@
1
1
  CHANGES
2
2
  ----------------------------------------------------------------------
3
3
 
4
+ 2013-07-03:
5
+ -----------
6
+
7
+ - Version 0.1.3
8
+
9
+ - add 'alias' and 'unalias' commands, and generally support for aliases.
10
+ (changes by David Lindes.)
11
+ - fix compatibility by using FileUtils#copy instead of File#copy
12
+ (changes by Jason Lewis.)
13
+
4
14
  2010-03-29:
5
15
  -----------
6
16
 
@@ -16,4 +26,3 @@ CHANGES
16
26
 
17
27
  - Search strings are highlighted
18
28
  - Fixed usage/comments to say 'search' instead of 'find'
19
-
@@ -56,14 +56,16 @@ rails-02.example.com User dbrady" would emit
56
56
  Hostname rails-02.example.com
57
57
  User dbrady
58
58
 
59
+ Note: If you just want another short name for a host, use alias instead
59
60
 
60
61
  === dump
61
62
 
62
63
  Dumps the entire file.
63
64
 
64
- === show <host>
65
+ === show <host> [<host> ...]
65
66
 
66
- Dumps one section. (Similar to ssh-what.)
67
+ Dumps one section. (Similar to ssh-what.) <host> may be the main name
68
+ for a host or an alias.
67
69
 
68
70
  === search <pattern>
69
71
 
@@ -74,3 +76,14 @@ pattern. (Similar to ssh-what.)
74
76
 
75
77
  Lists all section names (just the Host line)
76
78
 
79
+ === alias <host> <alias> [<alias> ...]
80
+
81
+ Adds one or more aliases to a host definition (to show up on the Host line).
82
+
83
+ === unalias [<host>] <alias> [<alias> ...]
84
+
85
+ With 2 or more arguments, looks up host by <host> (must be the first
86
+ name on the Host line), and removes any <alias>es from the list of
87
+ alternate names. With only 1 argument, looks for an entry by that
88
+ alias (must NOT be the first name on the Host line), and removes the
89
+ alias from the entry, if found.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'ftools'
2
+ require 'fileutils'
3
3
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'ssh-config'))
4
4
 
5
5
  # ssh-config - Manipulate config values in the .ssh/config file.
@@ -12,7 +12,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'ssh-con
12
12
  # ssh-config set <host-nickname> <setting> <value> [<setting> <value> [...]]
13
13
  # Note: "<setting> -" deletes a setting
14
14
  # ssh-config rm <host-nickname> # delete config settings for that host
15
- # ssh-config copy <host-nickname> <new-host-nickname> # copy settings from one to the other.
15
+ # ssh-config copy <host-nickname> <new-host-nickname> # copy settings from one to the other.
16
+ # ssh-config alias <host-nickname> <alternate-host-nickname> # add an alias
17
+ # ssh-config unalias [<host-nickname>] <alias-to-remove>
16
18
 
17
19
  config = ConfigFile.new
18
20
  argv = ARGV
@@ -49,10 +51,11 @@ when 'help'
49
51
  when 'list'
50
52
  puts config.list
51
53
  when 'show'
52
- puts config.show(argv.shift)
54
+ puts config.show(*argv)
53
55
  when 'search'
54
56
  search = argv.shift
55
- puts config.search(search).gsub(search, "\033[43m#{search}\033[0m")
57
+ result = config.search(search).gsub(search, "\033[43m#{search}\033[0m")
58
+ puts result unless result.empty?
56
59
  when 'cp', 'copy'
57
60
  config.copy! argv.shift, argv.shift, *argv
58
61
  when 'unset'
@@ -60,8 +63,14 @@ when 'unset'
60
63
  when 'set'
61
64
  config.set!(*argv)
62
65
  when 'rm', 'del', 'delete'
63
- config.rm! argv.shift
66
+ config.rm! argv.shift
64
67
  when 'dump'
65
68
  puts config.dump
69
+ when 'alias'
70
+ config.alias!(*argv)
71
+ when 'unalias'
72
+ config.unalias!(*argv)
73
+ else
74
+ $stderr.puts "Sorry, don't know how to handle command '#{command}'"
66
75
  end
67
76
 
@@ -6,14 +6,14 @@ class ConfigFile
6
6
  @sections_by_name = {}
7
7
  read_config
8
8
  end
9
-
9
+
10
10
  def add_section(name)
11
11
  section = ConfigSection.new(name)
12
12
  @sections << section
13
- @sections_by_name[name] = section
13
+ @sections_by_name[section.name] = section
14
14
  section
15
15
  end
16
-
16
+
17
17
  def read_config
18
18
  current_section = nil
19
19
  IO.readlines(File.expand_path("~/.ssh/config")).each_with_index do |line, i|
@@ -25,19 +25,23 @@ class ConfigFile
25
25
  current_section.lines << line
26
26
  else
27
27
  @header_lines << line
28
- end
28
+ end
29
29
  end
30
30
  end
31
31
  end
32
-
33
- def show(host_nick)
34
- @sections_by_name[host_nick]
32
+
33
+ def show(*host_nicks)
34
+ items = host_nicks.map {|nick|
35
+ sections = @sections.select {|section| section.matches_exactly?(nick)}
36
+ sections.empty? ? "# No entry found for: #{nick}" : sections
37
+ }
38
+ items.flatten.uniq * "\n"
35
39
  end
36
-
40
+
37
41
  def list()
38
- to_text(@sections_by_name.keys.sort.map {|name| "Host #{name}"})
42
+ to_text(@sections_by_name.keys.sort.map {|name| @sections_by_name[name].header})
39
43
  end
40
-
44
+
41
45
  def search(text)
42
46
  to_text(@sections.find_all {|section| section.matches?(text)}.sort_by {|section| section.name})
43
47
  end
@@ -51,12 +55,12 @@ class ConfigFile
51
55
  save
52
56
  section
53
57
  end
54
-
58
+
55
59
  def set(host_nick, key, value)
56
60
  section = @sections_by_name[host_nick] || add_section(host_nick)
57
61
  section[key] = value
58
62
  end
59
-
63
+
60
64
  def unset!(host_nick, *keys)
61
65
  backup if @make_backups
62
66
  while keys.length > 0
@@ -65,13 +69,13 @@ class ConfigFile
65
69
  save
66
70
  section
67
71
  end
68
-
72
+
69
73
  def unset(host_nick, key)
70
74
  if section = @sections_by_name[host_nick]
71
75
  section.unset(key)
72
76
  end
73
77
  end
74
-
78
+
75
79
  def dump
76
80
  to_text([@header_lines, @sections].flatten)
77
81
  end
@@ -81,49 +85,81 @@ class ConfigFile
81
85
  rm(host_nick)
82
86
  save
83
87
  end
84
-
88
+
85
89
  def rm(host_nick)
86
90
  if @sections_by_name.key?(host_nick)
87
91
  @sections_by_name.delete host_nick
88
92
  @sections.delete_at(@sections.index{|s| s.name == host_nick})
89
93
  end
90
94
  end
91
-
95
+
92
96
  def copy!(old_host_nick, new_host_nick, *args)
93
97
  backup if @make_backups
94
98
  copy(old_host_nick, new_host_nick, *args)
95
99
  save
96
100
  end
97
-
101
+
98
102
  def copy(old_host_nick, new_host_nick, *args)
99
103
  if @sections_by_name.key?(old_host_nick)
100
104
  old_section = @sections_by_name[old_host_nick]
101
105
  new_section = @sections_by_name[new_host_nick] || add_section(new_host_nick)
102
106
  new_section.lines = old_section.lines.dup
103
-
107
+
104
108
  if old_section["Hostname"]
105
109
  new_section["Hostname"] = old_section["Hostname"].gsub(old_host_nick, new_host_nick)
106
110
  end
107
-
111
+
108
112
  while args.length > 0
109
113
  key, value = args.shift, args.shift
110
114
  section = set(new_host_nick, key, value)
111
115
  end
112
116
  end
113
117
  end
114
-
118
+
119
+ def alias!(host_nick, *args)
120
+ backup if @make_backups
121
+ while args.length > 0
122
+ new_alias = args.shift
123
+ section = add_alias(host_nick, new_alias)
124
+ end
125
+ save
126
+ section
127
+ end
128
+
129
+ def add_alias(host_nick, new_alias)
130
+ section = @sections_by_name[host_nick] || add_section(host_nick)
131
+ section.aliases.push(new_alias) unless section.aliases.member?(new_alias)
132
+ end
133
+
134
+ def unalias!(*args)
135
+ if(args.size >= 2)
136
+ name = args.shift
137
+ section = @sections_by_name[name]
138
+ else
139
+ section = @sections.select {|section| section.has_alias?(args[0])}.first
140
+ end
141
+
142
+ if(section) then
143
+ section.aliases -= args
144
+ else
145
+ $stderr.puts "Failed to find section named #{name || args[0]}"
146
+ end
147
+ save
148
+ section
149
+ end
150
+
115
151
  def save
116
152
  File.open(File.expand_path("~/.ssh/config"), "w") do |file|
117
153
  file.puts dump
118
154
  end
119
155
  end
120
-
156
+
121
157
  def backup
122
- File.copy(File.expand_path("~/.ssh/config"), File.expand_path("~/.ssh/config~"))
158
+ FileUtils.copy(File.expand_path("~/.ssh/config"), File.expand_path("~/.ssh/config~"))
123
159
  end
124
-
160
+
125
161
  private
126
-
162
+
127
163
  def to_text(ray)
128
164
  ray.map {|s| s.to_s } * "\n"
129
165
  end
@@ -1,12 +1,14 @@
1
1
  class ConfigSection
2
- attr_accessor :name, :lines, :settings
2
+ attr_accessor :name, :aliases, :lines, :settings
3
3
 
4
4
  def initialize(name)
5
- @name = name
5
+ all_names = name.split(/\s+/)
6
+ @name = all_names.shift
7
+ @aliases = all_names
6
8
  @settings = {}
7
9
  @lines = []
8
10
  end
9
-
11
+
10
12
  def [](setting)
11
13
  unless @settings.key? setting
12
14
  if line = lines[setting_index(setting)]
@@ -16,18 +18,23 @@ class ConfigSection
16
18
  end
17
19
  @settings[setting]
18
20
  end
19
-
21
+
20
22
  def unset(setting)
21
23
  if line_num = setting_index(setting)
22
24
  @settings.delete setting
23
25
  @lines.delete_at line_num
24
26
  end
25
27
  end
26
-
28
+
29
+ def header
30
+ name_with_aliases = [@name, *aliases].join(" ")
31
+ "Host #{name_with_aliases}"
32
+ end
33
+
27
34
  def to_s
28
- ["Host #{name}", *lines] * "\n"
35
+ [header, *lines] * "\n"
29
36
  end
30
-
37
+
31
38
  def []=(setting, value)
32
39
  if value != '-'
33
40
  line_num = setting_index(setting) || lines.length
@@ -37,21 +44,28 @@ class ConfigSection
37
44
  @lines.delete_at(setting_index(setting))
38
45
  end
39
46
  end
40
-
47
+
41
48
  def matches?(text)
42
49
  r = Regexp.new text
43
- name =~ r || lines.any? {|line| line =~ r}
50
+ name =~ r || aliases.any? {|a| a =~ r} || lines.any? {|line| line =~ r}
44
51
  end
45
-
52
+
53
+ def matches_exactly?(text)
54
+ name == text || has_alias?(text)
55
+ end
56
+
57
+ def has_alias?(text)
58
+ aliases.member?(text)
59
+ end
60
+
46
61
  protected
47
-
62
+
48
63
  def format_line(setting, value)
49
64
  " #{setting} #{value}"
50
65
  end
51
-
66
+
52
67
  def setting_index(setting)
53
68
  r = Regexp.new(setting)
54
69
  lines.index {|line| line =~ r}
55
70
  end
56
71
  end
57
-
@@ -1,3 +1,2 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'config_file'))
2
2
  require File.expand_path(File.join(File.dirname(__FILE__), 'config_section'))
3
-
metadata CHANGED
@@ -1,34 +1,26 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ssh-config
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 2
9
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
10
5
  platform: ruby
11
- authors:
6
+ authors:
12
7
  - David Brady
13
8
  autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
-
17
- date: 2010-03-29 00:00:00 -06:00
18
- default_executable:
11
+ date: 2011-02-09 00:00:00.000000000 Z
19
12
  dependencies: []
20
-
21
- description: Ssh-Config, a tool that lets you quickly add, update, remove, and copy ssh config file entries.
13
+ description: Ssh-Config, a tool that lets you quickly add, update, remove, and copy
14
+ ssh config file entries.
22
15
  email: github@shinybit.com
23
- executables:
16
+ executables:
24
17
  - ssh-config
25
18
  extensions: []
26
-
27
- extra_rdoc_files:
19
+ extra_rdoc_files:
28
20
  - README.rdoc
29
21
  - MIT-LICENSE
30
22
  - CHANGES
31
- files:
23
+ files:
32
24
  - CHANGES
33
25
  - MIT-LICENSE
34
26
  - README.rdoc
@@ -36,40 +28,33 @@ files:
36
28
  - lib/config_file.rb
37
29
  - lib/config_section.rb
38
30
  - lib/ssh-config.rb
39
- has_rdoc: true
40
31
  homepage: http://github.com/dbrady/ssh-config/
41
32
  licenses: []
42
-
33
+ metadata: {}
43
34
  post_install_message:
44
- rdoc_options:
35
+ rdoc_options:
45
36
  - --line-numbers
46
37
  - --inline-source
47
38
  - --main
48
39
  - README.rdoc
49
40
  - --title
50
41
  - Ssh-Config - A Tool for ssh config files
51
- require_paths:
42
+ require_paths:
52
43
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
60
- required_rubygems_version: !ruby/object:Gem::Requirement
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- segments:
65
- - 0
66
- version: "0"
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
67
54
  requirements: []
68
-
69
55
  rubyforge_project:
70
- rubygems_version: 1.3.6
56
+ rubygems_version: 2.0.3
71
57
  signing_key:
72
- specification_version: 3
58
+ specification_version: 4
73
59
  summary: Ssh-Config - tool for managing your .ssh/config file
74
60
  test_files: []
75
-