text:
package org.apache.pdfbox.pdmodel.encryption;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;

/**
 * A security handler as described in the PDF specifications.
 * A security handler is responsible of documents protection.
 *
 * @author Ben Litchfield
 * @author Benoit Guillon
 * @author Manuel Kasper
 *
 * @param <T_POLICY> the protection policy.
 */
public abstract class SecurityHandler<T_POLICY extends ProtectionPolicy>
{
    private static final Log LOG = LogFactory.getLog(SecurityHandler.class);

    private static final int DEFAULT_KEY_LENGTH = 40;

    // see 7.6.2, page 58, PDF 32000-1:2008
    private static final byte[] AES_SALT = { (byte) 0x73, (byte) 0x41, (byte) 0x6c, (byte) 0x54 };

    /** The length in bits of the secret key used to encrypt the document. */
    private int keyLength = DEFAULT_KEY_LENGTH;

    /** The encryption key that will used to encrypt / decrypt.*/
    private byte[] encryptionKey;

    /** The RC4 implementation used for cryptographic functions. */
    private final RC4Cipher rc4 = new RC4Cipher();

    /** indicates if the Metadata have to be decrypted of not. */
    private boolean decryptMetadata;

    /** Can be used to allow stateless AES encryption */
    private SecureRandom customSecureRandom;

    // PDFBOX-4453, PDFBOX-4477: Originally this was just a Set. This failed in rare cases
    // when a decrypted string was identical to an encrypted string.
    // Because COSString.equals() checks the contents, decryption was then skipped.
    // This solution keeps all different "equal" objects.
    // IdentityHashMap solves this problem and is also faster than a HashMap
    private final Set<COSBase> objects = Collections.newSetFromMap(new IdentityHashMap<>());

    private boolean useAES;

    /**
     * The typed {@link ProtectionPolicy} to be used for encryption.
     */
    private T_POLICY protectionPolicy = null;

    /**
     * The access permission granted to the current user for the document. These
     * permissions are computed during decryption and are in read only mode.
     */
    private AccessPermission currentAccessPermission = null;

    /**
     * The stream filter name.
     */
    private COSName streamFilterName;

    /**
     * The string filter name.
     */
    private COSName stringFilterName;

    /**
     * Constructor.
     */
    protected SecurityHandler()
    {
    }

    /**
     * Constructor used for encryption.
     *
     * @param protectionPolicy The protection policy.
     */
    protected SecurityHandler(T_POLICY protectionPolicy)
    {
        this.protectionPolicy = protectionPolicy;
        keyLength = protectionPolicy.getEncryptionKeyLength();
    }

    /**
     * Set whether to decrypt meta data.
     *
     * @param decryptMetadata true if meta data has to be decrypted.
     */
    protected void setDecryptMetadata(boolean decryptMetadata)
    {
        this.decryptMetadata = decryptMetadata;
    }

    /**
     * Set the string filter name.
     *
     * @param stringFilterName the string filter name.
     */
    protected void setStringFilterName(COSName stringFilterName)
    {
        this.stringFilterName = stringFilterName;
    }

    /**
     * Set the stream filter name.
     *
     * @param streamFilterName the stream filter name.
     */
    protected void setStreamFilterName(COSName streamFilterName)
    {
        this.streamFilterName = streamFilterName;
    }

    /**
     * Set the custom SecureRandom.
     *
     * @param customSecureRandom the custom SecureRandom for AES encryption
     */
    public void setCustomSecureRandom(SecureRandom customSecureRandom)
    {
        this.customSecureRandom = customSecureRandom;
    }

    /**
     * Prepare the document for encryption.
     *
     * @param doc The document that will be encrypted.
     *
     * @throws IOException If there is an error with the document.
     */
    public abstract void prepareDocumentForEncryption(PDDocument doc) throws IOException;

    /**
     * Prepares everything to decrypt the document.
     *
     * @param encryption  encryption dictionary, can be retrieved via {@link PDDocument#getEncryption()}
     * @param documentIDArray  document id which is returned via {@link org.apache.pdfbox.cos.COSDocument#getDocumentID()}
     * @param decryptionMaterial Information used to decrypt the document.
     *
     * @throws InvalidPasswordException If the password is incorrect.
     * @throws IOException If there is an error accessing data.
     */
    public abstract void prepareForDecryption(PDEncryption encryption, COSArray documentIDArray,
            DecryptionMaterial decryptionMaterial) throws IOException;

    /**
     * Encrypt or decrypt a set of data.
     *
     * @param objectNumber The data object number.
     * @param genNumber The data generation number.
     * @param data The data to encrypt.
     * @param output The output to write the encrypted data to.
     * @param decrypt true to decrypt the data, false to encrypt it.
     *
     * @throws IOException If there is an error reading the data.
     */
    private void encryptData(long objectNumber, long genNumber, InputStream data,
                            OutputStream output, boolean decrypt) throws IOException
    {
        // Determine whether we're using Algorithm 1 (for RC4 and AES-128), or 1.A (for AES-256)
        if (useAES && encryptionKey.length == 32)
        {
            encryptDataAES256(data, output, decrypt);
        }
        else
        {
            byte[] finalKey = calcFinalKey(objectNumber, genNumber);

            if (useAES)
            {
                encryptDataAESother(finalKey, data, output, decrypt);
            }
            else
            {
                encryptDataRC4(finalKey, data, output);
            }
        }
        output.flush();
    }

    /**
     * Calculate the key to be used for RC4 and AES-128.
     *
     * @param objectNumber The data object number.
     * @param genNumber The data generation number.
     * @return the calculated key.
     */
    private byte[] calcFinalKey(long objectNumber, long genNumber)
    {
        byte[] newKey = new byte[encryptionKey.length + 5];
        System.arraycopy(encryptionKey, 0, newKey, 0, encryptionKey.length);
        // PDF 1.4 reference pg 73
        // step 1
        // we have the reference
        // step 2
        newKey[newKey.length - 5] = (byte) (objectNumber & 0xff);
        newKey[newKey.length - 4] = (byte) (objectNumber >> 8 & 0xff);
        newKey[newKey.length - 3] = (byte) (objectNumber >> 16 & 0xff);
        newKey[newKey.length - 2] = (byte) (genNumber & 0xff);
        newKey[newKey.length - 1] = (byte) (genNumber >> 8 & 0xff);
        // step 3
        MessageDigest md = MessageDigests.getMD5();
        md.update(newKey);
        if (useAES)
        {
            md.update(AES_SALT);
        }
        byte[] digestedKey = md.digest();
        // step 4
        int length = Math.min(newKey.length, 16);
        byte[] finalKey = new byte[length];
        System.arraycopy(digestedKey, 0, finalKey, 0, length);
        return finalKey;
    }

    /**
     * Encrypt or decrypt data with RC4.
     *
     * @param finalKey The final key obtained with via {@link #calcFinalKey(long, long)}.
     * @param input The data to encrypt.
     * @param output The output to write the encrypted data to.
     *
     * @throws IOException If there is an error reading the data.
     */
    protected void encryptDataRC4(byte[] finalKey, InputStream input, OutputStream output)
            throws IOException
    {
        rc4.setKey(finalKey);
        rc4.write(input, output);
    }

    /**
     * Encrypt or decrypt data with RC4.
     *
     * @param finalKey The final key obtained with via {@link #calcFinalKey(long, long)}.
     * @param input The data to encrypt.
     * @param output The output to write the encrypted data to.
     *
     * @throws IOException If there is an error reading the data.
     */
    protected void encryptDataRC4(byte[] finalKey, byte[] input, OutputStream output) throws IOException
    {
        rc4.setKey(finalKey);
        rc4.write(input, output);
    }

    /**
     * Encrypt or decrypt data with AES with key length other than 256 bits.
     *
     * @param finalKey The final key obtained with via {@link #calcFinalKey(long, long)}.
     * @param data The data to encrypt.
     * @param output The output to write the encrypted data to.
     * @param decrypt true to decrypt the data, false to encrypt it.
     *
     * @throws IOException If there is an error reading the data.
     */
    private void encryptDataAESother(byte[] finalKey, InputStream data, OutputStream output, boolean decrypt)
            throws IOException
    {
        byte[] iv = new byte[16];

        if (!prepareAESInitializationVector(decrypt, iv, data, output))
        {
            return;
        }

        try
        {
            Cipher decryptCipher = createCipher(finalKey, iv, decrypt);
            byte[] buffer = new byte[256];
            int n;
            while ((n = data.read(buffer)) != -1)
            {
                byte[] dst = decryptCipher.update(buffer, 0, n);
                if (dst != null)
                {
                    output.write(dst);
                }
            }
            output.write(decryptCipher.doFinal());
        }
        catch (GeneralSecurityException e)
        {
            throw new IOException(e);
        }
    }

    /**
     * Encrypt or decrypt data with AES256.
     *
     * @param data The data to encrypt.
     * @param output The output to write the encrypted data to.
     * @param decrypt true to decrypt the data, false to encrypt it.
     *
     * @throws IOException If there is an error reading the data.
     */
    private void encryptDataAES256(InputStream data, OutputStream output, boolean decrypt) throws IOException
    {
        byte[] iv = new byte[16];

        if (!prepareAESInitializationVector(decrypt, iv, data, output))
        {
            return;
        }

        Cipher cipher;
        try
        {
            cipher = createCipher(this.encryptionKey, iv, decrypt);
        }
        catch (GeneralSecurityException e)
        {
            throw new IOException(e);
        }

        try (CipherInputStream cis = new CipherInputStream(data, cipher))
        {
            IOUtils.copy(cis, output);
        }
        catch (IOException exception)
        {
            // starting with java 8 the JVM wraps an IOException around a GeneralSecurityException
            // it should be safe to swallow a GeneralSecurityException
            if (!(exception.getCause() instanceof GeneralSecurityException))
            {
                throw exception;
            }
            LOG.debug("A GeneralSecurityException occurred when decrypting some stream data", exception);
        }
    }

