-
Notifications
You must be signed in to change notification settings - Fork 261
Intergrating clib-uninstall #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c14afd8
Intergrating clib-uninstall
Isty001 7f563e4
Intergrating clib-uninstall
Isty001 1a838c6
Intergrating clib-uninstall
Isty001 b1b7ee2
Intergrating clib-uninstall
Isty001 242d4d2
Intergrating clib-uninstall
Isty001 7cda5b9
Intergrating clib-uninstall
Isty001 e1c3a71
Integrating clib-uninstal
Isty001 7c5495b
Integrating clib-uninstall
Isty001 155cdfd
Intergrating clib-uninstall
Isty001 08374f9
Intergarting clib-uninstall
Isty001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // | ||
| // clib-uninstall.c | ||
| // | ||
| // Copyright (c) 2014 Stephen Mathieson | ||
| // Copyright (c) 2021 clib authors | ||
| // | ||
| // MIT licensed | ||
| // | ||
|
|
||
| #include "asprintf/asprintf.h" | ||
| #include "commander/commander.h" | ||
| #include "http-get/http-get.h" | ||
| #include "logger/logger.h" | ||
| #include "parse-repo/parse-repo.h" | ||
| #include "debug/debug.h" | ||
| #include "parson/parson.h" | ||
| #include "version.h" | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #define CLIB_UNINSTALL_DEFAULT_TARGET "make uninstall" | ||
|
|
||
| #if defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) || \ | ||
| defined(__MINGW64__) || defined(__CYGWIN__) | ||
| #define setenv(k, v, _) _putenv_s(k, v) | ||
| #endif | ||
|
|
||
| debug_t debugger; | ||
|
|
||
| static void setopt_prefix(command_t *self) { | ||
| setenv("PREFIX", (char *)self->arg, 1); | ||
| debug(&debugger, "set prefix: %s", (char *)self->arg); | ||
| } | ||
|
|
||
| static char *get_tarball_url(const char *owner, const char *name, | ||
| const char *version) { | ||
| char *tarball = NULL; | ||
| int size = 0; | ||
|
|
||
| size = asprintf(&tarball, "https://github.com/%s/%s/archive/%s.tar.gz", owner, | ||
| name, version); | ||
|
|
||
| if (-1 == size) | ||
| return NULL; | ||
| return tarball; | ||
| } | ||
|
|
||
| static char *get_tar_filepath(const char *name, const char *version) { | ||
| char *file = NULL; | ||
| int size = asprintf(&file, "%s-%s.tar.gz", name, version); | ||
| if (-1 == size) | ||
| return NULL; | ||
| return file; | ||
| } | ||
|
|
||
| static char *get_tmp_tarball(const char *file) { | ||
| char *tmp = NULL; | ||
| int size = asprintf(&tmp, "/tmp/%s", file); | ||
| if (-1 == size) | ||
| return NULL; | ||
| return tmp; | ||
| } | ||
|
|
||
| static char *get_untar_command(const char *file) { | ||
| char *cmd = NULL; | ||
| int size = 0; | ||
| size = asprintf(&cmd, "cd /tmp && tar -xf %s", file); | ||
| if (-1 == size) | ||
| return NULL; | ||
| return cmd; | ||
| } | ||
|
|
||
| static char *get_uninstall_target(const char *name, const char *version) { | ||
| int size = 0; | ||
| char *target = NULL; | ||
| char *dir = NULL; | ||
| char *pkg = NULL; | ||
| const char *val = NULL; | ||
| JSON_Value *root = NULL; | ||
| JSON_Object *obj = NULL; | ||
|
|
||
| size = asprintf(&dir, "/tmp/%s-%s", name, version); | ||
| if (-1 == size) | ||
| return NULL; | ||
|
|
||
| size = asprintf(&pkg, "%s/package.json", dir); | ||
| if (-1 == size) | ||
| goto done; | ||
|
|
||
| root = json_parse_file(pkg); | ||
| if (!root) | ||
| goto done; | ||
|
|
||
| obj = json_value_get_object(root); | ||
| if (!obj) | ||
| goto done; | ||
|
|
||
| val = json_object_get_string(obj, "uninstall"); | ||
| if (!val) { | ||
| logger_warn("warning", | ||
| "No uninstall target specified. Defaulting to '%s'.", | ||
| CLIB_UNINSTALL_DEFAULT_TARGET); | ||
| // default to "make uninstall" | ||
| val = CLIB_UNINSTALL_DEFAULT_TARGET; | ||
| } | ||
|
|
||
| size = asprintf(&target, "cd %s && %s", dir, val); | ||
| if (-1 == size) | ||
| return NULL; | ||
|
|
||
| done: | ||
| if (root) | ||
| json_value_free(root); | ||
| free(dir); | ||
| free(pkg); | ||
| return target; | ||
| } | ||
|
|
||
| static int clib_uninstall(const char *owner, const char *name, | ||
| const char *version) { | ||
| char *tarball = NULL; | ||
| char *file = NULL; | ||
| char *tarpath = NULL; | ||
| char *cmd = NULL; | ||
| char *target = NULL; | ||
| int rc = -1; | ||
|
|
||
| // sanity | ||
| if (!owner || !name || !version) | ||
| return -1; | ||
|
|
||
| if (!(tarball = get_tarball_url(owner, name, version))) | ||
| goto done; | ||
| if (!(file = get_tar_filepath(name, version))) | ||
| goto done; | ||
| if (!(tarpath = get_tmp_tarball(file))) | ||
| goto done; | ||
|
|
||
| logger_info("fetch", tarball); | ||
| if (-1 == http_get_file(tarball, tarpath)) { | ||
| logger_error("error", "failed to fetch tarball"); | ||
| goto done; | ||
| } | ||
|
|
||
| if (!(cmd = get_untar_command(file))) | ||
| goto done; | ||
|
|
||
| logger_info("untar", tarpath); | ||
| if (0 != system(cmd)) { | ||
| logger_error("error", "failed to untar"); | ||
| goto done; | ||
| } | ||
|
|
||
| target = get_uninstall_target(name, version); | ||
| if (!target) | ||
| goto done; | ||
|
|
||
| rc = system(target); | ||
|
|
||
| done: | ||
| free(tarball); | ||
| free(file); | ||
| free(tarpath); | ||
| free(cmd); | ||
| free(target); | ||
| return rc; | ||
| } | ||
|
|
||
| int main(int argc, char **argv) { | ||
| int rc = 1; | ||
| command_t program; | ||
|
|
||
| debug_init(&debugger, "clib-uninstall"); | ||
|
|
||
| command_init(&program, "clib-uninstall", CLIB_VERSION); | ||
|
|
||
| program.usage = "[options] [name ...]"; | ||
|
|
||
| command_option(&program, "-P", "--prefix <dir>", | ||
| "change the prefix directory (usually '/usr/local')", | ||
| setopt_prefix); | ||
|
|
||
| command_parse(&program, argc, argv); | ||
|
|
||
| if (0 == program.argc) | ||
| command_help(&program); | ||
|
|
||
| for (int i = 0; i < program.argc; i++) { | ||
| char *owner = parse_repo_owner(program.argv[i], NULL); | ||
| if (!owner) | ||
| goto cleanup; | ||
| char *name = parse_repo_name(program.argv[i]); | ||
| if (!name) { | ||
| free(owner); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| int res = clib_uninstall(owner, name, "master"); | ||
| free(owner); | ||
| free(name); | ||
| if (-1 == res) { | ||
| logger_error("error", "Failed to uninstall %s", program.argv[i]); | ||
| goto cleanup; | ||
| } | ||
| } | ||
|
|
||
| rc = 0; | ||
|
|
||
| cleanup: | ||
| command_free(&program); | ||
| return rc; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #!/bin/bash | ||
|
|
||
| mkdir -p tmp/bin | ||
|
|
||
| clib install -c stephenmathieson/[email protected] -P tmp > /dev/null || { | ||
| echo >&2 "Failed to install stephenmathieson/tabs-to-spaces" | ||
| exit 1 | ||
| } | ||
|
|
||
| clib uninstall stephenmathieson/tabs-to-spaces -P tmp | ||
|
|
||
| # ensure the un-installation worked | ||
| command -v tmp/bin/t2s >/dev/null 2>&1 && { | ||
| exit 0 | ||
| } | ||
|
|
||
| echo >&2 "Failed to uninstall stephenmathieson/tabs-to-spaces"; | ||
| exit 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.