Beispiel #1
0
        public AvailableOptions(List <string> actionArgs, out bool successful)
        {
            if (actionArgs.Count > 1)
            {
                CLIInterface.logError($"{description.name} action takes one optional argument: resource type. Usage:");
                logUsage();
                successful = false;
                return;
            }
            prefix = null;
            if (actionArgs.Count == 1)
            {
                string resourceTypeStr = actionArgs[1];
                try
                {
                    resourceType = ParseUtils.parseResourceType(resourceTypeStr);
                }
                catch (ParseUtils.ArgumentProcessException ex)
                {
                    CLIInterface.logError($"Error: {ex.Message}");
                    successful = false;
                    return;
                }
            }


            successful = true;
            return;
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            new CLIParser(args.ToList())
            .withInit(opts => {
                PackageManager.initDirectories();
            })
            .withList(opts => {
                PackageManager.listResources(opts.resourceType);
            })
            .withAdd(opts => {
                PackageManager.addPackage(opts.prefix, opts.resourceIdentifier);
            })
            .withRemove(opts => {
                PackageManager.removePackage(opts.resourceIdentifier);
            })
            .withAvailable(opts => {
                PackageManager.listAvailableResources(opts.prefix, opts.resourceType);
            })
            .withDependencies(opts => {
                PackageManager.listDependencies(opts.prefix, opts.resourceIdentifier);
            })
            .withPublish(opts => {
                PackageManager.publishResource(opts.prefix, opts.resourceIdentifier, opts.deps);
            })
            .withRemote(opts => {
                PackageManager.setRemoteServerAddress(opts.prefix, opts.webAddress);
            });

            // To return console color to normal if necessary and to add some space at the bottom of the output
            CLIInterface.logLine("");
        }
Beispiel #3
0
        public AddOptions(List <string> actionArgs, out bool successful)
        {
            string resourceStr;

            if (!(actionArgs.Count == 1 || actionArgs.Count == 2))
            {
                CLIInterface.logError($"{description.name} action takes two arguments: alias(optional) ,resource name. Usage:");
                logUsage();
                successful = false;
                return;
            }
            else if (actionArgs.Count == 1)
            {
                prefix      = null;
                resourceStr = actionArgs[0];
            }
            else
            {
                prefix      = actionArgs[0];
                resourceStr = actionArgs[1];
            }
            try
            {
                resourceIdentifier = ParseUtils.generateResourceIdentifier(resourceStr);
            }
            catch (ParseUtils.ArgumentProcessException ex)
            {
                CLIInterface.logError($"Error: {ex.Message}");
                successful = false;
                return;
            }

            successful = true;
        }
Beispiel #4
0
        private static bool tryCreateDirIfNonExistent(
            string dirName,
            bool logExisting,
            bool logCreated)
        {
            try
            {
                if (Directory.Exists(dirName))
                {
                    if (logExisting)
                    {
                        CLIInterface.logLine($"Directory \"{dirName}\" already exists");
                    }
                    return(true);
                }
                else
                {
                    Directory.CreateDirectory(dirName);

                    if (logCreated)
                    {
                        CLIInterface.logLine($"Directory \"{dirName}\" created");
                    }
                    return(true);
                }
            }
            catch (System.Exception)
            {
                throw new FSOpsException($"Failed to create directory \"{dirName}\"");
            }
        }