    private Cipher createCipher(byte[] key, byte[] iv, boolean decrypt) throws GeneralSecurityException
    {
        @SuppressWarnings({"squid:S5542"}) // PKCS#5 padding is requested by PDF specification
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        Key keySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec ips = new IvParameterSpec(iv);
        cipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, keySpec, ips);
        return cipher;
    }

    private boolean prepareAESInitializationVector(boolean decrypt, byte[] iv, InputStream data, OutputStream output) throws IOException
    {
        if (decrypt)
        {
            // read IV from stream
            int ivSize = (int) IOUtils.populateBuffer(data, iv);
            if (ivSize == 0)
            {
                return false;
            }
            if (ivSize != iv.length)
            {
                throw new IOException(
                        "AES initialization vector not fully read: only "
                                + ivSize + " bytes read instead of " + iv.length);
            }
        }
        else
        {
            // generate random IV and write to stream
            SecureRandom rnd = getSecureRandom();
            rnd.nextBytes(iv);
            output.write(iv);
        }
        return true;
    }

    /**
     * Returns a SecureRandom If customSecureRandom is not defined, instantiate a new SecureRandom
     *
     * @return SecureRandom
     */
    private SecureRandom getSecureRandom()
    {
        if (customSecureRandom != null)
        {
            return customSecureRandom;
        }
        return new SecureRandom();
    }

    /**
     * This will dispatch to the correct method.
     *
     * @param obj    The object to decrypt.
     * @param objNum The object number.
     * @param genNum The object generation Number.
     *
     * @throws IOException If there is an error getting the stream data.
     */
    public void decrypt(COSBase obj, long objNum, long genNum) throws IOException
    {
        if (!(obj instanceof COSString || obj instanceof COSDictionary || obj instanceof COSArray))
        {
            return;
        }
        // PDFBOX-4477: only cache strings and streams, this improves speed and memory footprint
        if (obj instanceof COSString)
        {
            if (objects.contains(obj))
            {
                return;
            }
            objects.add(obj);
            decryptString((COSString) obj, objNum, genNum);
        }
        else if (obj instanceof COSStream)
        {
            if (objects.contains(obj))
            {
                return;
            }
            objects.add(obj);
            decryptStream((COSStream) obj, objNum, genNum);
        }
        else if (obj instanceof COSDictionary)
        {
            decryptDictionary((COSDictionary) obj, objNum, genNum);
        }
        else if (obj instanceof COSArray)
        {
            decryptArray((COSArray) obj, objNum, genNum);
        }
    }

    /**
     * This will decrypt a stream.
     *
     * @param stream The stream to decrypt.
     * @param objNum The object number.
     * @param genNum The object generation number.
     *
     * @throws IOException If there is an error getting the stream data.
     */
    public void decryptStream(COSStream stream, long objNum, long genNum) throws IOException
    {
        // Stream encrypted with identity filter
        if (COSName.IDENTITY.equals(streamFilterName))
        {
            return;
        }

        COSBase type = stream.getCOSName(COSName.TYPE);
        if (!decryptMetadata && COSName.METADATA.equals(type))
        {
            return;
        }
        // "The cross-reference stream shall not be encrypted"
        if (COSName.XREF.equals(type))
        {
            return;
        }
        if (COSName.METADATA.equals(type))
        {
            byte[] buf;
            // PDFBOX-3229 check case where metadata is not encrypted despite /EncryptMetadata missing
            try (InputStream is = stream.createRawInputStream())
            {
                buf = new byte[10];
                long isResult = IOUtils.populateBuffer(is, buf);

                if (Long.compare(isResult, buf.length) != 0)
                {
                    LOG.debug("Tried reading " + buf.length + " bytes but only " + isResult + " bytes read");
                }
            }
            if (Arrays.equals(buf, "<?xpacket ".getBytes(StandardCharsets.ISO_8859_1)))
            {
                LOG.warn("Metadata is not encrypted, but was expected to be");
                LOG.warn("Read PDF specification about EncryptMetadata (default value: true)");
                return;
            }
        }
        decryptDictionary(stream, objNum, genNum);
        byte[] encrypted = IOUtils.toByteArray(stream.createRawInputStream());
        ByteArrayInputStream encryptedStream = new ByteArrayInputStream(encrypted);
        try (OutputStream output = stream.createRawOutputStream())
        {
            encryptData(objNum, genNum, encryptedStream, output, true /* decrypt */);
        }
        catch (IOException ex)
        {
            LOG.error(ex.getClass().getSimpleName() + " thrown when decrypting object " +
                    objNum + " " + genNum + " obj");
            throw ex;
        }
    }

    /**
     * This will encrypt a stream, but not the dictionary as the dictionary is
     * encrypted by visitFromString() in COSWriter and we don't want to encrypt
     * it twice.
     *
     * @param stream The stream to decrypt.
     * @param objNum The object number.
     * @param genNum The object generation number.
     *
     * @throws IOException If there is an error getting the stream data.
     */
    public void encryptStream(COSStream stream, long objNum, int genNum) throws IOException
    {
        // empty streams don't need to be encrypted
        if (!stream.hasData())
        {
            return;
        }
        byte[] rawData = IOUtils.toByteArray(stream.createRawInputStream());
        ByteArrayInputStream encryptedStream = new ByteArrayInputStream(rawData);
        try (OutputStream output = stream.createRawOutputStream())
        {
            encryptData(objNum, genNum, encryptedStream, output, false /* encrypt */);
        }
    }

    /**
     * This will decrypt a dictionary.
     *
     * @param dictionary The dictionary to decrypt.
     * @param objNum The object number.
     * @param genNum The object generation number.
     *
     * @throws IOException If there is an error creating a new string.
     */
    private void decryptDictionary(COSDictionary dictionary, long objNum, long genNum) throws IOException
    {
        if (dictionary.getItem(COSName.CF) != null)
        {
            // PDFBOX-2936: avoid orphan /CF dictionaries found in US govt "I-" files
            return;
        }
        COSName type = dictionary.getCOSName(COSName.TYPE);
        boolean isSignature = COSName.SIG.equals(type) || COSName.DOC_TIME_STAMP.equals(type) ||
                // PDFBOX-4466: /Type is optional, see
                // https://ec.europa.eu/cefdigital/tracker/browse/DSS-1538
                (dictionary.getDictionaryObject(COSName.CONTENTS) instanceof COSString &&
                 dictionary.getDictionaryObject(COSName.BYTERANGE) instanceof COSArray);
        for (Map.Entry<COSName, COSBase> entry : dictionary.entrySet())
        {
            if (isSignature && COSName.CONTENTS.equals(entry.getKey()))
            {
                // do not decrypt the signature contents string
                continue;
            }
            COSBase value = entry.getValue();
            // within a dictionary only the following kind of COS objects have to be decrypted
            if (value instanceof COSString || value instanceof COSArray || value instanceof COSDictionary)
            {
                decrypt(value, objNum, genNum);
            }
        }
    }

    /**
     * This will decrypt a string.
     *
     * @param string the string to decrypt.
     * @param objNum The object number.
     * @param genNum The object generation number.
     *
     * @throws IOException If an error occurs writing the new string.
     */
    private void decryptString(COSString string, long objNum, long genNum) throws IOException
    {
        // String encrypted with identity filter
        if (COSName.IDENTITY.equals(stringFilterName))
        {
            return;
        }

        ByteArrayInputStream data = new ByteArrayInputStream(string.getBytes());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try
        {
            encryptData(objNum, genNum, data, outputStream, true /* decrypt */);
            string.setValue(outputStream.toByteArray());
        }
        catch (IOException ex)
        {
            LOG.error("Failed to decrypt COSString of length " + string.getBytes().length +
                    " in object " + objNum + ": " + ex.getMessage(), ex);
        }
    }

    /**
     * This will encrypt a string.
     *
     * @param string the string to encrypt.
     * @param objNum The object number.
     * @param genNum The object generation number.
     *
     * @throws IOException If an error occurs writing the new string.
     */
    public void encryptString(COSString string, long objNum, int genNum) throws IOException
    {
        ByteArrayInputStream data = new ByteArrayInputStream(string.getBytes());
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        encryptData(objNum, genNum, data, buffer, false /* encrypt */);
        string.setValue(buffer.toByteArray());
    }

    /**
     * This will decrypt an array.
     *
     * @param array The array to decrypt.
     * @param objNum The object number.
     * @param genNum The object generation number.
     *
     * @throws IOException If there is an error accessing the data.
     */
    private void decryptArray(COSArray array, long objNum, long genNum) throws IOException
    {
        for (int i = 0; i < array.size(); i++)
        {
            decrypt(array.get(i), objNum, genNum);
        }
    }

    /**
     * Getter of the property <tt>keyLength</tt>.
     * @return  Returns the keyLength.
     */
    public int getKeyLength()
    {
        return keyLength;
    }

    /**
     * Setter of the property <tt>keyLength</tt>.
     *
     * @param keyLen  The keyLength to set.
     */
    public void setKeyLength(int keyLen)
    {
        this.keyLength = keyLen;
    }

    /**
     * Sets the access permissions.
     *
     * @param currentAccessPermission The access permissions to be set.
     */
    public void setCurrentAccessPermission(AccessPermission currentAccessPermission)
    {
        this.currentAccessPermission = currentAccessPermission;
    }

    /**
     * Returns the access permissions that were computed during document decryption.
     * The returned object is in read only mode.
     *
     * @return the access permissions or null if the document was not decrypted.
     */
    public AccessPermission getCurrentAccessPermission()
    {
        return currentAccessPermission;
    }

    /**
     * True if AES is used for encryption and decryption.
     *
     * @return true if AEs is used
     */
    public boolean isAES()
    {
        return useAES;
    }

    /**
     * Set to true if AES for encryption and decryption should be used.
     *
     * @param aesValue if true AES will be used
     *
     */
    public void setAES(boolean aesValue)
    {
        useAES = aesValue;
    }

    /**
     * Returns whether a protection policy has been set.
     *
     * @return true if a protection policy has been set.
     */
    public boolean hasProtectionPolicy()
    {
        return protectionPolicy != null;
    }

    /**
     * Returns the set {@link ProtectionPolicy} or null.
     *
     * @return The set {@link ProtectionPolicy}.
     */
    protected T_POLICY getProtectionPolicy()
    {
        return protectionPolicy;
    }

    /**
     * Sets the {@link ProtectionPolicy} to the given value.
     * @param protectionPolicy The {@link ProtectionPolicy}, that shall be set.
     */
    protected void setProtectionPolicy(T_POLICY protectionPolicy)
    {
        this.protectionPolicy = protectionPolicy;
    }

    /**
     * Returns the current encryption key data.
     *
     * @return The current encryption key data.
     */
    public byte[] getEncryptionKey()
    {
        return encryptionKey;
    }

    /**
     * Sets the current encryption key data.
     *
     * @param encryptionKey The encryption key data to set.
     */
    public void setEncryptionKey(byte[] encryptionKey)
    {
        this.encryptionKey = encryptionKey;
    }

    /**
     * Computes the version number of the {@link SecurityHandler} based on the encryption key
     * length. See PDF Spec 1.6 p 93 and
     * <a href="https://www.adobe.com/content/dam/acom/en/devnet/pdf/adobe_supplement_iso32000.pdf">PDF
     * 1.7 Supplement ExtensionLevel: 3</a> and
     * <a href="http://intranet.pdfa.org/wp-content/uploads/2016/08/ISO_DIS_32000-2-DIS4.pdf">PDF
     * Spec 2.0</a>.
     *
     * @return The computed version number.
     */
    protected int computeVersionNumber()
    {
        if (keyLength == 40)
        {
            return 1;
        }
        else if (keyLength == 128 && protectionPolicy.isPreferAES())
        {
            return 4;
        }
        else if (keyLength == 256)
        {
            return 5;
        }
        return 2;
    }
}


