From c55a67f8a93115887490bea67241b1572aa075d4 Mon Sep 17 00:00:00 2001 From: tcsenpai Date: Tue, 16 Jul 2024 13:22:40 +0200 Subject: [PATCH] first upload --- LICENSE.md | 7 ++++ README.md | 48 ++++++++++++++++++++++ upm.bash | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 LICENSE.md create mode 100644 README.md create mode 100755 upm.bash diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..bcaf722 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright 2024-present, tcsenpai + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..68dbe63 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# Universal Package Manager (UPM) + +UPM is a bash script that simplifies package management across multiple package managers. + +## Features + +- Supports apt, brew, pip, npm, and cargo +- Installs, removes, updates, and searches for packages +- Automatically elevates privileges when necessary +- Logs operations for troubleshooting + +## Installation + +1. Download the `upm.bash` script +2. Make it executable: `chmod +x upm.bash` +3. Move it to a directory in your PATH: `sudo mv upm.bash /usr/local/bin/upm` + +## Usage + +`upm ` + + +Commands: +- `install`: Install a package +- `remove`: Remove a package +- `update`: Update a package +- `search`: Search for a package +- `version`: Display UPM version + +## Examples + +`bash +upm install nodejs +upm remove python3 +upm update git +upm search docker` + +## Logging + +Operations are logged to `/var/log/upm.log` + +## Version + +Current version: 1.0.0 + +## License + +This project is licensed under the MIT License - see the LICENSE file for details \ No newline at end of file diff --git a/upm.bash b/upm.bash new file mode 100755 index 0000000..09bd205 --- /dev/null +++ b/upm.bash @@ -0,0 +1,118 @@ +#!/bin/bash + +# Color definitions +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +VERSION="1.0.0" +LOG_FILE="/var/log/upm.log" + +# Function to log messages +log() { + echo "$(date): $1" >> "$LOG_FILE" +} + +# Function to check and elevate privileges if necessary +elevate_privileges() { + if [ "$(id -u)" -ne 0 ]; then + echo -e "${YELLOW}This script requires root privileges. Attempting to run with sudo...${NC}" + exec sudo "$0" "$@" + log "Elevating privileges" + fi +} + +# Call the function to elevate privileges +elevate_privileges "$@" + + +function usage { + echo -e "${BLUE}Usage: upm ${NC}" + echo -e "${BLUE}Commands: install, remove, update, search, version${NC}" + exit 1 +} + +function run_command { + local action=$1 + local pkg=$2 + + # Validate package name + if [[ ! "$pkg" =~ ^[a-zA-Z0-9_.-]+$ ]]; then + echo -e "${RED}Invalid package name. Only alphanumeric characters, underscores, hyphens, and dots are allowed.${NC}" + log "Invalid package name: $pkg" + return 1 + fi + + local managers=( + "apt:install:remove:upgrade:search" + "brew:install:uninstall:upgrade:search" + "pip:install:uninstall:install --upgrade:search" + "npm:-g install:uninstall:update:search" + "cargo:install:uninstall:update:search" + ) + + for manager_info in "${managers[@]}"; do + IFS=':' read -r manager install remove update search <<< "$manager_info" + if command -v $manager &> /dev/null; then + echo -e "${BLUE}Attempting to $action $pkg using $manager...${NC}" + log "Attempting to $action $pkg using $manager" + case $action in + install) cmd=$install ;; + remove) cmd=$remove ;; + update) cmd=$update ;; + search) cmd=$search ;; + esac + if $manager $cmd $pkg; then + echo -e "${GREEN}Successfully $action $pkg using $manager.${NC}" + log "Successfully $action $pkg using $manager" + return 0 + else + echo -e "${YELLOW}Failed to $action $pkg using $manager. Trying next package manager...${NC}" + log "Failed to $action $pkg using $manager" + fi + else + echo -e "${YELLOW}$manager is not installed. Skipping...${NC}" + log "$manager is not installed. Skipping" + fi + done + + echo -e "${RED}Failed to $action $pkg with any available package manager.${NC}" + log "Failed to $action $pkg with any available package manager" + return 1 +} + +if [ $# -lt 1 ]; then + usage +fi + +command=$1 +package=$2 + +case $command in + install|remove|update|search) + if [ -z "$package" ]; then + echo -e "${RED}Error: Package name is required for $command command.${NC}" + log "Error: Package name is required for $command command" + usage + fi + run_command "$command" "$package" + ;; + version) + echo "upm version $VERSION" + exit 0 + ;; + *) + usage + ;; +esac + +if [ $? -eq 0 ]; then + echo -e "${GREEN}Operation completed successfully.${NC}" + log "Operation completed successfully" +else + echo -e "${RED}Operation failed.${NC}" + log "Operation failed" + exit 1 +fi \ No newline at end of file