Beispiel #5
0
        public DependenciesOptions(List <string> actionArgs, out bool successful)
        {
            if (actionArgs.Count != 1)
            {
                CLIInterface.logError($"{description.name} action takes one argument: resource name. Usage:");
                logUsage();
                successful = false;
                return;
            }

            string resourceStr = actionArgs[0];

            try
            {
                resourceIdentifier = ParseUtils.generateResourceIdentifier(resourceStr);
            }
            catch (ParseUtils.ArgumentProcessException ex)
            {
                CLIInterface.logError($"Error: {ex.Message}");
                successful = false;
                return;
            }

            successful = true;
        }
        public static void listResources(ResourceType?listType)
        {
            try
            {
                if (!FSOps.hasNecessaryDirs())
                {
                    CLIInterface.logError($"Missing some or all resource directories in current directory. Try running {ConstStrings.APPLICATION_ALIAS} init?");
                    return;
                }

                CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                    { ConstStrings.HeaderStringType, 6 },
                    { ConstStrings.HeaderStringNameOfResource, 21 },
                    { ConstStrings.HeaderStringVersion, 16 },
                    { ConstStrings.HeaderStringFileSize, 11 },
                };

                List <ResourceType> resourcesToList = listType.HasValue ?
                                                      new List <ResourceType> {
                    listType.Value
                } :
                new List <ResourceType> {
                    ResourceType.Code, ResourceType.Data, ResourceType.Model
                };

                foreach (ResourceType resourceType in resourcesToList)
                {
                    foreach (string resourceName in FSOps.resourceNames(resourceType))
                    {
                        string version;
                        if (FSOps.resourceVersionFileExists(resourceType, resourceName))
                        {
                            version = FSOps.getResourceVersion(resourceType, resourceName);
                        }
                        else
                        {
                            version = "Unknown version";
                        }

                        long fileSize = FSOps.getResourceSize(resourceType, resourceName);

                        table.addRow(
                            doFormat(resourceType.ToString()),
                            doFormat(resourceName),
                            doFormat(version),
                            doFormat(bytesToString(fileSize))
                            );
                    }
                }

                CLIInterface.logTable(table);
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
        }
 public static void initDirectories()
 {
     try
     {
         FSOps.createCodeDataModelDirs(logExisting: true, logCreated: true);
     }
     catch (FSOps.FSOpsException ex)
     {
         CLIInterface.logError($"File System Error: " + ex.Message);
     }
 }
Beispiel #8
0
        public PublishOptions(List <string> actionArgs, out bool successful)
        {
            if (actionArgs.Count < 1)
            {
                CLIInterface.logError($"{description.name} action takes one required argument, resource name, with an option --deps to add dependencies. Usage:");
                logUsage();
                successful = false;
                return;
            }
            prefix = "-r";
            string resourceStr = actionArgs[0];

            try
            {
                resourceIdentifier = ParseUtils.generateResourceIdentifier(resourceStr);
            }
            catch (ParseUtils.ArgumentProcessException ex)
            {
                CLIInterface.logError($"Error: {ex.Message}");
                successful = false;
                return;
            }

            if (actionArgs.Count >= 2)
            {
                string shouldBeDepsOption = actionArgs[1];
                if (shouldBeDepsOption != "--deps")
                {
                    CLIInterface.logError($"Error: {description.name} has one possible option, --deps. Usage:");
                    logUsage();
                    successful = false;
                    return;
                }

                IEnumerable <string> depStrings = actionArgs.Skip(2);

                foreach (string depString in depStrings)
                {
                    try
                    {
                        deps.Add(ParseUtils.generateResourceIdentifier(depString));
                    }
                    catch (ParseUtils.ArgumentProcessException ex)
                    {
                        CLIInterface.logError($"Error: {ex.Message}");
                        successful = false;
                        return;
                    }
                }
            }

            successful = true;
        }
Beispiel #9
0
        public InitOptions(List <string> actionArgs, out bool successful)
        {
            if (actionArgs.Count != 0)
            {
                CLIInterface.logError($"{description.name} action takes no arguments. Usage:");
                logUsage();
                successful = false;
                return;
            }

            successful = true;
        }
