From b852f09ad5d7acd6dbf5dad08fa7401634c70035 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Fri, 11 Mar 2016 14:58:56 +0000 Subject: Fix bulkcreatedevices to handle DOS line endings and blank lines --- provisioning-model.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/provisioning-model.lua b/provisioning-model.lua index 57880b5..943267e 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -2387,7 +2387,9 @@ mymodule.bulk_create_devices = function(self, devicelist) -- To allow uploading a file, check the bulkdevicedata format -- Haserl will pass a temporary file name if a file is uploaded if #devicelist.value.bulkdevicedata.value == 1 and string.find(devicelist.value.bulkdevicedata.value[1], "^/tmp/[^.]+$") and fs.is_file(devicelist.value.bulkdevicedata.value[1]) then - devicelist.value.bulkdevicedata.value = fs.read_file_as_array(devicelist.value.bulkdevicedata.value[1]) or {} + -- Need to account for possible DOS line endings + --devicelist.value.bulkdevicedata.value = fs.read_file_as_array(devicelist.value.bulkdevicedata.value[1]) or {} + devicelist.value.bulkdevicedata.value = format.string_to_table(fs.read_file(devicelist.value.bulkdevicedata.value[1]), "\r?\n") end local res, err = pcall(function() connected = databaseconnect() @@ -2439,7 +2441,11 @@ mymodule.bulk_create_devices = function(self, devicelist) reverseheaders[h] = i end - for i=2,#devicelist.value.bulkdevicedata.value do + -- The repeat at the end of the line gives us a 'continue'-like feature by calling break + -- see http://stackoverflow.com/a/13825260 + for i=2,#devicelist.value.bulkdevicedata.value do repeat + -- continue if blank line + if "" == devicelist.value.bulkdevicedata.value[i] then break end local values = csv_parseline(devicelist.value.bulkdevicedata.value[i], ",") -- Create the device @@ -2525,7 +2531,7 @@ mymodule.bulk_create_devices = function(self, devicelist) if params.errtxt then error("Failed to create device in line "..i.."\n"..params:print_errtxt()) end - end + until true end if connected then databasedisconnect() end end) if not res and err then -- cgit v1.2.3