package org.geoserver.shell;

import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class ShapefileCommands implements CommandMarker {

    @Autowired
    private Geoserver geoserver;

    @CliCommand(value = "shapefile publish", help = "Publish a shapefile.")
    public boolean publish(
        @CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace,
        @CliOption(key = "datastore", mandatory = true, help = "The datastore") String datastore,
        @CliOption(key = "layer", mandatory = true, help = "The layer name") String layer,
        @CliOption(key = "file", mandatory = true, help = "The zipped shapefile") File file,
        @CliOption(key = "srs", mandatory = false, unspecifiedDefaultValue = "EPSG:4326", help = "The EPSG srs code") String srs,
        @CliOption(key = "style", mandatory = false, help = "The style") String style
    ) throws Exception {
        GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
        return publisher.publishShp(workspace, datastore, layer, file, srs, style);
    }

}

@@ -392,16 +392,181 @@
 io.File;
+%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.FilenameFilter;%0Aimport java.util.zip.ZipEntry;%0Aimport java.util.zip.ZipOutputStream;
 %0A%0A@Compo
@@ -922,35 +922,36 @@
 e%22, mandatory = 
-tru
+fals
 e, help = %22The d
@@ -1017,35 +1017,36 @@
 r%22, mandatory = 
-tru
+fals
 e, help = %22The l
@@ -1417,226 +1417,1798 @@
     
-GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());%0A        return publisher.publishShp(workspace, datastore, layer, file, srs, style);%0A    %7D%0A
+String baseName = file.getName().substring(0, file.getName().lastIndexOf(%22.zip%22));%0A        if (datastore == null) %7B%0A            datastore = baseName;%0A        %7D%0A        if (layer == null) %7B%0A            layer = baseName;%0A        %7D%0A        GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());%0A        return publisher.publishShp(workspace, datastore, layer, file, srs, style);%0A    %7D%0A%0A    @CliCommand(value = %22shapefile zip%22, help = %22Zip a shapefile.%22)%0A    public boolean zip(%0A        @CliOption(key = %22shapefile%22, mandatory = true, help = %22The shapefile%22) File shapefile,%0A        @CliOption(key = %22zipfile%22, mandatory = true, help = %22The output zipfile%22) File zipfile%0A    ) throws Exception %7B%0A        int filesZipped = 0;%0A        String baseName = shapefile.getName().substring(0, shapefile.getName().lastIndexOf(%22.shp%22));%0A        String%5B%5D exts = %7B%22shp%22,%22dbf%22,%22prj%22,%22shx%22,%22fix%22,%22sbn%22,%22sbx%22%7D;%0A        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipfile));%0A        zipOut.setMethod(ZipOutputStream.DEFLATED);%0A        byte%5B%5D bytes = new byte%5B1024%5D;%0A        for(String ext: exts) %7B%0A            File file = new File(shapefile.getParentFile(), baseName + %22.%22 + ext);%0A            if (file.exists()) %7B%0A                filesZipped++;%0A                ZipEntry zipEntry = new ZipEntry(file.getName());%0A                zipOut.putNextEntry(zipEntry);%0A                FileInputStream in = new FileInputStream(file);%0A                int len;%0A                while((len = in.read(bytes)) %3E 0) %7B%0A                    zipOut.write(bytes, 0, len);%0A                %7D%0A                zipOut.closeEntry();%0A                in.close();%0A            %7D%0A        %7D%0A        zipOut.close();%0A        return filesZipped %3E 0;%0A    %7D
 %0A%7D%0A

Update CLI Options. Add sensible defaults and zipping of shapefiles.