Beispiel #10
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} programName.py",
                "Removes a code resource named programName.py from the code folder in the current directory"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #11
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name}",
                $"Initialize the resource directories that {ConstStrings.APPLICATION_ALIAS} uses in the current directory"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
        public static void removePackage(ResourceIdentifier resourceDescription)
        {
            try
            {
                string       resourceName = resourceDescription.resourceName;
                ResourceType resourceType = resourceDescription.resourceType;
                FSOps.createCodeDataModelDirs();

                CLIInterface.logLine($"Removing {resourceType.ToString().ToLower()} resource \"{resourceName}\"");
                if (!FSOps.resourceFileExists(resourceType, resourceName))
                {
                    CLIInterface.logError($"{resourceType} resource \"{resourceName}\" does not exist");
                    return;
                }

                if (resourceDescription.version != null)
                {
                    if (FSOps.resourceVersionFileExists(resourceType, resourceName))
                    {
                        string localVersion = FSOps.getResourceVersion(resourceType, resourceName);
                        if (localVersion != resourceDescription.version)
                        {
                            bool removeAnyways = CLIInterface.askYesOrNo($"Present version is {localVersion}, not {resourceDescription.version}. Remove anyways?");
                            if (!removeAnyways)
                            {
                                CLIInterface.logLine("Aborting resource removal.");
                                return;
                            }
                        }
                    }
                    else
                    {
                        bool removeAnyways = CLIInterface.askYesOrNo($"Local file {resourceName} has unknown version. Remove anyways?");
                        if (!removeAnyways)
                        {
                            CLIInterface.logLine("Aborting resource removal.");
                            return;
                        }
                    }
                }

                FSOps.removeResourceFilesIfPresent(resourceType, resourceName);
                CLIInterface.logLine("Resource removed");
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
        }
Beispiel #13
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} programName.py",
                "Lists dependencies of a code resource named programName.py. If the resource is present locally," +
                " this will list the dependencies of the local version. Otherwise, it'll list the dependencies of " +
                "the latest version available on the server."
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #14
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} -m modelName.pmml",
                "Deploy local model resource called modelNamme.pmml to zementismodeler"
                );
            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} -s modelName.pmml",
                "Deploy local model resource called modelNamme.pmml to zementisserver"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
        private static void downloadPackage(string prefix, ResourceType resourceType, string resourceName, string version)
        {
            try
            {
                // check that resource is on server
                var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType);
                if (!availableResources.resourceDescriptions.ContainsKey(resourceName))
                {
                    CLIInterface.logError($"Could not find {resourceType.ToString()} resource with name {resourceName} on server");
                    return;
                }

                // check that resource on server has specified version
                var versionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName);
                if (!versionInfo.versions.ContainsKey(version))
                {
                    CLIInterface.logError(
                        $"Could not find version {version} on server. These are the version(s) available: {string.Join(", ", versionInfo.versions.Keys.ToList())}"
                        );
                    return;
                }

                using (FileStream resourceFileStream = FSOps.createResourceFile(resourceType, resourceName))
                    using (StreamWriter versionFileStream = FSOps.createOrOverwriteResourceVersionFile(resourceType, resourceName))
                    {
                        Task resourceFileTask = NetworkUtils.downloadResource(prefix, resourceType, resourceName, version, resourceFileStream);

                        Task resourceVersionTask = Task.Factory.StartNew(() => {
                            versionFileStream.WriteLine(version);
                        });

                        Task.WaitAll(resourceFileTask, resourceVersionTask);
                    }
                CLIInterface.logLine($"{resourceType} resource {resourceName} added");
            }
            catch (NetworkUtils.NetworkUtilsException ex)
            {
                CLIInterface.logError($"Network Error: {ex.Message}");
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
        }
Beispiel #16
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} programName.py",
                "Adds the latest version a code resource named programName.py to the code folder in the current directory"
                );

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} [email protected]",
                "Adds version 1.2.3 of a model resource named model1.pmml to the model folder in the current directory"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #17
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} model",
                "Lists all model resources available on repository server"
                );

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name}",
                "Lists all resources available on server"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #18
0
        private void listActions()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Action Name", 0 },
                { "Action Description", 0 },
            };

            foreach (KeyValuePair <string, OptionDescription> descriptionKVPair in optionDescriptions)
            {
                string            actionName  = descriptionKVPair.Key;
                OptionDescription description = descriptionKVPair.Value;

                table.addRow(
                    actionName,
                    description.summary
                    );
            }

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #19
0
 public RemoteOptions(List <string> actionArgs, out bool successful)
 {
     if (actionArgs.Count == 1)
     {
         prefix     = null;
         webAddress = actionArgs[0];
     }
     else if (actionArgs.Count == 2)
     {
         prefix     = actionArgs[0];
         webAddress = actionArgs[1];
     }
     else
     {
         CLIInterface.logError($"{description.name} action takes one or two argument, alias and web address, where alias is optional argument Usage:");
         logUsage();
         successful = false;
         return;
     }
     successful = true;
 }
