Example #1
0
        /// <inheritdoc/>
        public override void Run(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption || commandLine.Arguments.Length == 0)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            if (commandLine.Arguments.Length == 0)
            {
                Console.Error.WriteLine("*** ERROR: Expected a TARGET argument like: [couchbase://HOST@USER:PASSWORD:BUCKET] or [http(s)://HOST:PORT@USER:PASSWORD:BUCKET]");
                Program.Exit(1);
            }

            // Open a Couchbase bucket for the TARGET.
            //
            //      http(s)://HOST:PORT@USER:PASSWORD:BUCKET
            //      couchbase://HOST@USER:PASSWORD:BUCKET

            var target = commandLine.Arguments[0];
            var error  = $"*** ERROR: [{target}] is not a valid Couchbase target.  Expected: [couchbase://HOST@USER:PASSWORD:BUCKET] or [http(s)://HOST:PORT@USER:PASSWORD:BUCKET]";
            var fields = target.Split(new char[] { '@' }, 2);

            if (fields.Length != 2)
            {
                Console.WriteLine(error);
                Program.Exit(1);
            }

            var uri = fields[0];

            fields = fields[1].Split(':');

            if (fields.Length != 3)
            {
                Console.WriteLine(error);
                Program.Exit(1);
            }

            var username   = fields[0];
            var password   = fields[1];
            var bucketName = fields[2];
            var config     = new CouchbaseSettings();

            config.Servers.Clear();
            config.Servers.Add(new Uri(uri));
            config.Bucket = bucketName;

            using (var bucket = config.OpenBucket(username, password))
            {
                commandLine = commandLine.Shift(1);

                if (commandLine.Arguments.Length != 1)
                {
                    Console.Error.WriteLine("*** ERROR: JSON-FILE argument is required.");
                    Program.Exit(1);
                }

                var fileArg = commandLine.Arguments[0];
                var input   = (Stream)null;

                if (fileArg == "-")
                {
                    // Read from STDIN.

                    input = NeonHelper.OpenStandardInput();
                }
                else
                {
                    input = new FileStream(fileArg, FileMode.Open, FileAccess.ReadWrite);
                }

                var keyPattern    = commandLine.GetOption("--key");
                var firstKeyValue = commandLine.GetOption("--first-key", "1");

                if (!long.TryParse(firstKeyValue, out var firstKey))
                {
                    Console.Error.WriteLine($"*** ERROR: [--firstkey={firstKeyValue}] is not a valid integer.");
                    Program.Exit(1);
                }

                var upsertError = false;

                using (var reader = new StreamReader(input, Encoding.UTF8))
                {
                    var importer = new CouchbaseImporter(
                        message =>
                    {
                        upsertError = true;
                        Console.Error.WriteLine($"*** ERROR: {message}");
                    },
                        bucket, keyPattern, firstKey);

                    foreach (var line in reader.Lines())
                    {
                        if (line.Trim() == string.Empty)
                        {
                            continue;   // Ignore blank lines
                        }

                        var item = JToken.Parse(line);

                        if (item.Type != JTokenType.Object)
                        {
                            upsertError = true;
                            Console.Error.WriteLine($"*** ERROR: [{fileArg}] includes one or more lines with non-JSON document objects.");
                            break;
                        }

                        importer.WriteDocument((JObject)item);
                    }
                }

                if (upsertError)
                {
                    Program.Exit(1);
                }
            }

            Program.Exit(0);
        }