text:
<?php

namespace Kappa\Deaw\Utils;
use Kappa\Deaw\InvalidArgumentException;

/**
 * Class SelectFactory
 * @package Kappa\Deaw\Utils
 */
class SelectFactory
{
	/**
	 * Prepare SQL select string
	 * @param string|array $select
	 * @param string|null $table
	 * @param string|null $prefix
	 * @return string
	 */
	public static function format($select, $table = null, $prefix = null)
	{
		if (!is_array($select) && !is_string($select)) {
			throw new InvalidArgumentException('Invalid select. Select must be only string or array');
		}

		$result = [];

		if (is_string($select)) {
			$select = explode(',', $select);
		}
		foreach ($select as $key => $column) {
			if (!is_string($column)) {
				throw new InvalidArgumentException('Invalid column \'' . $key . '\'. Column name must be only string');
			}
			$column = trim($column);
			$preparedResult = '';
			if ($table !== null) {
				$preparedResult .= $table . '.' . $column;
			} else {
				$preparedResult .= $column;
			}
			if ($prefix !== null && !preg_match('~\s+as\s+~', $column)) {
				$preparedResult .= ' as ' . $prefix . '_' . $column;
			}
			$result[] = $preparedResult;
		}

		return implode(', ', $result);
	}
}

redpajama_set_name: RedPajamaGithub


text:
-- Can't be FUNCTION bacause RAISERROR used
IF OBJECT_ID('[dbo].[log_message]') IS NOT NULL
	DROP PROCEDURE dbo.log_message;
GO

/**
* example usage:
 DECLARE @dateStart DATETIME = GETDATE() - 1;
 EXEC dbo.log_message @dateStart = @dateStart, @textHead = 'Operation took: '
* output:
* Operation took: 24:0:0.123
*
* @deprecated since log.sp introduced
*/
CREATE PROCEDURE dbo.log_message(@dateStart DATETIME2, @dateEnd DATETIME2 = null, @textHead VARCHAR(1024) = '', @textTrail VARCHAR(1024) = '', @timeMilliseconds INT = 0 OUTPUT, @timeVarchar VARCHAR(12) = '' OUTPUT)
-- Datetime2 is more accurate than datetime: http://stackoverflow.com/questions/1334143/sql-server-datetime2-vs-datetime
AS
BEGIN
	IF (@dateEnd IS NULL)
		SET @dateEnd = GETDATE()

	DECLARE @sql VARCHAR(MAX);
	SET @timeMilliseconds = DATEDIFF(MILLISECOND, @dateStart, @dateEnd);
	SET @timeVarchar = dbo.format_interval(NULL, NULL, @timeMilliseconds);
	SET @sql = REPLACE(@textHead + @timeVarchar + @textTrail, '%', '%%'); -- % has special meaning for RAISERROR
	-- http://stackoverflow.com/questions/306945/how-do-i-flush-the-print-buffer-in-tsql
	RAISERROR(@sql, 0, 1) WITH NOWAIT;
END


text:
WAITFORIT_cmdname=${0##*/}

echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
    cat << USAGE >&2
Usage:
    $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
    -h HOST | --host=HOST       Host or IP under test
    -p PORT | --port=PORT       TCP port under test
                                Alternatively, you specify the host and port as host:port
    -s | --strict               Only execute subcommand if the test succeeds
    -q | --quiet                Don't output any status messages
    -t TIMEOUT | --timeout=TIMEOUT
                                Timeout in seconds, zero for no timeout
    -- COMMAND ARGS             Execute command with args after the test finishes
USAGE
    exit 1
}

wait_for()
{
    if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
        echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
    else
        echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
    fi
    WAITFORIT_start_ts=$(date +%s)
    while :
    do
        if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
            nc -z $WAITFORIT_HOST $WAITFORIT_PORT
            WAITFORIT_result=$?
        else
            (echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
            WAITFORIT_result=$?
        fi
        if [[ $WAITFORIT_result -eq 0 ]]; then
            WAITFORIT_end_ts=$(date +%s)
            echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
            break
        fi
        sleep 1
    done
    return $WAITFORIT_result
}

wait_for_wrapper()
{
    # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
    if [[ $WAITFORIT_QUIET -eq 1 ]]; then
        timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
    else
        timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
    fi
    WAITFORIT_PID=$!
    trap "kill -INT -$WAITFORIT_PID" INT
    wait $WAITFORIT_PID
    WAITFORIT_RESULT=$?
    if [[ $WAITFORIT_RESULT -ne 0 ]]; then
        echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
    fi
    return $WAITFORIT_RESULT
}

# process arguments
while [[ $# -gt 0 ]]
do
    case "$1" in
        *:* )
        WAITFORIT_hostport=(${1//:/ })
        WAITFORIT_HOST=${WAITFORIT_hostport[0]}
        WAITFORIT_PORT=${WAITFORIT_hostport[1]}
        shift 1
        ;;
        --child)
        WAITFORIT_CHILD=1
        shift 1
        ;;
        -q | --quiet)
        WAITFORIT_QUIET=1
        shift 1
        ;;
        -s | --strict)
        WAITFORIT_STRICT=1
        shift 1
        ;;
        -h)
        WAITFORIT_HOST="$2"
        if [[ $WAITFORIT_HOST == "" ]]; then break; fi
        shift 2
        ;;
        --host=*)
        WAITFORIT_HOST="${1#*=}"
        shift 1
        ;;
        -p)
        WAITFORIT_PORT="$2"
        if [[ $WAITFORIT_PORT == "" ]]; then break; fi
        shift 2
        ;;
        --port=*)
        WAITFORIT_PORT="${1#*=}"
        shift 1
        ;;
        -t)
        WAITFORIT_TIMEOUT="$2"
        if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
        shift 2
        ;;
        --timeout=*)
        WAITFORIT_TIMEOUT="${1#*=}"
        shift 1
        ;;
        --)
        shift
        WAITFORIT_CLI=("$@")
        break
        ;;
        --help)
        usage
        ;;
        *)
        echoerr "Unknown argument: $1"
        usage
        ;;
    esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
    echoerr "Error: you need to provide a host and port to test."
    usage
fi

WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}

# Check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)

WAITFORIT_BUSYTIMEFLAG=""
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
    WAITFORIT_ISBUSY=1
    # Check if busybox timeout uses -t flag
    # (recent Alpine versions don't support -t anymore)
    if timeout &>/dev/stdout | grep -q -e '-t '; then
        WAITFORIT_BUSYTIMEFLAG="-t"
    fi
else
    WAITFORIT_ISBUSY=0
fi

if [[ $WAITFORIT_CHILD -gt 0 ]]; then
    wait_for
    WAITFORIT_RESULT=$?
    exit $WAITFORIT_RESULT
else
    if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
        wait_for_wrapper
        WAITFORIT_RESULT=$?
    else
        wait_for
        WAITFORIT_RESULT=$?
    fi
fi

if [[ $WAITFORIT_CLI != "" ]]; then
    if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
        echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
        exit $WAITFORIT_RESULT
    fi
    exec "${WAITFORIT_CLI[@]}"
else
    exit $WAITFORIT_RESULT
fi


text:
#pragma once
#include <aws/finspace-data/FinSpaceData_EXPORTS.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/client/AWSClient.h>
#include <aws/core/utils/json/JsonSerializer.h>
#include <aws/finspace-data/FinSpaceDataServiceClientModel.h>

namespace Aws
{
namespace FinSpaceData
{
  /**
   *
 The FinSpace APIs let you take actions inside the FinSpace.

   */
  class AWS_FINSPACEDATA_API FinSpaceDataClient : public Aws::Client::AWSJsonClient
  {
    public:
      typedef Aws::Client::AWSJsonClient BASECLASS;
      static const char* SERVICE_NAME;
      static const char* ALLOCATION_TAG;

       /**
        * Initializes client to use DefaultCredentialProviderChain, with default http client factory, and optional client config. If client config
        * is not specified, it will be initialized to default values.
        */
        FinSpaceDataClient(const Aws::FinSpaceData::FinSpaceDataClientConfiguration& clientConfiguration = Aws::FinSpaceData::FinSpaceDataClientConfiguration(),
                           std::shared_ptr<FinSpaceDataEndpointProviderBase> endpointProvider = Aws::MakeShared<FinSpaceDataEndpointProvider>(ALLOCATION_TAG));

       /**
        * Initializes client to use SimpleAWSCredentialsProvider, with default http client factory, and optional client config. If client config
        * is not specified, it will be initialized to default values.
        */
        FinSpaceDataClient(const Aws::Auth::AWSCredentials& credentials,
                           std::shared_ptr<FinSpaceDataEndpointProviderBase> endpointProvider = Aws::MakeShared<FinSpaceDataEndpointProvider>(ALLOCATION_TAG),
                           const Aws::FinSpaceData::FinSpaceDataClientConfiguration& clientConfiguration = Aws::FinSpaceData::FinSpaceDataClientConfiguration());

