From 81f8f1f71737e77b2cd0616c1cb5d14f6bddc2f0 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Thu, 18 Jan 2018 16:36:00 -0600 Subject: [PATCH] Attempting to add a repository class --- src/spm/spm.moon | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/spm/spm.moon b/src/spm/spm.moon index 8dd7ed0..c549e24 100644 --- a/src/spm/spm.moon +++ b/src/spm/spm.moon @@ -22,6 +22,11 @@ defaultRepoLocationConfig = "{URL_BASE}/repos.cfg" localInstalledPackagesFile = "/etc/spdata.svd" localConfigFile = "/etc/spm.cfg" +class Repository + new: (name, src) => + @name = name + @src = src + args, options = shell.parse(...) printUsage = -> @@ -59,7 +64,16 @@ getRepositories = -> io.stderr\write("Could not connect to the Internet. Please ensure you have an Internet connection.") return -1 - return serial.unserialize(raw_repositories) + deserialized_repositories = serial.unserialize(raw_repositories) + + repositories = {} + for repositoryName, repository in pairs(deserialized_repositories) + src = repository or nil + if src == nil + src = githubContentRoot .. repository.repo .."/master" + repositories[repositoryName] = Repository(repositoryName, src) + + repositories -- Get Packages for src getPackages = (src) -> @@ -70,9 +84,6 @@ getPackages = (src) -> return serial.unserialize(packages) -getGithubPackages = (repo) -> - return getPackages(githubContentRoot .. repo .. "/master") - --For sorting table values by alphabet compare = (a, b) -> for i = 1, math.min(#a, #b) @@ -153,20 +164,12 @@ listPackages = (filter) -> return for _, repository in pairs(repositories) - packages = -1 - target = -1 - - if repository.src - print("Checking Repository " .. repository.src) - packages = getPackages(repository.src) - target = repository.src - elseif repository.repo - print("Checking Repository " .. repository.repo) - packages = getGithubPackages(repository.repo) - target = repository.repo + + print("Checking Repository " .. repository.src) + packages = getPackages(repository.src) if packages == nil - io.stderr\write("Error while trying to receive package list for " .. target .. "\n") + io.stderr\write("Error while trying to receive package list for " .. repository.src .. "\n") return elseif type(packages) == "table" for package in pairs(packages) @@ -220,18 +223,10 @@ getInformation = (requestedPackage) -> return for _, repository in pairs(repositories) - packages = -1 - target = -1 - - if repository.src - packages = getPackages(repository.src) - target = repository.src - elseif repository.repo - packages = getGithubPackages(repository.repo) - target = githubContentRoot .. repository.repo + packages = getPackages(repository.src) if packages == nil - io.stderr\write("Error while trying to receive package list for " .. target .. "\n") + io.stderr\write("Error while trying to receive package list for " .. repository.src .. "\n") elseif type(packages) == "table" for package in pairs(packages) if package == requestedPackage