From 717b642fd163be8b090083869eda529ab8f02201 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Fri, 18 Oct 2019 04:36:05 +0200 Subject: [PATCH] clarity --- src/configuration.cr | 39 ++++++++--------------------- src/main.cr | 1 + src/network_commands.cr | 16 +++++++++--- src/network_configuration_parser.cr | 6 +---- 4 files changed, 25 insertions(+), 37 deletions(-) diff --git a/src/configuration.cr b/src/configuration.cr index 8106002..eb6660c 100644 --- a/src/configuration.cr +++ b/src/configuration.cr @@ -51,45 +51,36 @@ class WirelessAPSetup def to_s(io : IO) - io << to_string + io << indent(1, to_string) end def to_string String.build do |str| - str << "\t#{CBLUE}#{ssid}#{CRESET}\n" + str << "#{CBLUE}#{ssid}#{CRESET}\n" - str << "\t\t#description #{description.not_nil!}\n" unless description.nil? - str << "\t\t#{@up? "up" : "down"}\n" - str << "\t\tmtu #{mtu}\n" unless mtu.nil? + str << "\t#description #{description.not_nil!}\n" unless description.nil? + str << "\t#{@up? "up" : "down"}\n" + str << "\tmtu #{mtu}\n" unless mtu.nil? # ipv4 unless main_ip_v4.is_a?(NotSetup) - str << "\t\tinet #{main_ip_v4}\n" + str << "\tinet #{main_ip_v4}\n" aliasses_v4.each do |a| - str << "\t\talias #{a}\n" + str << "\talias #{a}\n" end end # ipv6 unless main_ip_v6.is_a?(NotSetup) - str << "\t\tinet6 #{main_ip_v6}\n" + str << "\tinet6 #{main_ip_v6}\n" @aliasses_v6.each do |a| - str << "\t\talias6 #{a}\n" + str << "\talias6 #{a}\n" end end - dns.addresses.each do |ip| - str << "\t\tdns: #{ip}\n" - end - - dns.search.each do |localdomain| - str << "\t\tdomain: #{localdomain}\n" - end - - # to improve readability - str << "\n" + str << indent(1, dns.to_s) unless dns.addresses.empty? end end @@ -190,17 +181,9 @@ class InterfaceConfiguration str << "\t#{CRED}Should main ipv6 be obtained from autoconfiguration? DHCP? Static configuration?#{CRESET}\n" end - dns.addresses.each do |ip| - str << "\tdns: #{ip}\n" - end - - dns.search.each do |localdomain| - str << "\t\tdomain: #{localdomain}\n" - end + str << indent(1, dns.to_s) unless dns.addresses.empty? unless wireless_networks.empty? - # to improve readability - str << "\n" wireless_networks.each do |k,v| str << v end diff --git a/src/main.cr b/src/main.cr index 5ba3392..7b8819d 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,6 +1,7 @@ require "option_parser" require "ipaddress" +require "./indent" require "./colors" require "./context" require "./network_commands" diff --git a/src/network_commands.cr b/src/network_commands.cr index 8af9626..d4bcf4a 100644 --- a/src/network_commands.cr +++ b/src/network_commands.cr @@ -17,20 +17,28 @@ class NetworkCommands @search = Array(String).new end - def execute - Dir.mkdir_p("#{Context.root}/etc/") + def to_s(io : IO) + addresses.each do |ip| + io << "nameserver #{ip}" + end + io << "search #{search.join(" ")}" unless search.empty? + end + + def execute if Context.simulation puts "simulation, writing in #{Context.root}/etc/resolv.conf:" @addresses.each do |address| - puts "\tnameserver #{address}\n" + puts "\tnameserver #{address}" end puts "\tsearch #{@search.join(" ")}" unless search.empty? else + Dir.mkdir_p("#{Context.root}/etc/") + File.open("#{Context.root}/etc/resolv.conf", "w+") do |file| @addresses.each do |address| - file.puts "nameserver #{address}\n" + file.puts "nameserver #{address}" end file.puts "search #{@search.join(" ")}" unless search.empty? diff --git a/src/network_configuration_parser.cr b/src/network_configuration_parser.cr index 9d38c0c..47f6b9c 100644 --- a/src/network_configuration_parser.cr +++ b/src/network_configuration_parser.cr @@ -61,6 +61,7 @@ class NetworkConfigurationParser else main_ip_v6 = IPAddress.parse ipstr end + when /^join [^ \t]+ wpakey .*/ # WPA2-PSK, other security mechanisms are not supported, yet ssid = /^join ([^ \t]+)/.match(line).try &.[1] @@ -76,14 +77,10 @@ class NetworkConfigurationParser next end - # TODO new_ap = WirelessAPSetup.new ssid, WirelessAPSetup::WPA.new(wpakeystr) wireless_networks[ssid] = new_ap - when /^network [^ \t]+ inet6 autoconf/ - puts "TODO: network SSID inet6 autoconf" - ssid = /^network ([^ \t]+)/.match(line).try &.[1] ipstr = /^network [^ \t]+ inet6? ([^ \t]+)/.match(line).try &.[1] @@ -99,7 +96,6 @@ class NetworkConfigurationParser access_point = wireless_networks[ssid].not_nil! access_point.main_ip_v6 = Autoconfiguration.new - puts "for SSID: #{ssid} ipv6 configuration = autoconf" when /^network [^ \t]+ inet6? .*/ ssid = nil