       /**
        * Initializes client to use specified credentials provider with specified client config. If http client factory is not supplied,
        * the default http client factory will be used
        */
        FinSpaceDataClient(const std::shared_ptr<Aws::Auth::AWSCredentialsProvider>& credentialsProvider,
                           std::shared_ptr<FinSpaceDataEndpointProviderBase> endpointProvider = Aws::MakeShared<FinSpaceDataEndpointProvider>(ALLOCATION_TAG),
                           const Aws::FinSpaceData::FinSpaceDataClientConfiguration& clientConfiguration = Aws::FinSpaceData::FinSpaceDataClientConfiguration());

        /* Legacy constructors due deprecation */
       /**
        * Initializes client to use DefaultCredentialProviderChain, with default http client factory, and optional client config. If client config
        * is not specified, it will be initialized to default values.
        */
        FinSpaceDataClient(const Aws::Client::ClientConfiguration& clientConfiguration);

       /**
        * Initializes client to use SimpleAWSCredentialsProvider, with default http client factory, and optional client config. If client config
        * is not specified, it will be initialized to default values.
        */
        FinSpaceDataClient(const Aws::Auth::AWSCredentials& credentials,
                           const Aws::Client::ClientConfiguration& clientConfiguration);

       /**
        * Initializes client to use specified credentials provider with specified client config. If http client factory is not supplied,
        * the default http client factory will be used
        */
        FinSpaceDataClient(const std::shared_ptr<Aws::Auth::AWSCredentialsProvider>& credentialsProvider,
                           const Aws::Client::ClientConfiguration& clientConfiguration);

        /* End of legacy constructors due deprecation */
        virtual ~FinSpaceDataClient();

