package org.openslx.taskmanager.tasks; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.SystemCommandTask; public class AptGetUpgradable extends SystemCommandTask { private static final Pattern REGEX = Pattern.compile( "^(\\S+)/(\\S+)\\s+(\\S+)\\s.*\\[upgrad.*from:\\s+(\\S+)[,\\]]" ); private final Output status = new Output(); private StringBuilder otherOutput = new StringBuilder(); @Override protected boolean initTask() { this.setStatusObject( status ); this.timeoutSeconds = 25; return true; } @Override protected String[] initCommandLine() { return new String[] { "apt", "list", "--upgradable" }; } @Override protected void initEnvironment( Map environment ) { environment.put( "LANG", "C.UTF-8" ); environment.put( "LC_ALL", "C.UTF-8" ); } @Override protected boolean processEnded( int exitCode ) { if ( this.status.packages.isEmpty() && Util.isEmpty( this.status.error ) ) { this.status.error = this.otherOutput.toString(); } return exitCode == 0; } @Override protected void processStdOut( String line ) { Matcher m = REGEX.matcher( line ); if ( m.matches() ) { status.packages.add( new Package( m.group( 1 ), m.group( 2 ), m.group( 4 ), m.group( 3 ) ) ); } else { otherOutput.append( line ); otherOutput.append( '\n' ); } } @Override protected void processStdErr( String line ) { if ( line.isEmpty() || line.contains( "stable CLI interface" ) ) return; if ( status.error == null ) { status.error = line; } else { status.error += "\n" + line; } } private static class Output { public final List packages = new ArrayList<>(); public String error; } @SuppressWarnings( "unused" ) private static class Package { public final String name; public final String source; public final String newVersion; public final String oldVersion; public Package( String name, String source, String oldVersion, String newVersion ) { this.name = name; this.source = source; this.newVersion = newVersion; this.oldVersion = oldVersion; } } }