Beispiel #20
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} \"http://serveraddress.com\"",
                $"Set the {ConstStrings.APPLICATION_ALIAS} remote server address for the repository server with this directory to http://serveraddress.com/nyokaapi"
                );
            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} -m \"http://serveraddress.com\"",
                $"Set the {ConstStrings.APPLICATION_ALIAS} remote server address for zementismodeler http://serveraddress.com/nyokaapi"
                );
            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} -s \"http://serveraddress.com\"",
                $"Set the {ConstStrings.APPLICATION_ALIAS} remote server address for zementisserver http://serveraddress.com/nyokaapi"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #21
0
        private void logUsage()
        {
            CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                { "Example Action", 0 },
                { "Explanation", 0 },
            };

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} programName.py",
                "Publish local code resource called programName.py. Since version is not provided, version number defaults to 1.0."
                );

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} [email protected]",
                "Publish local data resource called dataFile.csv with version number 10.2.2"
                );

            table.addRow(
                $"{ConstStrings.APPLICATION_ALIAS} {description.name} [email protected] --deps [email protected] [email protected]",
                "Publish local code resource program1.py at version 2.2.3, with the dependencies program2.py, version 1.2.3 and program2, version 9.1"
                );

            CLIInterface.logTable(table, visibleLines: false);
        }