        /**
         *
Adds a user account to a permission group to grant permissions for actions a
         * user can perform in FinSpace.

See Also:
   AWS
         * API Reference

         */
        virtual Model::AssociateUserToPermissionGroupOutcome AssociateUserToPermissionGroup(const Model::AssociateUserToPermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for AssociateUserToPermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::AssociateUserToPermissionGroupOutcomeCallable AssociateUserToPermissionGroupCallable(const Model::AssociateUserToPermissionGroupRequest& request) const;

        /**
         * An Async wrapper for AssociateUserToPermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void AssociateUserToPermissionGroupAsync(const Model::AssociateUserToPermissionGroupRequest& request, const AssociateUserToPermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Creates a new Changeset in a FinSpace Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::CreateChangesetOutcome CreateChangeset(const Model::CreateChangesetRequest& request) const;

        /**
         * A Callable wrapper for CreateChangeset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::CreateChangesetOutcomeCallable CreateChangesetCallable(const Model::CreateChangesetRequest& request) const;

        /**
         * An Async wrapper for CreateChangeset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void CreateChangesetAsync(const Model::CreateChangesetRequest& request, const CreateChangesetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Creates a Dataview for a Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::CreateDataViewOutcome CreateDataView(const Model::CreateDataViewRequest& request) const;

        /**
         * A Callable wrapper for CreateDataView that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::CreateDataViewOutcomeCallable CreateDataViewCallable(const Model::CreateDataViewRequest& request) const;

        /**
         * An Async wrapper for CreateDataView that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void CreateDataViewAsync(const Model::CreateDataViewRequest& request, const CreateDataViewResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Creates a new FinSpace Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::CreateDatasetOutcome CreateDataset(const Model::CreateDatasetRequest& request) const;

        /**
         * A Callable wrapper for CreateDataset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::CreateDatasetOutcomeCallable CreateDatasetCallable(const Model::CreateDatasetRequest& request) const;

        /**
         * An Async wrapper for CreateDataset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void CreateDatasetAsync(const Model::CreateDatasetRequest& request, const CreateDatasetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Creates a group of permissions for various actions that a user can perform in
         * FinSpace.

See Also:
   AWS
         * API Reference

         */
        virtual Model::CreatePermissionGroupOutcome CreatePermissionGroup(const Model::CreatePermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for CreatePermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::CreatePermissionGroupOutcomeCallable CreatePermissionGroupCallable(const Model::CreatePermissionGroupRequest& request) const;

        /**
         * An Async wrapper for CreatePermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void CreatePermissionGroupAsync(const Model::CreatePermissionGroupRequest& request, const CreatePermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Creates a new user in FinSpace.

See Also:
   AWS
         * API Reference

         */
        virtual Model::CreateUserOutcome CreateUser(const Model::CreateUserRequest& request) const;

        /**
         * A Callable wrapper for CreateUser that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::CreateUserOutcomeCallable CreateUserCallable(const Model::CreateUserRequest& request) const;

        /**
         * An Async wrapper for CreateUser that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void CreateUserAsync(const Model::CreateUserRequest& request, const CreateUserResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Deletes a FinSpace Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::DeleteDatasetOutcome DeleteDataset(const Model::DeleteDatasetRequest& request) const;

        /**
         * A Callable wrapper for DeleteDataset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::DeleteDatasetOutcomeCallable DeleteDatasetCallable(const Model::DeleteDatasetRequest& request) const;

        /**
         * An Async wrapper for DeleteDataset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void DeleteDatasetAsync(const Model::DeleteDatasetRequest& request, const DeleteDatasetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Deletes a permission group. This action is irreversible.

See
         * Also:
   AWS
         * API Reference

         */
        virtual Model::DeletePermissionGroupOutcome DeletePermissionGroup(const Model::DeletePermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for DeletePermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::DeletePermissionGroupOutcomeCallable DeletePermissionGroupCallable(const Model::DeletePermissionGroupRequest& request) const;

        /**
         * An Async wrapper for DeletePermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void DeletePermissionGroupAsync(const Model::DeletePermissionGroupRequest& request, const DeletePermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Denies access to the FinSpace web application and API for the specified
         * user.

See Also:
   AWS
         * API Reference

         */
        virtual Model::DisableUserOutcome DisableUser(const Model::DisableUserRequest& request) const;

        /**
         * A Callable wrapper for DisableUser that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::DisableUserOutcomeCallable DisableUserCallable(const Model::DisableUserRequest& request) const;

        /**
         * An Async wrapper for DisableUser that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void DisableUserAsync(const Model::DisableUserRequest& request, const DisableUserResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Removes a user account from a permission group.

See Also:
   AWS
         * API Reference

         */
        virtual Model::DisassociateUserFromPermissionGroupOutcome DisassociateUserFromPermissionGroup(const Model::DisassociateUserFromPermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for DisassociateUserFromPermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::DisassociateUserFromPermissionGroupOutcomeCallable DisassociateUserFromPermissionGroupCallable(const Model::DisassociateUserFromPermissionGroupRequest& request) const;

        /**
         * An Async wrapper for DisassociateUserFromPermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void DisassociateUserFromPermissionGroupAsync(const Model::DisassociateUserFromPermissionGroupRequest& request, const DisassociateUserFromPermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
 Allows the specified user to access the FinSpace web application and
         * API.

See Also:
   AWS
         * API Reference

         */
        virtual Model::EnableUserOutcome EnableUser(const Model::EnableUserRequest& request) const;

        /**
         * A Callable wrapper for EnableUser that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::EnableUserOutcomeCallable EnableUserCallable(const Model::EnableUserRequest& request) const;

        /**
         * An Async wrapper for EnableUser that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void EnableUserAsync(const Model::EnableUserRequest& request, const EnableUserResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Get information about a Changeset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::GetChangesetOutcome GetChangeset(const Model::GetChangesetRequest& request) const;

        /**
         * A Callable wrapper for GetChangeset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetChangesetOutcomeCallable GetChangesetCallable(const Model::GetChangesetRequest& request) const;

        /**
         * An Async wrapper for GetChangeset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetChangesetAsync(const Model::GetChangesetRequest& request, const GetChangesetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Gets information about a Dataview.

See Also:
   AWS
         * API Reference

         */
        virtual Model::GetDataViewOutcome GetDataView(const Model::GetDataViewRequest& request) const;

        /**
         * A Callable wrapper for GetDataView that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetDataViewOutcomeCallable GetDataViewCallable(const Model::GetDataViewRequest& request) const;

        /**
         * An Async wrapper for GetDataView that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetDataViewAsync(const Model::GetDataViewRequest& request, const GetDataViewResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Returns information about a Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::GetDatasetOutcome GetDataset(const Model::GetDatasetRequest& request) const;

        /**
         * A Callable wrapper for GetDataset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetDatasetOutcomeCallable GetDatasetCallable(const Model::GetDatasetRequest& request) const;

        /**
         * An Async wrapper for GetDataset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetDatasetAsync(const Model::GetDatasetRequest& request, const GetDatasetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Returns the credentials to access the external Dataview from an S3 location.
         * To call this API:

You must retrieve the programmatic
         * credentials.

You must be a member of a FinSpace user group,
         * where the dataset that you want to access has Read Dataset Data
         * permissions.

See Also:
   AWS
         * API Reference

         */
        virtual Model::GetExternalDataViewAccessDetailsOutcome GetExternalDataViewAccessDetails(const Model::GetExternalDataViewAccessDetailsRequest& request) const;

        /**
         * A Callable wrapper for GetExternalDataViewAccessDetails that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetExternalDataViewAccessDetailsOutcomeCallable GetExternalDataViewAccessDetailsCallable(const Model::GetExternalDataViewAccessDetailsRequest& request) const;

        /**
         * An Async wrapper for GetExternalDataViewAccessDetails that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetExternalDataViewAccessDetailsAsync(const Model::GetExternalDataViewAccessDetailsRequest& request, const GetExternalDataViewAccessDetailsResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Retrieves the details of a specific permission group.

See
         * Also:
   AWS
         * API Reference

         */
        virtual Model::GetPermissionGroupOutcome GetPermissionGroup(const Model::GetPermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for GetPermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetPermissionGroupOutcomeCallable GetPermissionGroupCallable(const Model::GetPermissionGroupRequest& request) const;

        /**
         * An Async wrapper for GetPermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetPermissionGroupAsync(const Model::GetPermissionGroupRequest& request, const GetPermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Request programmatic credentials to use with FinSpace SDK.

See
         * Also:
   AWS
         * API Reference

         */
        virtual Model::GetProgrammaticAccessCredentialsOutcome GetProgrammaticAccessCredentials(const Model::GetProgrammaticAccessCredentialsRequest& request) const;

        /**
         * A Callable wrapper for GetProgrammaticAccessCredentials that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetProgrammaticAccessCredentialsOutcomeCallable GetProgrammaticAccessCredentialsCallable(const Model::GetProgrammaticAccessCredentialsRequest& request) const;

        /**
         * An Async wrapper for GetProgrammaticAccessCredentials that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetProgrammaticAccessCredentialsAsync(const Model::GetProgrammaticAccessCredentialsRequest& request, const GetProgrammaticAccessCredentialsResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Retrieves details for a specific user.

See Also:
   AWS
         * API Reference

         */
        virtual Model::GetUserOutcome GetUser(const Model::GetUserRequest& request) const;

        /**
         * A Callable wrapper for GetUser that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetUserOutcomeCallable GetUserCallable(const Model::GetUserRequest& request) const;

        /**
         * An Async wrapper for GetUser that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetUserAsync(const Model::GetUserRequest& request, const GetUserResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
A temporary Amazon S3 location, where you can copy your files from a source
         * location to stage or use as a scratch space in FinSpace notebook.

See
         * Also:
   AWS
         * API Reference

         */
        virtual Model::GetWorkingLocationOutcome GetWorkingLocation(const Model::GetWorkingLocationRequest& request) const;

        /**
         * A Callable wrapper for GetWorkingLocation that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::GetWorkingLocationOutcomeCallable GetWorkingLocationCallable(const Model::GetWorkingLocationRequest& request) const;

        /**
         * An Async wrapper for GetWorkingLocation that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void GetWorkingLocationAsync(const Model::GetWorkingLocationRequest& request, const GetWorkingLocationResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists the FinSpace Changesets for a Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::ListChangesetsOutcome ListChangesets(const Model::ListChangesetsRequest& request) const;

        /**
         * A Callable wrapper for ListChangesets that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListChangesetsOutcomeCallable ListChangesetsCallable(const Model::ListChangesetsRequest& request) const;

        /**
         * An Async wrapper for ListChangesets that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListChangesetsAsync(const Model::ListChangesetsRequest& request, const ListChangesetsResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists all available Dataviews for a Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::ListDataViewsOutcome ListDataViews(const Model::ListDataViewsRequest& request) const;

        /**
         * A Callable wrapper for ListDataViews that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListDataViewsOutcomeCallable ListDataViewsCallable(const Model::ListDataViewsRequest& request) const;

        /**
         * An Async wrapper for ListDataViews that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListDataViewsAsync(const Model::ListDataViewsRequest& request, const ListDataViewsResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists all of the active Datasets that a user has access to.

See
         * Also:
   AWS
         * API Reference

         */
        virtual Model::ListDatasetsOutcome ListDatasets(const Model::ListDatasetsRequest& request) const;

        /**
         * A Callable wrapper for ListDatasets that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListDatasetsOutcomeCallable ListDatasetsCallable(const Model::ListDatasetsRequest& request) const;

        /**
         * An Async wrapper for ListDatasets that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListDatasetsAsync(const Model::ListDatasetsRequest& request, const ListDatasetsResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists all available permission groups in FinSpace.

See Also:

         * AWS
         * API Reference

         */
        virtual Model::ListPermissionGroupsOutcome ListPermissionGroups(const Model::ListPermissionGroupsRequest& request) const;

        /**
         * A Callable wrapper for ListPermissionGroups that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListPermissionGroupsOutcomeCallable ListPermissionGroupsCallable(const Model::ListPermissionGroupsRequest& request) const;

        /**
         * An Async wrapper for ListPermissionGroups that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListPermissionGroupsAsync(const Model::ListPermissionGroupsRequest& request, const ListPermissionGroupsResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists all the permission groups that are associated with a specific user
         * account.

See Also:
   AWS
         * API Reference

         */
        virtual Model::ListPermissionGroupsByUserOutcome ListPermissionGroupsByUser(const Model::ListPermissionGroupsByUserRequest& request) const;

        /**
         * A Callable wrapper for ListPermissionGroupsByUser that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListPermissionGroupsByUserOutcomeCallable ListPermissionGroupsByUserCallable(const Model::ListPermissionGroupsByUserRequest& request) const;

        /**
         * An Async wrapper for ListPermissionGroupsByUser that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListPermissionGroupsByUserAsync(const Model::ListPermissionGroupsByUserRequest& request, const ListPermissionGroupsByUserResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists all available user accounts in FinSpace.

See Also:
   AWS
         * API Reference

         */
        virtual Model::ListUsersOutcome ListUsers(const Model::ListUsersRequest& request) const;

        /**
         * A Callable wrapper for ListUsers that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListUsersOutcomeCallable ListUsersCallable(const Model::ListUsersRequest& request) const;

        /**
         * An Async wrapper for ListUsers that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListUsersAsync(const Model::ListUsersRequest& request, const ListUsersResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Lists details of all the users in a specific permission group.

See
         * Also:
   AWS
         * API Reference

         */
        virtual Model::ListUsersByPermissionGroupOutcome ListUsersByPermissionGroup(const Model::ListUsersByPermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for ListUsersByPermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ListUsersByPermissionGroupOutcomeCallable ListUsersByPermissionGroupCallable(const Model::ListUsersByPermissionGroupRequest& request) const;

        /**
         * An Async wrapper for ListUsersByPermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ListUsersByPermissionGroupAsync(const Model::ListUsersByPermissionGroupRequest& request, const ListUsersByPermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Resets the password for a specified user ID and generates a temporary one.
         * Only a superuser can reset password for other users. Resetting the password
         * immediately invalidates the previous password associated with the
         * user.

See Also:
   AWS
         * API Reference

         */
        virtual Model::ResetUserPasswordOutcome ResetUserPassword(const Model::ResetUserPasswordRequest& request) const;

        /**
         * A Callable wrapper for ResetUserPassword that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::ResetUserPasswordOutcomeCallable ResetUserPasswordCallable(const Model::ResetUserPasswordRequest& request) const;

        /**
         * An Async wrapper for ResetUserPassword that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void ResetUserPasswordAsync(const Model::ResetUserPasswordRequest& request, const ResetUserPasswordResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Updates a FinSpace Changeset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::UpdateChangesetOutcome UpdateChangeset(const Model::UpdateChangesetRequest& request) const;

        /**
         * A Callable wrapper for UpdateChangeset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::UpdateChangesetOutcomeCallable UpdateChangesetCallable(const Model::UpdateChangesetRequest& request) const;

        /**
         * An Async wrapper for UpdateChangeset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void UpdateChangesetAsync(const Model::UpdateChangesetRequest& request, const UpdateChangesetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Updates a FinSpace Dataset.

See Also:
   AWS
         * API Reference

         */
        virtual Model::UpdateDatasetOutcome UpdateDataset(const Model::UpdateDatasetRequest& request) const;

        /**
         * A Callable wrapper for UpdateDataset that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::UpdateDatasetOutcomeCallable UpdateDatasetCallable(const Model::UpdateDatasetRequest& request) const;

        /**
         * An Async wrapper for UpdateDataset that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void UpdateDatasetAsync(const Model::UpdateDatasetRequest& request, const UpdateDatasetResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Modifies the details of a permission group. You cannot modify a
         * permissionGroupID.

See Also:
   AWS
         * API Reference

         */
        virtual Model::UpdatePermissionGroupOutcome UpdatePermissionGroup(const Model::UpdatePermissionGroupRequest& request) const;

        /**
         * A Callable wrapper for UpdatePermissionGroup that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::UpdatePermissionGroupOutcomeCallable UpdatePermissionGroupCallable(const Model::UpdatePermissionGroupRequest& request) const;

        /**
         * An Async wrapper for UpdatePermissionGroup that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void UpdatePermissionGroupAsync(const Model::UpdatePermissionGroupRequest& request, const UpdatePermissionGroupResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

        /**
         *
Modifies the details of the specified user account. You cannot update the
         * userId for a user.

See Also:
   AWS
         * API Reference

         */
        virtual Model::UpdateUserOutcome UpdateUser(const Model::UpdateUserRequest& request) const;

        /**
         * A Callable wrapper for UpdateUser that returns a future to the operation so that it can be executed in parallel to other requests.
         */
        virtual Model::UpdateUserOutcomeCallable UpdateUserCallable(const Model::UpdateUserRequest& request) const;

        /**
         * An Async wrapper for UpdateUser that queues the request into a thread executor and triggers associated callback when operation has finished.
         */
        virtual void UpdateUserAsync(const Model::UpdateUserRequest& request, const UpdateUserResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const;

      void OverrideEndpoint(const Aws::String& endpoint);
      std::shared_ptr<FinSpaceDataEndpointProviderBase>& accessEndpointProvider();
    private:
      void init(const FinSpaceDataClientConfiguration& clientConfiguration);

      FinSpaceDataClientConfiguration m_clientConfiguration;
      std::shared_ptr<Aws::Utils::Threading::Executor> m_executor;
      std::shared_ptr<FinSpaceDataEndpointProviderBase> m_endpointProvider;
  };

} // namespace FinSpaceData
} // namespace Aws


text:
import React from "react";
import { from } from "rxjs";
import HackerNewsApi from "../../api/HackerNewsApi";
import CommentComponent from "./CommentComponent";
import { URL, Link } from "async-react-router";

export default class ItemComponent extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      isLoading: true
    };

    this.commentApiStream = null;
  }

  componentDidMount() {
    this.commentApiStream = from(
      HackerNewsApi.getComments(this.props.item.kids)
    ).subscribe((comments) => {
      this.props.actions.comments.sync(comments);
      this.setState({ isLoading: false });
    });
  }

  componentWillUnmount() {
    this.commentApiStream.unsubscribe();
  }

  render() {
    const loadingComponent = this.getLoadingComponent();
    const commentComponents = this.getCommentComponents();
    return (

            {this.props.item.title}

{this.props.item.score} points

                by{" "}
                <Link
                  to={URL.name("UserPage", { userId: this.props.item.by })}
                >
                  {this.props.item.by}
                </Link>

{this.props.item.getTimeAgo()}

Comment {loadingComponent}

{commentComponents}

    );
  }

  getCommentComponents() {
    if (this.state.isLoading) {
      return
Loading...
;
    }

    if (this.props.comments.isEmpty()) {
      return
No comments yet.
;
    }

    return this.props.comments
      .filter((comment) => !!comment.by)
      .map((comment) => (
        <CommentComponent key={comment.id} comment={comment} />
      ));
  }

  getLoadingComponent() {
    if (this.state.isLoading) {
      return (

      );
    }

    return null;
  }
}


text:
using System;
using System.Collections.Generic;

namespace Isop.Abstractions
{
    /// <summary>
    /// AppHost contains the service provider and configuration needed to run a command line app
    /// </summary>
    public interface IAppHost
    {
        /// <summary>
        /// Parse command line arguments and return parsed arguments entity
        /// </summary>
        IParsed Parse(IEnumerable<string> arg);

        /// <summary>
        /// Return help-text
        /// </summary>
        String Help();

        /// <summary>
        ///
        /// </summary>
        IController Controller(string controllerName);
        /// <summary>
        /// Controllers exposed by the API
        /// </summary>
        IReadOnlyList<IController> Controllers { get; }
    }
}


text:
import React from 'react';
import { action } from '@storybook/addon-actions';

import InputDateRangePicker from './InputDateRangePicker.component';

import DateManager from '../Date/Manager';
import DatePicker from '../Date/Picker';

export default {
	title: 'Form/Controls/DatePicker/Date Range',

	decorators: [
		story => (
			<form
				onSubmit={event => {
					event.persist();
					event.preventDefault();
					action('submit')(event);
				}}
			>
				{story()}
			</form>
		),
	],
};

export const Input = () => (
	<InputDateRangePicker
		id="my-date-picker"
		name="daterange"
		onBlur={action('onBlur')}
		onChange={action('onChange')}
	/>
);

export const Disabled = () => (
	<InputDateRangePicker
		disabled
		id="my-date-picker"
		name="daterange"
		onBlur={action('onBlur')}
		onChange={action('onChange')}
	/>
);

export const InputWithMinWidth = () => (
	<InputDateRangePicker
		id="my-date-picker"
		name="daterange"
		onBlur={action('onBlur')}
		onChange={action('onChange')}
		minWidth={250}
	/>
);

export const Picker = () => (
	<div style={{ border: '1px solid black', width: 320 }}>
		<DateManager id="simple" onChange={action('onChange', { depth: 3 })}>
			<DatePicker selectedDate={new Date(2019, 9, 24)} endDate={new Date(2019, 9, 30)} />
		</DateManager>
	</div>
);

export const Inline = () => (
	<InputDateRangePicker
		id="my-date-picker"
		name="daterange"
		onBlur={action('onBlur')}
		onChange={action('onChange')}
		inline
	/>
);


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jaydenxiao.common">

<application
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >
    <activity
        android:name=".imagePager.BigImagePagerActivity"/>
</application>

</manifest>


text:
{% extends 'base.html' %}
{% block content %}
{% if message %}{{message}}

{% endif %}
<form action="{% url reference_url group.pk %}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

Back to group
{% endblock %}


text:
[ ![Codeship Status for gabtemp/ascii-table](https://codeship.com/projects/25fa57c0-a46f-0133-8425-16383a36e4d1/status?branch=master)](https://codeship.com/projects/129434)

A java library for creating a nice and clean text table.


text:
package main

import (
	"crypto/md5"
	"encoding/gob"
	"errors"
	"flag"
	"fmt"
	"hash/fnv"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"code.google.com/p/goauth2/oauth"
	drive "code.google.com/p/google-api-go-client/drive/v2"
)

var config = &oauth.Config{
	ClientId:     "", // Set by --clientid or --clientid_file
	ClientSecret: "", // Set by --secret or --secret_file
	Scope:        "", // filled in per-API
	AuthURL:      "https://accounts.google.com/o/oauth2/auth",
	TokenURL:     "https://accounts.google.com/o/oauth2/token",
}

// Flags
var (
	clientId     = flag.String("clientid", "", "OAuth Client ID.  If non-empty, overrides --clientid_file")
	clientIdFile = flag.String("clientid_file", "clientid.dat",
		"Name of a file containing just the project's OAuth Client ID from https://code.google.com/apis/console/")
	secret     = flag.String("secret", "", "OAuth Client Secret.  If non-empty, overrides --secret_file")
	secretFile = flag.String("secret_file", "clientsecret.dat",
		"Name of a file containing just the project's OAuth Client Secret from https://code.google.com/apis/console/")
	cacheToken  = flag.Bool("cachetoken", true, "cache the OAuth token")
	debug       = flag.Bool("debug", false, "show HTTP traffic")
	driveFolder = flag.String("drivefolder", "", "The name of a folder on Googel Drive")
	localFolder = flag.String("localfolder", "", "The folder on the local file system")
)

func getDriveFilesByFolder(service *drive.Service, folderName string) (map[string]*drive.File, string) {
	query := fmt.Sprintf("mimeType = 'application/vnd.google-apps.folder' and title = '%s'", folderName)
	folderList, err := service.Files.List().Q(query).Do()
	log.Printf("Got folderList, err: %#v, %v", folderList, err)

	if len(folderList.Items) != 1 {
		log.Fatalf("Number of folders: %d != 1 for %s on Google Drive", len(folderList.Items), *driveFolder)
	}

	files := make(map[string]*drive.File)

	folderId := folderList.Items[0].Id
	query = fmt.Sprintf("'%s' in parents", folderId)
	fileList, err := service.Files.List().Q(query).Do()
	log.Printf("Got fileList, err: %#v, %v", fileList, err)
	for _, file := range fileList.Items {
		key := fmt.Sprintf("%s:%s", file.Title, file.Md5Checksum)
		files[key] = file
	}

	doPaging := true
	for doPaging && fileList.NextPageToken != "" {
		log.Printf("Going to the next page")
		fileList, err = service.Files.List().Q(query).PageToken(fileList.NextPageToken).Do()
		for _, file := range fileList.Items {
			key := fmt.Sprintf("%s:%s", file.Title, file.Md5Checksum)
			files[key] = file
		}
	}

	return files, folderId
}

type File struct {
	filename string
	base     string
	md5      string
}

func getLocalFiles(folderName string) map[string]File {
	files := make(map[string]File)

	glob := filepath.Join(folderName, "*")
	filenames, _ := filepath.Glob(glob)
	for _, filename := range filenames {
		fileInfo, err := os.Stat(filename)
		if err != nil {
			log.Fatalf("Failed to stat %s", filename)
		}
		if fileInfo.IsDir() || strings.HasPrefix(fileInfo.Name(), ".") {
			continue
		}

		file, err := os.Open(filename)
		if err != nil {
			log.Fatalf("Failed to open %s", filename)
		}

		md5er := md5.New()
		io.Copy(md5er, file)
		hash := fmt.Sprintf("%x", md5er.Sum(nil))
		file.Close()

		base := filepath.Base(filename)
		key := fmt.Sprintf("%s:%s", base, hash)
		files[key] = File{filename, base, hash}
	}

	return files
}

func mergeFolders(driveFiles map[string]*drive.File, localFiles map[string]File) []File {
	var remote []*drive.File
	var local []File

	synced := make(map[string]bool)

	for key, file := range driveFiles {
		_, ok := localFiles[key]
		if !ok {
			remote = append(remote, file)
		} else {
			synced[key] = true
		}
	}

	for key, file := range localFiles {
		_, ok := driveFiles[key]
		if !ok {
			local = append(local, file)
		} else {
			synced[key] = true
		}
	}

	fmt.Printf("%v files are synced correctly\n", len(synced))

	if len(remote) > 0 {
		fmt.Printf("%v files are visible on remote but not local\n", len(remote))
		for _, file := range remote {
			fmt.Printf("%v\t%v\t%v\t%v\t%v\n",
				file.Title,
				file.FileSize,
				file.OwnerNames,
				file.Md5Checksum,
				file.Id,
			)
		}
	}

	if len(local) > 0 {
		fmt.Printf("%v files have not been synched\n", len(local))
		for _, file := range local {
			fmt.Printf("%v\t%v\n",
				file.filename,
				file.md5,
			)
		}
	}

	return local
}

func main() {
	flag.Parse()

	config.Scope = drive.DriveScope
	config.ClientId = valueOrFileContents(*clientId, *clientIdFile)
	config.ClientSecret = valueOrFileContents(*secret, *secretFile)

	client := getOAuthClient(config)
	service, _ := drive.New(client)

	// Get the ID here
	driveFiles, folderId := getDriveFilesByFolder(service, *driveFolder)
	localFiles := getLocalFiles(*localFolder)

	toSync := mergeFolders(driveFiles, localFiles)

	// Synchronization
	fmt.Printf("Synchronizing %v files", len(toSync))

	work := make(chan uploadRequest, 1)
	done := make(chan bool, len(toSync))
	for i := 0; i < 3; i++ {
		go uploadWorker(service, work, done)
	}

	total := len(toSync)
	for _, file := range toSync {
		goFile, err := os.Open(file.filename)
		if err != nil {
			log.Fatalf("Could not open file %s: %s", file.filename, err)
		}
		driveFile := drive.File{
			Title: file.base,
			Parents: []*drive.ParentReference{
				&drive.ParentReference{Id: folderId},
			},
		}

		work <- uploadRequest{&driveFile, goFile, file.filename}
		total = total - 1
		log.Printf("%d files left to sync", total)
	}

	for _, _ = range toSync {
		<-done
	}
	log.Printf("Done synchronizing!")
}

type uploadRequest struct {
	driveFile *drive.File
	localFile *os.File
	filename  string
}

func uploadWorker(service *drive.Service, work <-chan uploadRequest, done chan bool) {
	for req := range work {
		log.Printf("++ Uploading %s", req.filename)
		_, err := service.Files.Insert(req.driveFile).Media(req.localFile).Do()
		if err != nil {
			log.Fatalf("Failed when uploading %s: %s", req.filename, err)
		}

		log.Printf("-- Done uploading %s", req.filename)
		req.localFile.Close()
		done <- true
	}
}

func osUserCacheDir() string {
	switch runtime.GOOS {
	case "darwin":
		return filepath.Join(os.Getenv("HOME"), "Library", "Caches")
	case "linux", "freebsd":
		return filepath.Join(os.Getenv("HOME"), ".cache")
	}
	log.Printf("TODO: osUserCacheDir on GOOS %q", runtime.GOOS)
	return "."
}

func tokenCacheFile(config *oauth.Config) string {
	hash := fnv.New32a()
	hash.Write([]byte(config.ClientId))
	hash.Write([]byte(config.ClientSecret))
	hash.Write([]byte(config.Scope))
	fn := fmt.Sprintf("gdrive-sync-tok%v", hash.Sum32())
	return filepath.Join(osUserCacheDir(), url.QueryEscape(fn))
}

func tokenFromFile(file string) (*oauth.Token, error) {
	if !*cacheToken {
		return nil, errors.New("--cachetoken is false")
	}
	f, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	t := new(oauth.Token)
	err = gob.NewDecoder(f).Decode(t)
	return t, err
}

func saveToken(file string, token *oauth.Token) {
	f, err := os.Create(file)
	if err != nil {
		log.Printf("Warning: failed to cache oauth token: %v", err)
		return
	}
	defer f.Close()
	gob.NewEncoder(f).Encode(token)
}

func condDebugTransport(rt http.RoundTripper) http.RoundTripper {
	if *debug {
		return &logTransport{rt}
	}
	return rt
}

func getOAuthClient(config *oauth.Config) *http.Client {
	cacheFile := tokenCacheFile(config)
	token, err := tokenFromFile(cacheFile)
	if err != nil {
		token = tokenFromWeb(config)
		saveToken(cacheFile, token)
	} else {
		log.Printf("Using cached token %#v from %q", token, cacheFile)
	}

	t := &oauth.Transport{
		Token:     token,
		Config:    config,
		Transport: condDebugTransport(http.DefaultTransport),
	}
	return t.Client()
}

func tokenFromWeb(config *oauth.Config) *oauth.Token {
	ch := make(chan string)
	randState := fmt.Sprintf("st%d", time.Now().UnixNano())
	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		if req.URL.Path == "/favicon.ico" {
			http.Error(rw, "", 404)
			return
		}
		if req.FormValue("state") != randState {
			log.Printf("State doesn't match: req = %#v", req)
			http.Error(rw, "", 500)
			return
		}
		if code := req.FormValue("code"); code != "" {
			fmt.Fprintf(rw, "
Success
Authorized.")
			rw.(http.Flusher).Flush()
			ch <- code
			return
		}
		log.Printf("no code")
		http.Error(rw, "", 500)
	}))
	defer ts.Close()

	config.RedirectURL = ts.URL
	authUrl := config.AuthCodeURL(randState)
	go openUrl(authUrl)
	log.Printf("Authorize this app at: %s", authUrl)
	code := <-ch
	log.Printf("Got code: %s", code)

	t := &oauth.Transport{
		Config:    config,
		Transport: condDebugTransport(http.DefaultTransport),
	}
	_, err := t.Exchange(code)
	if err != nil {
		log.Fatalf("Token exchange error: %v", err)
	}
	return t.Token
}

func openUrl(url string) {
	try := []string{"xdg-open", "google-chrome", "open"}
	for _, bin := range try {
		err := exec.Command(bin, url).Run()
		if err == nil {
			return
		}
	}
	log.Printf("Error opening URL in browser.")
}

func valueOrFileContents(value string, filename string) string {
	if value != "" {
		return value
	}
	slurp, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatalf("Error reading %q: %v", filename, err)
	}
	return strings.TrimSpace(string(slurp))
}


text:
layout: post
title: go - consul 服务注册
categories: go
description: go - consul 服务注册
keywords: go
---

go - consul 服务注册

# consul

consul 是微服务架构中的 服务注册与发现 中间件。

## consul 安装

### 具体安装过程

> https://learn.hashicorp.com/consul/getting-started/install

### 官网 demo

> https://demo.consul.io/ui/

# 使用 go 进行连接

## go web 程序编写

使用 go 编写一个简单的 web 服务程序

> main.go

```go
package main

import (
  "fmt"
  "net/http"
)

// test 用来处理 来自 /test 的请求
func test(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.URL)
    w.Write([]byte("hello world"))
}

func main(){
  http.HandleFunc("/test", test)
  // 在端口 8099 启动服务器
  http.ListenAndServe(":8099", nil)
}

```

## 注册 consul

### 首选要获取 go 的 consul 包

```shell
go get -u github.com/hashicorp/consul/api
```

### 获取 go kit 包

```shell
go get -u github.com/go-kit/kit
```

### 使用 go 进行连接

> main.go

```go
package main

import (
    "fmt"
    "github.com/go-kit/kit/log"
    "github.com/go-kit/kit/sd/consul"
    "github.com/hashicorp/consul/api"
    "net/http"
)

func test(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.URL)
    w.Write([]byte("hello world"))
}

func main() {
    RegisterConsul()

    http.HandleFunc("/", test)
    http.ListenAndServe("localhost:8099", nil)
}

func RegisterConsul() {
    serviceName := "test"
    address := "localhost"
    port := 8099
    consulConfig := api.DefaultConfig()
    consulConfig.Address = "http://localhost:8500"
    consulClient, err := api.NewClient(consulConfig)
    if err != nil {
        fmt.Println("consul_err", err)
    }
    client := consul.NewClient(consulClient)
    asg := &api.AgentServiceRegistration{
        ID:      fmt.Sprintf("%s:%s:%d", serviceName, address, port),
        Name:    serviceName,
        Port:    port,
        Address: address,
        Check: &api.AgentServiceCheck{
            DeregisterCriticalServiceAfter: "5m",
            Interval:                       "10s",
            TCP:                            fmt.Sprintf("%s:%d", address, 8099),
        },
    }
    register := consul.NewRegistrar(client, asg, log.NewNopLogger())
    register.Register()
}

```

> 首先使用 api.DefaultConfig() 创建默认的 consulConfig

```go
consulConfig := api.DefaultConfig
consulConfig.Address = "http://localhost:8500"
```

> 然后使用 consulConfig 创建 consulClient

```go
consulClient := api.NewClient(consulConfig)
```

> 然后使用 go-kit consul 创建 client

```go
client := consul.NewClient(consulClient)
```

> 配置参数

```go
asg := &api.AgentServiceRegistration{
      ID:      fmt.Sprintf("%s:%s:%d", serviceName, address, port),
      Name:    serviceName,
      Port:    port,
      Address: address,
      Check: &api.AgentServiceCheck{
          DeregisterCriticalServiceAfter: "5m",
          Interval:                       "10s",
          TCP:                            fmt.Sprintf("%s:%d", address, 8099),
      },
  }
```

> 创建 register

```go
register := consul.NewRegistrar(client, asg, log.NewNopLogger())
```

> 注册

```go
register.Register()
```


text:
'use strict';

require('dotenv').config();

const fetch  = require('node-fetch');

const FILENAME = 'writeFile.txt';

describe('writeFile', () => {
  let fs, helper;

  beforeEach(async () => {
    fs     = await require('../../testUtils/createAzureBlobFS')();
    helper = require('../../testUtils/testHelper')(fs.promise);
  });

  describe(`when write "TEST" to "${ FILENAME }"`, () => {
    beforeEach(async () => {
      await helper.ensureUnlinkIfExists(FILENAME);
      await fs.promise.writeFile(FILENAME, 'TEST', { contentSettings: { contentType: 'text/plain' }, metadata: { hello: 'Aloha!' } });
    });

    afterEach(async () => await helper.ensureUnlinkIfExists(FILENAME));

    test('should have wrote "TEST" to the file', async () => {
      const now = Date.now();
      const token = fs.sas(FILENAME, { expiry: now + 15 * 60000, flag: 'r' });
      const url = `https://${ process.env.AZURE_STORAGE_ACCOUNT }.blob.core.windows.net/${ process.env.TEST_CONTAINER }/writeFile.txt?${ token }`;
      const res = await fetch(url);

      expect(res.status).toBe(200);

      const content = await res.text();

      expect(content).toBe('TEST');
    });

    describe('when stat-ing the file', () => {
      let stat;

      beforeEach(async () => {
        stat = await fs.promise.stat(FILENAME, { metadata: true });
      });

      test('should have a file size of 4 bytes', () => {
        expect(stat.size).toBe(4);
      });

      test('should not be a directory', () => {
        expect(stat.isDirectory()).toBe(false);
      });

      test('should have metadata "hello" equals to "Aloha!"', () => {
        expect(stat.metadata).toEqual({ hello: 'Aloha!' });
      });

      test('should have "Content-Settings" set to "text/plain"', () => {
        expect(stat.contentSettings.contentType).toBe('text/plain');
      });
    });
  });
});


text:
__all__ = ['hillas_parameters',
           'signal_to_border_distance']


text:
<?php
/**
 * Created by PhpStorm.
 * User: degola
 * Date: 29.11.13
 * Time: 00:30
 */

namespace slc\MVC\Storage;

use slc\MVC\Resources;

class Engine_Driver_MySQL extends Engine_Base {
	public function __construct($StorageEngine) {
		if(!is_null($StorageEngine))
			parent::__construct($StorageEngine);
		else
			parent::__construct(Resources::Factory('Database', 'default'));
	}
	public function get($objectId) {
		$objectData = $this->splitObjectId($objectId);
		if(defined($objectData->Class.'::TABLE'))
			$table = constant($objectData->Class.'::TABLE');
		else throw new Engine_Driver_MySQL_Exception('INVALID_CLASS_FOR_STORAGE_DRIVER', array('Class' => $objectData->Class, 'Id' => $objectData->Id));

		$primaryKey = constant($objectData->Class.'::PRIMARY_FIELD_NAME');

		$sql = 'SELECT
			*
		FROM
			`'.$table.'`
		WHERE
			`'.$primaryKey.'`='.$this->getStorageEngine()->quote($objectData->Id);

		$result = $this->getStorageEngine()->query($sql)->fetchObject();

		return $result?$result:false;
	}
	public function getMulti(array $objectIds = array()) {
		$idsByTable = array();
		$tables = array();
		$resultData = array();
		foreach($objectIds AS $objectId) {
			$objectData = $this->splitObjectId($objectId);
			if(defined($objectData->Class.'::TABLE'))
				$table = constant($objectData->Class.'::TABLE');
			else throw new Engine_Driver_MySQL_Exception('INVALID_CLASS_FOR_STORAGE_DRIVER', array('Class' => $objectData->Class, 'Id' => $objectData->Id));

			if(!isset($idsByTable[$table])) {
				$idsByTable[$table] = array();
				$tables[$table] = (object)array(
					'PrimaryField' => constant($objectData->Class.'::PRIMARY_FIELD_NAME'),
					'Class' => $objectData->Class
				);
			}
			$idsByTable[$table][] = $this->getStorageEngine()->quote($objectId);
		}
		foreach($idsByTable AS $table => $ids) {
			$sql = 'SELECT
				*
			FROM
				`'.$table.'`
			WHERE
				`'.$tables[$table]->PrimaryField.'` IN ('.implode(', ', $ids).')';
			$result = $this->getStorageEngine()->query($sql);
			while($row = $result->fetchObject()) {
				$resultData[$tables[$table]->Class.'::'.$row->{$tables[$table]->PrimaryField}] = $row;
			}
		}
		return $resultData;
	}

