#! /usr/bin/env python

# Copyright 2013 Andrew Mussey. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from sys import argv, exit
from cassandra_cql import cql_execute

if len(argv) < 4 or (len(argv) == 5 and argv[4] != '-f'):
    print '''
This file is used to inject a full *.cql file into a Cassandra database.
To run this script, all three command line parameters are required:
    
    python cassandra-cql.py hostname port script_file [-f]

        hostname     The address of the server you are connecting to
        port         The port of the cassandra server (usually 9160)
        script_file  The *.cql file to be executed
        -f           Force the script to continue if it hits any error 

An example script file would be:

    USE keyspace;

    CREATE COLUMNFAMILY projects (
      KEY uuid PRIMARY KEY,
      project_id int,
      name text
    );
    '''
else:
    try:
        port = int(argv[2])
    except:
        print '{argv2} is not a valid port.'.format(argv2=argv[2])
        sys.exit(-1)
    cql_execute_result = cql_execute(host=argv[1],
                                     port=port,
                                     filename=argv[3],
                                     force=(len(argv) > 4),
                                     silent=False)
    if not cql_execute_result:
        exit(-1)
