Module: Yast::NetworkLanCmdlineInclude

Includes:
Logger
Defined in:
src/include/network/lan/cmdline.rb

Defined Under Namespace

Classes: InvalidOption

Instance Method Summary collapse

Instance Method Details

#AddHandler(options) ⇒ Object

Handler for action "add"

Parameters:

  • options (Hash{String => String})

    action options



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'src/include/network/lan/cmdline.rb', line 109

def AddHandler(options)
  # slaves option is marked as obsolete, bond_ports should be used instead.
  # If both options are present, new one (bond_ports) wins.
  if !options.key?("bond_ports") && options.key?("slaves")
    options["bond_ports"] = options.delete("slaves")
  end

  type = options.fetch("type", infered_type(options))
  if type.empty?
    Report.Error(_("The device type is mandatory."))
    return false
  end

  builder = Y2Network::InterfaceConfigBuilder.for(
    Y2Network::InterfaceType.from_short_name(type)
  )
  builder.name = options.fetch("name")
  update_builder_from_options!(builder, options)

  builder.save
  ListHandler({})

  true
rescue InvalidOption => e
  Report.Error(e.message)
  false
end

#DeleteHandler(options) ⇒ Object

Handler for action "delete"

Parameters:

  • options (Hash{String => String})

    action options



162
163
164
165
166
167
168
169
170
171
# File 'src/include/network/lan/cmdline.rb', line 162

def DeleteHandler(options)
  config = Yast::Lan.yast_config
  return false unless validateId(options, config.interfaces)

  interface = config.interfaces.to_a[options["id"].to_i]

  config.delete_interface(interface.name)

  true
end

#EditHandler(options) ⇒ Object

Handler for action "edit"

Parameters:

  • options (Hash{String => String})

    action options



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'src/include/network/lan/cmdline.rb', line 139

def EditHandler(options)
  log.info "calling edit handler with #{options}"
  config = Lan.yast_config.copy

  return false unless validateId(options, config.interfaces)

  interface = config.interfaces.to_a[options["id"].to_i]

  connection_config = config.connections.by_name(interface.name)
  builder = Y2Network::InterfaceConfigBuilder.for(interface.type, config: connection_config)
  builder.name = interface.name
  update_builder_from_options!(builder, options)

  builder.save
  ShowHandler(options)
  true
rescue InvalidOption => e
  Report.Error(e.message)
  false
end

#initialize_network_lan_cmdline(_include_target) ⇒ Object



37
38
39
40
41
42
43
# File 'src/include/network/lan/cmdline.rb', line 37

def initialize_network_lan_cmdline(_include_target)
  textdomain "network"

  Yast.import "CommandLine"
  Yast.import "Lan"
  Yast.import "Report"
end

#ListHandler(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'src/include/network/lan/cmdline.rb', line 85

def ListHandler(options)
  config = Yast::Lan.yast_config
  CommandLine.Print("id\tname\tbootproto")
  config.interfaces.to_a.each_with_index do |interface, index|
    connection = config.connections.by_name(interface.name)
    next if connection && options.include?("unconfigured")
    next if !connection && options.include?("configured")

    status = if !connection
      "Not configured"
    elsif connection.bootproto == Y2Network::BootProtocol::STATIC
      connection.ip.address.to_s
    else
      connection.bootproto.name
    end
    CommandLine.Print(
      "#{index}\t#{interface.name}\t#{status}"
    )
  end
  true
end

#ShowHandler(options) ⇒ Object

Handler for action "show"

Parameters:

  • options (Hash{String => String})

    action options



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'src/include/network/lan/cmdline.rb', line 69

def ShowHandler(options)
  config = Yast::Lan.yast_config
  return false unless validateId(options, config.interfaces)

  presenter = Y2Network::Presenters::InterfaceSummary.new(
    config.interfaces.to_a[options["id"].to_i].name, config
  )
  text = presenter.text
  # create plain text from formated HTML
  text.gsub!(/(<br>)|(<\/li>)/, "\n")
  text.gsub!(/<[^>]+>/, "")
  CommandLine.Print(text)

  true
end

#validateId(options, config) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'src/include/network/lan/cmdline.rb', line 45

def validateId(options, config)
  if !options["id"]
    Report.Error(_("Use \"id\" option to determine device."))
    return false
  end

  begin
    id = Integer(options["id"])
  rescue ArgumentError
    Report.Error(_("Invalid value '%s' for \"id\" option.") % options["id"])
    return false
  end

  if id >= config.size
    Report.Error(
      _("Value of \"id\" is out of range. Use \"list\" option to check max. value of \"id\".")
    )
    return false
  end
  true
end