Example #2
0
        /// <inheritdoc/>
        public override void Run(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption || commandLine.Arguments.Length == 0)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            Program.ConnectHive();

            // Process the command arguments.

            var command = commandLine.Arguments[0];

            commandLine = commandLine.Shift(1);

            if (commandLine.Arguments.Length == 0)
            {
                Console.Error.WriteLine("*** ERROR: Expected a TARGET argument like: [couchbase://HOST@USER:PASSWORD:BUCKET] or [http(s)://HOST:PORT@USER:PASSWORD:BUCKET]");
                Program.Exit(1);
            }

            // Open a Couchbase bucket for the TARGET.
            //
            //      http(s)://HOST:PORT@USER:PASSWORD:BUCKET
            //      couchbase://HOST@USER:PASSWORD:BUCKET

            var target = commandLine.Arguments[0];
            var error  = $"*** ERROR: [{target}] is not a valid Couchbase target.  Expected: [couchbase://HOST@USER:PASSWORD:BUCKET] or [http(s)://HOST:PORT@USER:PASSWORD:BUCKET]";
            var fields = target.Split(new char[] { '@' }, 2);

            if (fields.Length != 2)
            {
                Console.WriteLine(error);
                Program.Exit(1);
            }

            var uri = fields[0];

            fields = fields[1].Split(':');

            if (fields.Length != 3)
            {
                Console.WriteLine(error);
                Program.Exit(1);
            }

            var username   = fields[0];
            var password   = fields[1];
            var bucketName = fields[2];
            var config     = new CouchbaseSettings();

            config.Servers.Clear();
            config.Servers.Add(new Uri(uri));
            config.Bucket = bucketName;

            using (var bucket = config.OpenBucket(username, password))
            {
                commandLine = commandLine.Shift(1);

                switch (command)
                {
                case "query":

                    // Get the N1QL query.

                    if (commandLine.Arguments.Length != 1)
                    {
                        Console.Error.WriteLine("*** ERROR: QUERY argument expected.");
                        Program.Exit(1);
                    }

                    var query = commandLine.Arguments[0];

                    if (query == "-")
                    {
                        // Read the query from STDIN.

                        query = NeonHelper.ReadStandardInputText();
                    }
                    else if (query.StartsWith("@"))
                    {
                        // Read the query from the file.

                        query = File.ReadAllText(query.Substring(1));
                    }

                    var queryResults = bucket.Query <JToken>(query);

                    Console.WriteLine(JsonConvert.SerializeObject(queryResults, Formatting.Indented));
                    break;

                case "upsert":

                    if (commandLine.Arguments.Length != 1)
                    {
                        Console.Error.WriteLine("*** ERROR: JSON object argument expected.");
                        Program.Exit(1);
                    }

                    var fileArg = commandLine.Arguments[0];
                    var input   = (Stream)null;

                    if (fileArg == "-")
                    {
                        // Read the object from STDIN.

                        input = Console.OpenStandardInput();
                    }
                    else
                    {
                        input = new FileStream(fileArg, FileMode.Open, FileAccess.ReadWrite);
                    }

                    var keyPattern = commandLine.GetOption("--key");

                    var firstKeyValue = commandLine.GetOption("--first-key", "1");

                    if (!long.TryParse(firstKeyValue, out var firstKey))
                    {
                        Console.Error.WriteLine($"*** ERROR: [--firstkey={firstKeyValue}] is not a valid integer.");
                        Program.Exit(1);
                    }

                    var upsertError = false;

                    using (var reader = new StreamReader(input, Encoding.UTF8))
                    {
                        var importer = new CouchbaseImporter(
                            message =>
                        {
                            upsertError = true;
                            Console.Error.WriteLine($"*** ERROR: {message}");
                        },
                            bucket, keyPattern, firstKey);

                        foreach (var line in reader.Lines())
                        {
                            if (line.Trim() == string.Empty)
                            {
                                continue;       // Ignore blank lines
                            }

                            var item = JToken.Parse(line);

                            if (item.Type != JTokenType.Object)
                            {
                                upsertError = true;
                                Console.Error.WriteLine($"*** ERROR: [{fileArg}] includes one or more lines with non-document objects.");
                                break;
                            }

                            importer.WriteDocument((JObject)item);
                        }
                    }

                    if (upsertError)
                    {
                        Program.Exit(1);
                    }
                    break;

                default:

                    Console.Error.WriteLine($"*** ERROR: Unknown command: [{command}]");
                    Program.Exit(1);
                    break;
                }
            }
        }
Example #3
0
        /// <inheritdoc/>
        public void Run(ModuleContext context)
        {
            var hive       = HiveHelper.Hive;
            var nodeGroups = hive.Definition.GetHostGroups(excludeAllGroup: true);

            //-----------------------------------------------------------------
            // Parse the module arguments.

            if (!context.ValidateArguments(context.Arguments, validModuleArgs))
            {
                context.Failed = true;
                return;
            }

            var couchbaseArgs = CouchbaseArgs.Parse(context);

            if (couchbaseArgs == null)
            {
                return;
            }

            var format = context.ParseEnum <CouchbaseFileFormat>("format");

            if (!format.HasValue)
            {
                format = default(CouchbaseFileFormat);
            }

            var source = context.ParseString("source");

            if (string.IsNullOrEmpty(source))
            {
                context.WriteErrorLine("[source] module parameter is required.");
                return;
            }

            if (!File.Exists(source))
            {
                context.WriteErrorLine($"File [{source}] does not exist.");
                return;
            }

            var keyPattern = context.ParseString("key");
            var firstKey   = context.ParseLong("first_key") ?? 1;

            if (context.HasErrors)
            {
                return;
            }

            //-----------------------------------------------------------------
            // Import the data.

            using (var bucket = couchbaseArgs.Settings.OpenBucket(couchbaseArgs.Credentials))
            {
                var importer = new CouchbaseImporter(message => context.WriteErrorLine(message), bucket, keyPattern, firstKey, context.CheckMode);

                switch (format.Value)
                {
                case CouchbaseFileFormat.JsonArray:

                    // $todo(jeff.lill):
                    //
                    // Would be nice not to read this whole thing in memory and then
                    // effectibely duplicating it in memory again when parsing.

                    var jToken = JToken.Parse(File.ReadAllText(source));

                    if (jToken.Type != JTokenType.Array)
                    {
                        context.WriteErrorLine($"[{source}] is not a JSON array of documents.");
                        return;
                    }

                    var jArray = (JArray)jToken;

                    foreach (var item in jArray)
                    {
                        if (item.Type != JTokenType.Object)
                        {
                            context.WriteErrorLine($"[{source}] includes one or more non-document objects in the array.");
                            return;
                        }

                        importer.WriteDocument((JObject)item);
                    }
                    break;

                case CouchbaseFileFormat.JsonLines:

                    using (var reader = new StreamReader(source, Encoding.UTF8))
                    {
                        foreach (var line in reader.Lines())
                        {
                            if (line.Trim() == string.Empty)
                            {
                                continue;       // Ignore blank lines
                            }

                            var item = JToken.Parse(line);

                            if (item.Type != JTokenType.Object)
                            {
                                context.WriteErrorLine($"[{source}] includes one or more lines with non-document objects.");
                                return;
                            }

                            importer.WriteDocument((JObject)item);
                        }
                    }
                    break;

                default:

                    throw new NotImplementedException($"Format [{format}] is not implemented.");
                }

                context.Changed = importer.DocumentCount > 0;

                if (context.CheckMode)
                {
                    context.WriteLine(AnsibleVerbosity.Info, $"[{importer.DocumentCount}] documents will be added when CHECK-MODE is disabled.");
                }
                else
                {
                    context.WriteLine(AnsibleVerbosity.Info, $"[{importer.DocumentCount}] documents were imported.");
                }
            }
        }