Beispiel #22
0
        public DeployOptions(List <string> actionArgs, out bool successful)
        {
            if (actionArgs.Count < 1)
            {
                CLIInterface.logError($"{description.name} action takes two required argument, server alias -r or -m and modelName.pmml file Usage:");
                logUsage();
                successful = false;
                return;
            }
            prefix = actionArgs[0];
            string resourceStr = actionArgs[1];

            try
            {
                resourceIdentifier = ParseUtils.generateResourceIdentifier(resourceStr);
            }
            catch (ParseUtils.ArgumentProcessException ex)
            {
                CLIInterface.logError($"Error:{ex.Message}");
                successful = false;
                return;
            }
            successful = true;
        }
        public static async System.Threading.Tasks.Task downloadResource(
            string prefix,
            ResourceType resourceType,
            string resourceName,
            string version,
            System.IO.Stream resultStream)
        {
            long totalFileSize;

            try
            {
                totalFileSize = getResourceVersions(prefix, resourceType, resourceName).versions[version].byteCount;
            }
            catch (System.Exception)
            {
                throw new NetworkUtilsException(
                          $"Could not find {resourceType.ToString().ToLower()} resource {resourceName} at version {version} on server"
                          );
            }

            int bufferSize = System.Math.Max(1, System.Math.Min((int)(totalFileSize / 100.0), 10000000));

            string url = resourceFileUrl(prefix, resourceType, resourceName, version);

            try
            {
                using (var contentStream = await client.GetStreamAsync(url))
                {
                    byte[] buffer = new byte[bufferSize];

                    bool doneReadingContent = false;

                    long totalBytesRead = 0;

                    // @TODO add cancellation?
                    do
                    {
                        int bytesRead = contentStream.Read(buffer, 0, buffer.Length);

                        if (bytesRead == 0)
                        {
                            doneReadingContent = true;
                        }

                        totalBytesRead += bytesRead;

                        await resultStream.WriteAsync(buffer, 0, bytesRead);

                        int percentDone = totalFileSize == 0 ? 100 : (int)(100 * (double)totalBytesRead / (double)totalFileSize);

                        CLIInterface.writeBottomLineOverwriteExisting($"Download {resourceName}: {percentDone}%");
                    }while(!doneReadingContent);
                    CLIInterface.logLine("");
                }
            }
            catch (System.Exception)
            {
                throw new NetworkUtilsException(
                          $"Unable to get file for {resourceType} resource {resourceName}"
                          );
            }
        }
        public static void publishResource(string prefix, ResourceIdentifier resourceDescription, IEnumerable <ResourceIdentifier> deps)
        {
            try
            {
                PublishDepsInfoContainer publishDepsInfo = new PublishDepsInfoContainer();

                foreach (ResourceIdentifier depDescription in deps)
                {
                    if (depDescription.version == null)
                    {
                        CLIInterface.logError(
                            "The versions of dependencies must be supplied. For example, " +
                            "\"dependency.csv\" does not include version, \"[email protected]\" does."
                            );
                        return;
                    }

                    var publishDepDescription = new PublishDepsInfoContainer.PublishDepDescription(depDescription.version);
                    if (depDescription.resourceType == ResourceType.Code)
                    {
                        publishDepsInfo.codeDeps[depDescription.resourceName] = publishDepDescription;
                    }
                    else if (depDescription.resourceType == ResourceType.Data)
                    {
                        publishDepsInfo.dataDeps[depDescription.resourceName] = publishDepDescription;
                    }
                    else if (depDescription.resourceType == ResourceType.Model)
                    {
                        publishDepsInfo.modelDeps[depDescription.resourceName] = publishDepDescription;
                    }
                }

                string       resourceName   = resourceDescription.resourceName;
                ResourceType resourceType   = resourceDescription.resourceType;
                string       publishVersion = resourceDescription.version;

                // check that user has provided version to publish file as
                if (publishVersion == null)
                {
                    publishVersion = "1.0";
                    CLIInterface.logLine($"Using default version {publishVersion}");
                }

                if (!FSOps.hasNecessaryDirs())
                {
                    CLIInterface.logError($"Could not find nyoka resource folders in current directory. Try running {ConstStrings.APPLICATION_ALIAS} {CLIParserNS.InitOptions.description.name}?");
                    return;
                }

                // If a file to publish with the given name can't be found
                if (!FSOps.resourceFileExists(resourceType, resourceName))
                {
                    CLIInterface.logError($"Resource with name {resourceName} not found.");
                    return;
                }

                var resourcesOnServer = NetworkUtils.getAvailableResources(prefix, resourceType);

                // If this resource already exists on server
                if (resourcesOnServer.resourceDescriptions.ContainsKey(resourceName))
                {
                    ResourceVersionsInfoContainer serverVersionsInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName);

                    // If this resource exists with the same version on server
                    if (serverVersionsInfo.versions.ContainsKey(publishVersion))
                    {
                        bool continueAnyways = CLIInterface.askYesOrNo(
                            $"Version {publishVersion} of {resourceType.ToString()} resource " +
                            $"{resourceName} already exists on server. Overwrite?"
                            );

                        if (!continueAnyways)
                        {
                            CLIInterface.logLine("Aborting publish.");
                            return;
                        }
                        else
                        {
                            CLIInterface.logLine("Overwriting resource on server.");
                        }
                    }
                }

                CLIInterface.logLine("Opening file.");
                FileStream fileStream = FSOps.readResourceFile(resourceType, resourceName);

                CLIInterface.logLine("Uploading file.");
                NetworkUtils.publishResource(
                    prefix,
                    fileStream,
                    resourceType,
                    resourceName,
                    publishVersion,
                    publishDepsInfo
                    );

                // create or overwrite version file locally for this resource to be the publishVersion
                using (var versionFileStream = FSOps.createOrOverwriteResourceVersionFile(resourceType, resourceName))
                {
                    versionFileStream.WriteLine(publishVersion);
                }
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
            catch (NetworkUtils.NetworkUtilsException ex)
            {
                CLIInterface.logError($"Network Error: {ex.Message}");
            }
        }
        public static void listAvailableResources(string prefix, ResourceType?listType)
        {
            try
            {
                List <ResourceType> resourcesToList = listType.HasValue ?
                                                      new List <ResourceType> {
                    listType.Value
                } :
                new List <ResourceType> {
                    ResourceType.Code, ResourceType.Data, ResourceType.Model
                };

                CLIInterface.PrintTable printTable = new CLIInterface.PrintTable {
                    { ConstStrings.HeaderStringType, 6 },
                    { ConstStrings.HeaderStringNameOfResource, 21 },
                    { ConstStrings.HeaderStringLatestVersion, 16 },
                    { ConstStrings.HeaderStringLocalVersion, 2 },
                    { ConstStrings.HeaderStringFileSize, 11 },
                };

                foreach (ResourceType resourceType in resourcesToList)
                {
                    var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType);

                    foreach (string resourceName in availableResources.resourceDescriptions.Keys.OrderBy(k => k))
                    {
                        string localVersionStr;
                        bool   resourceExistsLocally = FSOps.resourceFileExists(resourceType, resourceName);
                        if (resourceExistsLocally)
                        {
                            if (FSOps.resourceVersionFileExists(resourceType, resourceName))
                            {
                                localVersionStr = FSOps.getResourceVersion(resourceType, resourceName);
                            }
                            else
                            {
                                localVersionStr = "Unknown version";
                            }
                        }
                        else
                        {
                            localVersionStr = "Not present";
                        }

                        printTable.addRow(
                            doFormat(resourceType.ToString()),
                            doFormat(resourceName),
                            doFormat(availableResources.resourceDescriptions[resourceName].versionStr),
                            doFormat(localVersionStr),
                            doFormat(bytesToString(availableResources.resourceDescriptions[resourceName].byteCount))
                            );
                    }
                }

                CLIInterface.logTable(printTable);
            }
            catch (NetworkUtils.NetworkUtilsException ex)
            {
                CLIInterface.logError($"Network Error: {ex.Message}");
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
        }
        public static void listDependencies(string prefix, ResourceIdentifier resourceDescription)
        {
            string       resourceName = resourceDescription.resourceName;
            ResourceType resourceType = resourceDescription.resourceType;
            string       version      = resourceDescription.version;

            try
            {
                // check if this resource exists on server
                var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType);
                if (!availableResources.resourceDescriptions.ContainsKey(resourceName))
                {
                    CLIInterface.logError($"{resourceType.ToString()} resource {resourceName} could not be found on server");
                    return;
                }

                if (version == null)
                {
                    if (
                        FSOps.resourceFileExists(resourceType, resourceName) &&
                        FSOps.resourceVersionFileExists(resourceType, resourceName)
                        )
                    {
                        version = FSOps.getResourceVersion(resourceType, resourceName);
                    }
                    else
                    {
                        var versionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName);
                        version = versionInfo.latestVersion;
                    }
                }
                // check if user-specified version exists on the server at the given version
                else
                {
                    var versionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName);
                    if (!versionInfo.versions.ContainsKey(version))
                    {
                        CLIInterface.logError("Server does not report having a version \"{version}\" available for {resourceName}");
                    }
                }

                CLIInterface.logLine($"Showing dependencies of {resourceName}, version {version}");

                ResourceDependencyInfoContainer deps = NetworkUtils.getResourceDependencies(prefix, resourceType, resourceName, version);

                CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                    { "Resource Type", 13 },
                    { "Dependency Type", 15 },
                    { "Name of Resource", 15 },
                    { "Resource Version", 15 },
                    { "File Size", 10 },
                };

                var availableResourcesInfo = new Dictionary <ResourceType, AvailableResourcesInfoContainer> {
                    { ResourceType.Code, NetworkUtils.getAvailableResources(prefix, ResourceType.Code) },
                    { ResourceType.Data, NetworkUtils.getAvailableResources(prefix, ResourceType.Data) },
                    { ResourceType.Model, NetworkUtils.getAvailableResources(prefix, ResourceType.Model) },
                };

                var showDepDict = new Dictionary <ResourceType, Dictionary <string, ResourceDependencyInfoContainer.DependencyDescription> >()
                {
                    { ResourceType.Code, deps.codeDeps },
                    { ResourceType.Data, deps.dataDeps },
                    { ResourceType.Model, deps.modelDeps },
                };

                foreach (var(dependenciesType, descriptions) in showDepDict.Select(x => (x.Key, x.Value)))
                {
                    foreach (var(dependencyName, dependencyDescription) in descriptions.Select(x => (x.Key, x.Value)))
                    {
                        table.addRow(
                            dependenciesType.ToString(),
                            dependencyDescription.isDirectDependency ? "direct" : "indirect",
                            dependencyName,
                            dependencyDescription.versionStr,
                            bytesToString(availableResourcesInfo[dependenciesType].resourceDescriptions[dependencyName].byteCount)
                            );
                    }
                }

                CLIInterface.logTable(table);
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
            catch (NetworkUtils.NetworkUtilsException ex)
            {
                CLIInterface.logError($"Network Error: {ex.Message}");
            }
        }
