ACC SHELL

Path : /usr/share/YaST2/include/network/lan/
File Upload :
Current File : //usr/share/YaST2/include/network/lan/cmdline.ycp

/**
 * File:	lan/cmdline.ycp
 * Package:	Network configuration
 * Summary:	Network cards cmdline handlers
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: cmdline.ycp 61763 2010-04-19 13:27:22Z mzugec $
 */

{

textdomain "network";

import "CommandLine";
import "Label";
import "Lan";
import "NetworkInterfaces";
import "RichText";
import "Report";
import "LanItems";
import "Map";

list <map<string, any> > getConfigList(string config_filter){
 list <map<string, any> > confList = [];
 integer count = -1;
 LanItems::BuildLanOverview();
 //list<map<string,any> > overview = (list<map<string,any> >)LanItems::Overview();
 foreach(integer position,any row, LanItems::Items, {
  LanItems::current = position;
  count = count + 1;
  if (size(LanItems::getCurrentItem()["ifcfg"]:"")>0)
   {
	if (config_filter=="unconfigured") continue;
   } else {
	if (config_filter=="configured") continue;
     }

  confList = add(confList, $[ tostring(count) : $["id" : position,
			"rich_descr" : LanItems::getCurrentItem()["table_descr", "rich_descr"]:"",
			"descr" : LanItems::getCurrentItem()["table_descr", "table_descr", 0]:"",
			"addr" : LanItems::getCurrentItem()["table_descr", "table_descr", 1]:""]]);
 });
 return confList;
}

boolean validateId(map<string, string> options, list <map<string, any> > config){
 if (options["id"]:nil == nil){
			 Report::Error( _("Use \"id\" option to determine device."));
			 return false;
			}

   if (tointeger(options["id"]:"0")>size(config)-1){
			 Report::Error( _("Value of \"id\" is out of range. Use \"list\" option to check max. value of \"id\"."));
			 return false;
			}
 return true;
}

integer getItem(map<string, string> options, list <map<string, any> > config){
 integer ret = -1; 
 foreach(map<string, any> row, config, {
   if(options["id"]:"0" == Map::Keys(row)[0]:"") ret = tointeger(Map::Keys(row)[0]:"-1");
 });
 if(ret==-1)y2error("Device not matched!");
 return ret;
}



/**
 * Handler for action "show"
 * @param options action options
 */
define boolean ShowHandler(map<string, string> options) {
   list <map<string, any> > config = getConfigList("");
   if (validateId(options, config)==false) return false;
    foreach(map<string, any> row, config, {
     foreach(string key, map<string, any> value, (map<string, map<string, any> >) row, {
	if (key == options["id"]:"0"){
			// create plain text from formated HTML
		 string text = sformat("echo \"%1\"|sed s/'<br>'/'\\n'/g|sed s/'<\\/li>'/'\\n'/g|sed s/'<[/a-z]*>'/''/g", value["rich_descr"]:"");
			 map<string, any> descr = (map<string, any>)SCR::Execute(.target.bash_output, text);
			 CommandLine::Print( descr["stdout"]:"" );
			}
     });
    });
    return true;
}

define boolean ListHandler(map<string, string> options) {
 string config_filter="";
 if (contains(Map::Keys(options), "configured")) config_filter="configured";
 if (contains(Map::Keys(options), "unconfigured")) config_filter="unconfigured";
 list<map<string, any> > confList = getConfigList(config_filter);
 if (size(confList)>0) CommandLine::Print("id\tname, \t\t\tbootproto");
 foreach(map<string, any> row, confList, {
     foreach(string id, map <string,any> detail, (map<string, map<string, any> >)row, {
      CommandLine::Print(sformat("%1\t%2, %3",id, detail["descr"]:"", detail["addr"]:""));
     });
  });
 return true;
}
/**
 * Handler for action "add"
 * @param options action options
 */
define boolean AddHandler(map<string, string> options) {
  LanItems::AddNew();
  Lan::Add();
  LanItems::Items[LanItems::current, "ifcfg"]=options["name"]:"";
  LanItems::type=NetworkInterfaces::device_type(options["name"]:"");
  if (LanItems::type=="bond") LanItems::bond_slaves=splitstring(options["slaves"]:"", " ");
  if (LanItems::type=="vlan") LanItems::vlan_etherdevice=options["ethdevice"]:"";
  if (LanItems::type=="br") LanItems::bridge_ports=options["bridge_ports"]:"";

    LanItems::bootproto = options["bootproto"]:"none";
    if (!contains(["none", "static", "dhcp"], LanItems::bootproto)){
        Report::Error(_("Impossible value for bootproto!"));
         return false;
        }

    LanItems::ipaddr = options["ip"]:"";
    LanItems::prefix = options["prefix"]:"";
    LanItems::netmask = options["netmask"]:"255.255.255.0";
    LanItems::startmode = options["startmode"]:"auto";
    if (!contains(["auto", "ifplugd", "nfsroot"], LanItems::startmode)){
        Report::Error(_("Impossible value for startmode!"));
         return false;
        }

   LanItems::Commit();
   ListHandler($[]);

    return true;
}

/**
 * Handler for action "edit"
 * @param options action options
 */
define boolean EditHandler(map<string, string> options) {
   list <map<string, any> > config = getConfigList("");

   if (validateId(options, config)==false) return false;

   LanItems::current = getItem(options, config);
   LanItems::SetItem();

  if (size(LanItems::getCurrentItem()["ifcfg"]:"")==0){
   NetworkInterfaces::Add();
   LanItems::operation = `edit;
   LanItems::interfacename=LanItems::getCurrentItem()["hwinfo", "dev_name"]:"";
   LanItems::Items[LanItems::current, "ifcfg"] = LanItems::interfacename;
  }

    if (contains(Map::Keys(options), "ip")) options["bootproto"] = "static";

    LanItems::bootproto = options["bootproto"]:"none";
    if (!contains(["none", "static", "dhcp"], LanItems::bootproto)){
        Report::Error(_("Impossible value for bootproto!"));
         return false;
        }
    if (LanItems::bootproto=="static"){
	    if (!(size(options["ip"]:"")>0)){
		 Report::Error(_("For static configuration \"ip\" option is needed!"));
		 return false;
		}
	    LanItems::ipaddr = options["ip"]:"";
	    if (size(options["prefix"]:"")>0) LanItems::prefix = options["prefix"]:"";
		else {
			LanItems::netmask = options["netmask"]:"255.255.255.0";
			LanItems::prefix="";
		     }
	   } else{
		LanItems::ipaddr = "";
		LanItems::netmask = "";
		}

    LanItems::startmode = options["startmode"]:"auto";
    if (!contains(["auto", "ifplugd", "nfsroot"], LanItems::startmode)){
        Report::Error(_("Impossible value for startmode!"));
         return false;
        }

   LanItems::Commit();
   ShowHandler(options);
    return true;
}
/**
 * Handler for action "delete"
 * @param options action options
 */
define boolean DeleteHandler(map<string, string> options) {
   list <map<string, any> > config = getConfigList("");
   if (validateId(options, config)==false) return false;
   foreach(map<string, any> row, config, {
     foreach(string key, map<string, any> value, (map<string, map<string, any> >) row, {
        if (key == options["id"]:"0"){
			LanItems::current = tointeger(value["id"]:-1);
		            Lan::Delete();
		            LanItems::Commit();
			 CommandLine::Print(_("The device was deleted."));
                        }
     });
    });


    return true;
}

/* EOF */
}

ACC SHELL 2018