	public function set($objectId, $value) {
		$objectData = $this->splitObjectId($objectId);
		if(defined($objectData->Class.'::TABLE'))
			$table = constant($objectData->Class.'::TABLE');
		else throw new Engine_Driver_MySQL_Exception('INVALID_CLASS_FOR_STORAGE_DRIVER', array('Class' => $objectData->Class, 'Id' => $objectData->Id));

		$onDuplicateKey = array();
		foreach($value AS $k => $v) {
			$onDuplicateKey[] = '`'.$k.'`=VALUES(`'.$k.'`)';
			$value[$k] = $this->getStorageEngine()->quote($v);
		}

		$sql = 'INSERT INTO
			`'.$table.'` (`'.implode('`, `', array_keys($value)).'`) VALUES ('.implode(', ', $value).')
		ON DUPLICATE KEY UPDATE
			'.implode(', ', $onDuplicateKey);
		$this->getStorageEngine()->query($sql);
	}
	public function setMulti(array $values = array()) {
		throw new Engine_Base_Exception('NOT_IMPLEMENTED', array('Values' => $values));
	}

	public function create($objectId, $value) {
		$objectData = $this->splitObjectId($objectId);
		if(defined($objectData->Class.'::TABLE'))
			$table = constant($objectData->Class.'::TABLE');
		else throw new Engine_Driver_MySQL_Exception('INVALID_CLASS_FOR_STORAGE_DRIVER', array('Class' => $objectData->Class, 'Id' => $objectData->Id));

		if(defined($objectData->Class.'::PRIMARY_FIELD_NAME'))
			$primaryKey = constant($objectData->Class.'::PRIMARY_FIELD_NAME');
		else throw new Engine_Driver_MySQL_Exception('INVALID_PRIMARY_FIELD_NAME_FOR_STORAGE_DRIVER', array('Class' => $objectData->Class, 'Id' => $objectData->Id));

		$value[$primaryKey] = $objectData->Id;

		$insertData = array();
		foreach($value AS $key => $value) {
			$insertData[':'.$key] = $value;
			$fields[':'.$key] = $key;
		}

		$sql = 'INSERT INTO
			`'.$table.'`
			('.implode(', ', $fields).') VALUES ('.implode(', ', array_keys($fields)).')';

		$createdOwnTransaction = false;
		// check if we're already in a transaction, if not, start one, otherwise don't do anything since the root will commit everything after work is done.
		if(!$this->getStorageEngine()->inTransaction()) {
			$this->getStorageEngine()->beginTransaction();
			$createdOwnTransaction = true;
		}

		$query = $this->getStorageEngine()->prepare($sql);
		$query->execute($insertData);

		if($createdOwnTransaction)
			$this->getStorageEngine()->commit();

	}
	public function delete($objectId) {
		$objectData = $this->splitObjectId($objectId);
		if(defined($objectData->Class.'::TABLE'))
			$table = constant($objectData->Class.'::TABLE');
		else throw new Engine_Driver_MySQL_Exception('INVALID_CLASS_FOR_STORAGE_DRIVER', array('Class' => $objectData->Class, 'Id' => $objectData->Id));

		$primaryKey = constant($objectData->Class.'::PRIMARY_FIELD_NAME');

		$sql = 'DELETE FROM
			`'.$table.'`
		WHERE
			`'.$primaryKey.'`='.$this->getStorageEngine()->quote($objectData->Id);
		$this->getStorageEngine()->query($sql);
	}

}

class Engine_Driver_MySQL_Exception extends Engine_Driver_Exception {

}

?>


text:
<?php

namespace Magento\Sales\Controller\Adminhtml\Order;

use Magento\Backend\App\Action;

class ReviewPayment extends \Magento\Sales\Controller\Adminhtml\Order
{
    /**
     * Manage payment state
     *
     * Either denies or approves a payment that is in "review" state
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     */
    public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        try {
            $order = $this->_initOrder();
            if ($order) {
                $action = $this->getRequest()->getParam('action', '');
                switch ($action) {
                    case 'accept':
                        $order->getPayment()->accept();
                        $message = __('The payment has been accepted.');
                        break;
                    case 'deny':
                        $order->getPayment()->deny();
                        $message = __('The payment has been denied.');
                        break;
                    case 'update':
                        $order->getPayment()->update();
                        if ($order->getPayment()->getIsTransactionApproved()) {
                            $message = __('Transaction has been approved.');
                        } else if ($order->getPayment()->getIsTransactionDenied()) {
                            $message = __('Transaction has been voided/declined.');
                        } else {
                            $message = __('There is no update for the transaction.');
                        }
                        break;
                    default:
                        throw new \Exception(sprintf('Action "%s" is not supported.', $action));
                }
                $this->orderRepository->save($order);
                $this->messageManager->addSuccess($message);
            } else {
                $resultRedirect->setPath('sales/*/');
                return $resultRedirect;
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addError(__('We can\'t update the payment right now.'));
            $this->logger->critical($e);
        }
        $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getEntityId()]);
        return $resultRedirect;
    }