Beispiel #27
0
        public CLIParser(List <string> args)
        {
            if (args.Count == 0)
            {
                CLIInterface.logError($"{ConstStrings.APPLICATION_ALIAS} must be called with an action name. Available actions:");
                listActions();
                return;
            }

            string actionName = args[0];

            // arguments without action name
            List <string> actionArgs = args.Skip(1).ToList();

            if (!optionDescriptions.ContainsKey(actionName))
            {
                CLIInterface.logError($"Invalid action name {actionName}. Available actions:");
                listActions();
                return;
            }

            bool optionParseSuccessful;

            if (actionName == InitOptions.description.name)
            {
                var opts = new InitOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedInitOptions = opts;
                }
            }
            if (actionName == AddOptions.description.name)
            {
                var opts = new AddOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedAddOptions = opts;
                }
            }
            if (actionName == RemoveOptions.description.name)
            {
                var opts = new RemoveOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedRemoveOptions = opts;
                }
            }
            if (actionName == ListOptions.description.name)
            {
                var opts = new ListOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedListOptions = opts;
                }
            }
            if (actionName == AvailableOptions.description.name)
            {
                var opts = new AvailableOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedAvailableOptions = opts;
                }
            }
            if (actionName == DependenciesOptions.description.name)
            {
                var opts = new DependenciesOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedDependenciesOptions = opts;
                }
            }
            if (actionName == PublishOptions.description.name)
            {
                var opts = new PublishOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedPublishOptions = opts;
                }
            }
            if (actionName == DeployOptions.description.name)
            {
                var opts = new DeployOptions(actionArgs, out optionParseSuccessful);
                if (optionParseSuccessful)
                {
                    parsedDeployOptions = opts;
                }
            }
            if (actionName == RemoteOptions.description.name)
            {
                var opts = new RemoteOptions(actionArgs, out optionParseSuccessful);

                if (optionParseSuccessful)
                {
                    parsedRemoteOptions = opts;
                }
            }
        }
        public static void addPackage(string prefix, ResourceIdentifier resourceDescription)
        {
            try
            {
                ResourceType resourceType = resourceDescription.resourceType;
                string       resourceName = resourceDescription.resourceName;

                // check if the resource is available from the server
                var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType);
                if (!availableResources.resourceDescriptions.ContainsKey(resourceName))
                {
                    CLIInterface.logError($"No resource called {resourceName} is available from the server.");
                    return;
                }

                string version           = resourceDescription.version; // possible null
                var    serverVersionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName);

                if (version == null)
                {
                    version = serverVersionInfo.latestVersion;
                }
                else
                {
                    // check that the requested version is available from the server
                    if (!serverVersionInfo.versions.ContainsKey(version))
                    {
                        CLIInterface.logError(
                            $"There is no version {version} available of resource {resourceName}. " +
                            $"These are the version(s) available: {string.Join(", ", serverVersionInfo.versions.Keys.ToList())}"
                            );
                        return;
                    }
                }

                // check if nyoka directories exists
                if (!FSOps.hasNecessaryDirs())
                {
                    bool createDirs = CLIInterface.askYesOrNo(
                        "Resource directories are not present in this directory. Create them now?"
                        );

                    if (createDirs)
                    {
                        FSOps.createCodeDataModelDirs(logCreated: true);
                    }
                    else
                    {
                        CLIInterface.logLine("Package add aborted");
                        return;
                    }
                }

                // check if the resource is already present
                if (FSOps.resourceFileExists(resourceType, resourceName))
                {
                    bool continueAnyways = CLIInterface.askYesOrNo(
                        $"{resourceType.ToString()} resource {resourceName} is already present. Delete and replace ?"
                        );

                    if (continueAnyways)
                    {
                        FSOps.removeResourceFilesIfPresent(resourceType, resourceName);
                    }
                    else
                    {
                        CLIInterface.logLine("Aborting resource add.");
                        return;
                    }
                }

                ResourceDependencyInfoContainer dependencies = NetworkUtils.getResourceDependencies(prefix, resourceType, resourceName, version);

                var depDescriptions = new Dictionary <ResourceType, Dictionary <string, ResourceDependencyInfoContainer.DependencyDescription> > {
                    { ResourceType.Code, dependencies.codeDeps },
                    { ResourceType.Data, dependencies.dataDeps },
                    { ResourceType.Model, dependencies.modelDeps },
                };

                bool downloadDependencies = false;

                // if there package has any dependencies
                if (depDescriptions.Any(kvPair => kvPair.Value.Count != 0))
                {
                    CLIInterface.PrintTable table = new CLIInterface.PrintTable {
                        { "Resource Type", 13 },
                        { "Dependency Type", 15 },
                        { "Name of Resource", 16 },
                        { "Resource Version", 16 },
                        { "File Size", 9 },
                    };

                    foreach (var(depResourceType, deps) in depDescriptions.Select(x => (x.Key, x.Value)))
                    {
                        foreach (var(depName, depDescription) in deps.Select(x => (x.Key, x.Value)))
                        {
                            table.addRow(
                                depResourceType.ToString(),
                                depDescription.isDirectDependency ? "direct" : "indirect",
                                depName,
                                depDescription.versionStr,
                                bytesToString(depDescription.byteCount)
                                );
                        }
                    }

                    CLIInterface.logLine($"Resource {resourceName} has these dependencies:");
                    CLIInterface.logTable(table);
                    downloadDependencies = CLIInterface.askYesOrNo("Download these dependencies?");

                    if (downloadDependencies)
                    {
                        CLIInterface.logLine("Downloading dependencies");
                    }
                    else
                    {
                        CLIInterface.logLine("Skipping downloading dependencies.");
                    }
                }

                if (downloadDependencies)
                {
                    var depsToDownload = new List <(ResourceType, string, string)>();
                    foreach (var(depResourceType, deps) in depDescriptions.Select(x => (x.Key, x.Value)))
                    {
                        foreach (var(depName, depDescription) in deps.Select(x => (x.Key, x.Value)))
                        {
                            bool continueWithDownload = true;

                            // Ask user whether to overwrite file if a file with this name exists locally already
                            if (FSOps.resourceFileExists(depResourceType, depName))
                            {
                                if (FSOps.resourceVersionFileExists(depResourceType, depName))
                                {
                                    string depLocalVersion = FSOps.getResourceVersion(depResourceType, depName);

                                    if (depDescription.versionStr == depLocalVersion)
                                    {
                                        continueWithDownload = CLIInterface.askYesOrNo(
                                            $"Dependency {depName} file exists locally at the required version " +
                                            $"({depDescription.versionStr}). Overwrite this file?"
                                            );
                                    }
                                    else
                                    {
                                        continueWithDownload = CLIInterface.askYesOrNo(
                                            $"Dependency {depName} file exists locally at version {depLocalVersion}" +
                                            $" (depency required version is {depDescription.versionStr}). Overwrite this file?"
                                            );
                                    }
                                }
                                else
                                {
                                    continueWithDownload = CLIInterface.askYesOrNo(
                                        $"Dependency {depName} file exists locally at an unknown version. Overwrite this file?"
                                        );
                                }
                            }

                            if (continueWithDownload)
                            {
                                depsToDownload.Add((depResourceType, depName, depDescription.versionStr));
                            }
                            else
                            {
                                CLIInterface.logWarning($"Skipping download of dependency {depName}.");
                            }
                        }
                    }

                    foreach (var(depResourceType, depName, depVersion) in depsToDownload)
                    {
                        downloadPackage(prefix, depResourceType, depName, depVersion);
                    }
                }

                downloadPackage(prefix, resourceType, resourceName, version);
            }
            catch (FSOps.FSOpsException ex)
            {
                CLIInterface.logError($"File System Error: " + ex.Message);
            }
            catch (NetworkUtils.NetworkUtilsException ex)
            {
                CLIInterface.logError($"Network Error: {ex.Message}");
            }
        }