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"


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

def AddHandler(options)
  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)

  LanItems.Commit(builder)
  ListHandler({})

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

#DeleteHandler(options) ⇒ Object

Handler for action "delete"


155
156
157
158
159
160
161
162
163
164
# File 'src/include/network/lan/cmdline.rb', line 155

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"


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'src/include/network/lan/cmdline.rb', line 132

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)

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

#initialize_network_lan_cmdline(_include_target) ⇒ Object


39
40
41
42
43
44
45
46
# File 'src/include/network/lan/cmdline.rb', line 39

def initialize_network_lan_cmdline(_include_target)
  textdomain "network"

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

#ListHandler(options) ⇒ Object


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

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"


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

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


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

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