From bde200bd5c3e3344e4feea8a5ae1dbfb15cdedf1 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Sun, 28 May 2023 21:35:02 +0000 Subject: [PATCH] draft --- .devcontainer/devcontainer.json | 11 +-- .shellspec | 12 +++ LICENSE | 2 +- README.md | 46 ++++++++++- alias.sh | 4 + flake.nix | 32 ++------ .../patch-manjaro-sway-mirror-for-arm_spec.sh | 79 +++++++++++++++++++ spec/spec_helper.sh | 24 ++++++ src/hello.sh | 3 - src/patch-manjaro-sway-mirror-for-arm.sh | 76 ++++++++++++++++++ 10 files changed, 255 insertions(+), 34 deletions(-) create mode 100644 .shellspec create mode 100644 alias.sh create mode 100644 spec/patch-manjaro-sway-mirror-for-arm_spec.sh create mode 100644 spec/spec_helper.sh delete mode 100755 src/hello.sh create mode 100755 src/patch-manjaro-sway-mirror-for-arm.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 83a1938..b3a7d4a 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,9 +1,9 @@ // For format details, see https://aka.ms/devcontainer.json. For config options, see the // README at: https://github.com/devcontainers/templates/tree/main/src/alpine { - "name": "Alpine", + "name": "Manjaro", // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile - "image": "mcr.microsoft.com/devcontainers/base:alpine-3.17", + "image": "manjarolinux/build", "features": { "ghcr.io/devcontainers/features/nix:1": {} }, @@ -30,11 +30,12 @@ "eamodio.gitlens", "streetsidesoftware.code-spell-checker", "timonwong.shellcheck", - "foxundermoon.shell-format" + "foxundermoon.shell-format", + "yzhang.markdown-all-in-one" ] } - } + }, // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. - // "remoteUser": "root" + "remoteUser": "builder" } diff --git a/.shellspec b/.shellspec new file mode 100644 index 0000000..d567ecf --- /dev/null +++ b/.shellspec @@ -0,0 +1,12 @@ +--require spec_helper + +## Default kcov (coverage) options +# --kcov-options "--include-path=. --path-strip-level=1" +# --kcov-options "--include-pattern=.sh" +# --kcov-options "--exclude-pattern=/.shellspec,/spec/,/coverage/,/report/" + +## Example: Include script "myprog" with no extension +# --kcov-options "--include-pattern=.sh,myprog" + +## Example: Only specified files/directories +# --kcov-options "--include-pattern=myprog,/lib/" diff --git a/LICENSE b/LICENSE index 04d8fab..d5caa32 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 stubbfel +Copyright (c) 2023 stubbfel 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: diff --git a/README.md b/README.md index 55a30a5..7daddfd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,48 @@ -# manjaro-sway-mirror-arm-patch +# patch-manjaro-sway-mirror-for-arm + +The `patch-manjaro-sway-mirror-for-arm` package is a specialized package designed for Manjaro Linux users running the Sway window manager on ARM-based systems. +This package provides a convenient solution for patching or modifying mirror configurations specific to the Manjaro Sway environment. + +## Features + +- Observes the `/usr/bin/manjaro-sway-mirrors` file for changes. +- Automatically executes a sed command to modify mirror configurations. +- Changes the output of `$(pacman-mirrors -G)` from "arm-stable" to "stable". + +## Usage + +The package includes a POSIX-compatible script, `patch-manjaro-sway-mirror-for-arm.sh`, which offers a command-line interface with various options: + +``` +Usage: patch-manjaro-sway-mirror-for-arm.sh [OPTIONS] [FILE] [SEARCH_PATTERN] [REPLACE_PATTERN] + +Options: + -t, --target-file Specify the target file (default: /usr/bin/manjaro-sway-mirrors) + -s, --search-pattern Specify the search pattern (default: $(pacman-mirrors -G)) + -r, --replace-pattern Specify the replace pattern (default: $(pacman-mirrors -G | sed 's/^arm-//')) + --dry-run Perform a dry run without modifying the file + --help Display usage information +``` + +## Installation + +To install `patch-manjaro-sway-mirror-for-arm`, follow these steps: + +1. Clone the repository or download the package. +2. Run the installation command: + +``` +sudo pacman -U patch-manjaro-sway-mirror-for-arm.pkg.tar.zst +``` + +## License + +This package is licensed under the [MIT License](LICENSE). + +## Contributions + +Contributions to the project are welcome! +If you find any issues or have suggestions for improvements, please open an issue or submit a merge request. ## Nix Flake commands diff --git a/alias.sh b/alias.sh new file mode 100644 index 0000000..66a3c08 --- /dev/null +++ b/alias.sh @@ -0,0 +1,4 @@ +#!/bin/env sh + +alias nixe='nix --experimental-features "nix-command flakes"' +alias ulock='nixe run .#devTasks.updateFlakeLock' diff --git a/flake.nix b/flake.nix index f7ecc68..bcbcdbd 100644 --- a/flake.nix +++ b/flake.nix @@ -1,10 +1,13 @@ { - description = ""; + description = '' + With `patch-manjaro-sway-mirror-for-arm`, users can easily observe the `/usr/bin/manjaro-sway-mirrors` file for changes and automatically execute a sed command to modify its contents. + This ensures that the desired mirror configuration, specifically changing the output of `$(pacman-mirrors -G)` from "arm-stable" to "stable," is consistently maintained + ''; inputs.nixpkgs.url = "nixpkgs/nixos-22.05-small"; outputs = { self, nixpkgs }: let - name = "manjaro-sway-mirror-arm-patch"; + name = "patch-manjaro-sway-mirror-for-arm"; version = "0.0.1"; supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; forAllSystems = nixpkgs.lib.genAttrs supportedSystems; @@ -24,7 +27,7 @@ name = name; src = self; buildPhase = "echo nothing todo"; - installPhase = "mkdir -p $out/bin; install -t $out/bin src/hello.sh"; + installPhase = "mkdir -p $out/bin; install -t $out/bin src/patch-manjaro-sway-mirror-for-arm.sh"; }; }); @@ -36,7 +39,7 @@ ''; in { - default = { type = "app"; program = "${packages.${system}.default}/bin/hello.sh"; }; + default = { type = "app"; program = "${packages.${system}.default}/bin/patch-manjaro-sway-mirror-for-arm.sh"; }; devTasks = { updateFlakeLock = { type = "app"; program = "${updateLockScript}/bin/update_flake_lock.sh"; }; autoTag = { type = "app"; program = "${devTaskScripts.${system}.autoTag}"; }; @@ -47,30 +50,11 @@ { default = nixpkgsFor.${system}.mkShell { name = "dev-shell"; - packages = [ ]; + packages = [ nixpkgsFor.${system}.nixpkgs-fmt nixpkgsFor.${system}.shellspec ]; shellHook = '' alias nixe="nix --experimental-features 'nix-command flakes'" ''; }; }); - - hydraJobs = { - tarball = nixpkgsFor.x86_64-linux.releaseTools.sourceTarball { - name = name; - src = self; - version = version; - officialRelease = true; - bootstrapBuildInputs = [ ]; - distPhase = '' - mkdir $out/tarballs - tar -czvf $out/tarballs/${name}-${version}.tar.gz * - ''; - }; - - runCommandHook = { - recurseForDerivations = { }; - autoTag = devTaskScripts.x86_64-linux.autoTag; - }; - }; }; } diff --git a/spec/patch-manjaro-sway-mirror-for-arm_spec.sh b/spec/patch-manjaro-sway-mirror-for-arm_spec.sh new file mode 100644 index 0000000..0349e45 --- /dev/null +++ b/spec/patch-manjaro-sway-mirror-for-arm_spec.sh @@ -0,0 +1,79 @@ +#!/bin/env sh + +Describe "patch-manjaro-sway-mirror-for-arm.sh - Test Suite" + + test_root=$(mktemp -d) + target_file="${test_root}/usr/bin/manjaro-sway-mirrors" + custom_file="${target_file}-custom" + custom_replace_pattern="replaced" + + # shellcheck disable=SC2016 + test_content='curl -s "https://pkg.manjaro-sway.download/$(pacman-mirrors -G)/testing"' + + setup_test_data() { + export MSMAP_TARGET_ROOT="${test_root}" + mkdir -p "${test_root}/usr/bin" + + echo "${test_content}" > "${target_file}" + cp -f "${target_file}" "${custom_file}" + } + + teardown_test_data() { + unset MSMAP_TARGET_ROOT + rm -rf "${test_root}" + } + + BeforeEach 'setup_test_data' + AfterEach 'teardown_test_data' + + Describe "Help message" + It "should display the usage information" + When run src/patch-manjaro-sway-mirror-for-arm.sh --help + The output should include "Usage: patch-manjaro-sway-mirror-for-arm.sh" + The status should be success + End + End + + Describe "Dry run" + It "should perform a dry run without modifying the target file" + When run src/patch-manjaro-sway-mirror-for-arm.sh --dry-run + The status should be success + The contents of file "${target_file}" should equal "${test_content}" + # shellcheck disable=SC2016 + The output should equal "sed -i \"s|\\\$(pacman-mirrors -G)|\\\$(pacman-mirrors -G \| sed \\\"s/^arm-//\\\")|g\" \"${target_file}\" +curl -s \"https://pkg.manjaro-sway.download/\$(pacman-mirrors -G | sed \"s/^arm-//\")/testing\"" + End + End + + Describe "Default values" + It "should modify the target file with default values" + When run src/patch-manjaro-sway-mirror-for-arm.sh + The output should equal "" + The status should be success + The contents of file "${target_file}" should equal "curl -s \"https://pkg.manjaro-sway.download/\$(pacman-mirrors -G | sed \"s/^arm-//\")/testing\"" + End + End + + Describe "Custom target file and replace pattern" + It "should modify the custom target file with the provided replace pattern" + When run src/patch-manjaro-sway-mirror-for-arm.sh "${custom_file}" -r "${custom_replace_pattern}" + The output should equal "" + The status should be success + The contents of file "${target_file}" should equal "${test_content}" + The contents of file "$custom_file" should equal "curl -s \"https://pkg.manjaro-sway.download/replaced/testing\"" + End + End + + Describe "Positional arguments" + custom_search_pattern="testing" + + It "should modify the target file with positional arguments" + When run src/patch-manjaro-sway-mirror-for-arm.sh "$custom_file" "${custom_search_pattern}" "${custom_replace_pattern}" + The output should equal "" + The status should be success + The contents of file "${target_file}" should equal "${test_content}" + The contents of file "$custom_file" should equal "curl -s \"https://pkg.manjaro-sway.download/\$(pacman-mirrors -G)/replaced\"" + End + End +End + diff --git a/spec/spec_helper.sh b/spec/spec_helper.sh new file mode 100644 index 0000000..93f1908 --- /dev/null +++ b/spec/spec_helper.sh @@ -0,0 +1,24 @@ +# shellcheck shell=sh + +# Defining variables and functions here will affect all specfiles. +# Change shell options inside a function may cause different behavior, +# so it is better to set them here. +# set -eu + +# This callback function will be invoked only once before loading specfiles. +spec_helper_precheck() { + # Available functions: info, warn, error, abort, setenv, unsetenv + # Available variables: VERSION, SHELL_TYPE, SHELL_VERSION + : minimum_version "0.28.1" +} + +# This callback function will be invoked after a specfile has been loaded. +spec_helper_loaded() { + : +} + +# This callback function will be invoked after core modules has been loaded. +spec_helper_configure() { + # Available functions: import, before_each, after_each, before_all, after_all + : import 'support/custom_matcher' +} diff --git a/src/hello.sh b/src/hello.sh deleted file mode 100755 index 2202fe7..0000000 --- a/src/hello.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/env bash - -echo "hello world" \ No newline at end of file diff --git a/src/patch-manjaro-sway-mirror-for-arm.sh b/src/patch-manjaro-sway-mirror-for-arm.sh new file mode 100755 index 0000000..9502e51 --- /dev/null +++ b/src/patch-manjaro-sway-mirror-for-arm.sh @@ -0,0 +1,76 @@ +#!/bin/env sh + +usage() { + echo "Usage: patch-manjaro-sway-mirror-for-arm.sh [OPTIONS]" + echo "Options:" + echo " -t, --target-file Path to the target file (default: /usr/bin/manjaro-sway-mirrors)" + echo " -s, --search-pattern Search pattern to replace (default: $(pacman-mirrors -G)" + echo " -r, --replace-pattern Replace pattern (default: \$(pacman-mirrors -G \| sed 's/^arm-//'))" + echo " --dry-run Run the sed command without modifying the file" + echo " --help Display this help message" + exit 0 +} + +# Default values +dry_run=false + +# Parse command-line options +while [ $# -gt 0 ]; do + case "$1" in + -t | --target-file) + shift + target_file="$1" + ;; + -s | --search-pattern) + shift + search_pattern="$1" + ;; + -r | --replace-pattern) + shift + replace_pattern="$1" + ;; + --dry-run) + dry_run=true + ;; + --help) + usage + ;; + *) + # Check if positional arguments are provided + if [ -z "$target_file" ]; then + target_file="$1" + elif [ -z "$search_pattern" ]; then + search_pattern="$1" + elif [ -z "$replace_pattern" ]; then + replace_pattern="$1" + else + echo "Invalid argument: $1" + usage + fi + ;; + esac + shift +done + +if [ -z "$target_file" ]; then + target_file="${MSMAP_TARGET_ROOT}/usr/bin/manjaro-sway-mirrors" +fi + +if [ -z "$search_pattern" ]; then + # shellcheck disable=SC2016 + search_pattern='\$(pacman-mirrors -G)' +fi + +if [ -z "$replace_pattern" ]; then + # shellcheck disable=SC2016 + replace_pattern='\$(pacman-mirrors -G \| sed \"s/^arm-//\")' +fi + +sed_command="sed -i \"s|${search_pattern}|${replace_pattern}|g\" \"${target_file}\"" +# Perform the sed command +if $dry_run; then + echo "$sed_command" + sed "s|${search_pattern}|${replace_pattern}|g" "${target_file}" +else + eval "${sed_command}" +fi