    /**
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Magento_Sales::review_payment');
    }
}


text:
package ssh_profiles

import (
	"github.com/codegangsta/cli"
	"github.com/flexiant/concerto/cmd"
)

func SubCommands() []cli.Command {
	return []cli.Command{
		{
			Name:   "list",
			Usage:  "Lists all available SSH profiles.",
			Action: cmd.SSHProfileList,
		},
		{
			Name:   "show",
			Usage:  "Shows information about the SSH profile identified by the given id.",
			Action: cmd.SSHProfileShow,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "id",
					Usage: "SSH profile id",
				},
			},
		},
		{
			Name:   "create",
			Usage:  "Creates a new SSH profile.",
			Action: cmd.SSHProfileCreate,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "name",
					Usage: "Name of the SSH profile",
				},
				cli.StringFlag{
					Name:  "public_key",
					Usage: "Public key of the SSH profile",
				},
				cli.StringFlag{
					Name:  "private_key",
					Usage: "Private key of the SSH profile",
				},
			},
		},
		{
			Name:   "update",
			Usage:  "Updates an existing SSH profile",
			Action: cmd.SSHProfileUpdate,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "id",
					Usage: "SSH profile id",
				},
				cli.StringFlag{
					Name:  "name",
					Usage: "Name of the SSH profile",
				},
				cli.StringFlag{
					Name:  "public_key",
					Usage: "Public key of the SSH profile",
				},
				cli.StringFlag{
					Name:  "private_key",
					Usage: "Private key of the SSH profile",
				},
			},
		},
		{
			Name:   "destroy",
			Usage:  "Destroys an SSH profile",
			Action: cmd.SSHProfileDelete,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "id",
					Usage: "SSH profile id",
				},
			},
		},
	}
}


text:
const assert = require('assert');
const User = require('../../lib/models/user');
const expectedValidation = require('../helper/validation');

describe('user validation', () => {

  it('validates a user', () => {

    const testUser = new User({
      email: 'mofo37',
      password: 'Ilovecows'
    });
    return testUser.validate();
  });

  describe('validation failures', () => {

    it('requires a password', () => {
      const user = new User();
      return user.validate()
        .then(expectedValidation,
        err => {
          const errors = err.errors;
          assert.ok(errors.password && errors.password.kind === 'required');
        });
    });

    it('requires an email', () => {
      const user = new User();
      return user.validate()
        .then(expectedValidation,
        err => {
          const errors = err.errors;
          assert.ok(errors.email && errors.email.kind === 'required');
        });
    });
  });
});


text:
local sum_of_multiples = require('sum-of-multiples')

describe('sum-of-multiples', function()
  it('should sum multiples of a single number', function()
    assert.same(9, sum_of_multiples({ 3 }).to(7))
    assert.same(15, sum_of_multiples({ 5 }).to(12))
  end)

  it('should sum multiples of a list of numbers', function()
    assert.same(33, sum_of_multiples({ 3, 5 }).to(11))
    assert.same(129, sum_of_multiples({ 7, 3 }).to(25))
    assert.same(153, sum_of_multiples({ 7, 3, 8 }).to(25))
  end)

  it('should calculate multiples up to, but not including, the limit', function()
    assert.same(9, sum_of_multiples({ 3 }).to(9))
    assert.same(23, sum_of_multiples({ 3, 5 }).to(10))
  end)

  it('should not include a multiple more than once', function()
    assert.same(35, sum_of_multiples({ 2, 5 }).to(11))
    assert.same(sum_of_multiples({ 2 }).to(11), sum_of_multiples({ 2, 4 }).to(11))
  end)
end)