diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..a35e9b3 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,17 @@ +#!/bin/bash + +#/** +# * TangoMan pre-commit git hook +# * +# * @license MIT +# * @author "Matthias Morin" +# * @version 0.1.0 +# */ + +set -efu -o pipefail + +BASEDIR="$(git rev-parse --show-toplevel)" + +cd "${BASEDIR}" || exit 1 + +sh "${BASEDIR}"/entrypoint.sh lint tests diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..8b6807c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,33 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Call script with parameters ... +2. Type ... +3. Wait for ... seconds +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. MacOS] + - bash version (bash --version) + - Script version + - copy/paste output of script.sh -v + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/bash_unit.yml b/.github/workflows/bash_unit.yml new file mode 100644 index 0000000..017c269 --- /dev/null +++ b/.github/workflows/bash_unit.yml @@ -0,0 +1,28 @@ +name: bash_unit CI +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + ubuntu: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Unit testing with bash_unit + run: ./entrypoint.sh tests + + macos: + runs-on: macos-latest + if: "contains(toJSON(github.event.commits.*.message), '[macos]')" + + steps: + - uses: actions/checkout@v2 + + - name: Unit testing with bash_unit + run: ./entrypoint.sh tests diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml new file mode 100644 index 0000000..d4cfbda --- /dev/null +++ b/.github/workflows/shellcheck.yml @@ -0,0 +1,18 @@ +name: ShellCheck CI +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + shellcheck: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Check for code quality errors + run: ./entrypoint.sh lint diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1bca8df --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +Change Log +========== + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] +- WIP + +## [0.1.0](https://github.com/TangoMan75/tools/releases/tag/0.1.0) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..b480fb3 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,76 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, sex characteristics, gender identity and expression, +level of experience, education, socio-economic status, nationality, personal +appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at mat@tangoman.io. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see +https://www.contributor-covenant.org/faq diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..707387a --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,242 @@ +# Contributing + +## How do I... + +* [Use This Guide](#introduction)? +* Ask or Say Something? πŸ€”πŸ›πŸ˜± + * [Request Support](#request-support) + * [Report an Error or Bug](#report-an-error-or-bug) + * [Request a Feature](#request-a-feature) +* Make Something? πŸ€“πŸ‘©πŸ½β€πŸ’»πŸ“œπŸ³ + * [Project Setup](#project-setup) + * [Contribute Documentation](#contribute-documentation) + * [Contribute Code](#contribute-code) +* Manage Something βœ…πŸ™†πŸΌπŸ’ƒπŸ‘” + * [Provide Support on Issues](#provide-support-on-issues) + * [Label Issues](#label-issues) + * [Clean Up Issues and PRs](#clean-up-issues-and-prs) + * [Review Pull Requests](#review-pull-requests) + * [Merge Pull Requests](#merge-pull-requests) + * [Tag a Release](#tag-a-release) + * [Join the Project Team](#join-the-project-team) +* Add a Guide Like This One [To My Project](#attribution)? πŸ€–πŸ˜»πŸ‘» + +## Introduction + +Thank you so much for your interest in contributing!. All types of contributions are encouraged and valued. See the [table of contents](#toc) for different ways to help and details about how this project handles them!πŸ“ + +Please make sure to read the relevant section before making your contribution! It will make it a lot easier for us maintainers to make the most of it and smooth out the experience for all involved. πŸ’š + +## Request Support + +If you have a question about this project, how to use it, or just need clarification about something: + +* Open an Issue at https://github.com/TangoMan75/tools/issues +* Provide as much context as you can about what you're running into. +* Provide project and platform versions (bash, basher, etc), depending on what seems relevant. If not, please be ready to provide that information if maintainers ask for it. + +Once it's filed: + +* The project team will [label the issue](#label-issues). +* Someone will try to have a response soon. +* If you or the maintainers don't respond to an issue for 30 days, the [issue will be closed](#clean-up-issues-and-prs). + If you want to come back to it, reply (once, please), and we'll reopen the existing issue. Please avoid filing new issues as extensions of one you already made. + +## Report an Error or Bug + +If you run into an error or bug with the project: + +* Open an Issue at https://github.com/TangoMan75/tools/issues +* Include *reproduction steps* that someone else can follow to recreate the bug or error on their own. +* Provide project and platform versions (OS, bash, basher, etc), depending on what seems relevant. If not, please be ready to provide that information if maintainers ask for it. + +Once it's filed: + +* The project team will [label the issue](#label-issues). +* A team member will try to reproduce the issue with your provided steps. If there are no repro steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced. +* If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#contribute-code). +* If you or the maintainers don't respond to an issue for 30 days, the [issue will be closed](#clean-up-issues-and-prs). If you want to come back to it, reply (once, please), and we'll reopen the existing issue. Please avoid filing new issues as extensions of one you already made. +* `critical` issues may be left open, depending on perceived immediacy and severity, even past the 30 day deadline. + +## Request a Feature + +If the project doesn't do something you need or want it to do: + +* Open an Issue at https://github.com/TangoMan75/tools/issues +* Provide as much context as you can about what you're running into. +* Please be clear about why existing features and alternatives would not work for you. + +Once it's filed: + +* The project team will [label the issue](#label-issues). +* The project team will evaluate the feature request, possibly asking you more questions to understand its purpose and any relevant requirements. If the issue is closed, the team will convey their reasoning and suggest an alternative path forward. +* If the feature request is accepted, it will be marked for implementation with `feature-accepted`, which can then be done by either by a core team member or by anyone in the community who wants to [contribute code](#contribute-code). + +Note: The team is unlikely to be able to accept every single feature request that is filed. Please understand if they need to say no. + +## Project Setup + +So you want to contribute some code! That's great! This project uses GitHub Pull Requests to manage contributions, so [read up on how to fork a GitHub project and file a PR](https://guides.github.com/activities/forking) if you've never done it before. + +If this seems like a lot or you aren't able to do all this setup, you might also be able to [edit the files directly](https://help.github.com/articles/editing-files-in-another-user-s-repository/) without having to do any of this setup. Yes, [even code](#contribute-code). + +If you want to go the usual route and run the project locally, though: + +* [Fork the project](https://guides.github.com/activities/forking/#fork) + +Then in your terminal: +* `cd path/to/your/clone` + +And you should be ready to go! + +## Contribute Documentation + +Documentation is a super important, critical part of this project. Docs are how we keep track of what we're doing, how, and why. It's how we stay on the same page about our policies. And it's how we tell others everything they need in order to be able to use this project -- or contribute to it. So thank you in advance. + +Documentation contributions of any size are welcome! Feel free to file a PR even if you're just rewording a sentence to be more clear, or fixing a spelling mistake! + +To contribute documentation: + +* [Set up the project](#project-setup). +* Edit or add any relevant documentation. +* Make sure your changes are formatted correctly and consistently with the rest of the documentation. +* Re-read what you wrote, and run a spellchecker on it to make sure you didn't miss anything. +* Write clear, concise commit message(s) using [conventional-changelog format](https://github.com/conventional-changelog/conventional-changelog-angular/blob/master/convention.md). Documentation commits should use `docs(): `. +* Go to https://github.com/TangoMan75/tools/pulls and open a new pull request with your changes. +* If your PR is connected to an open issue, add a line in your PR's description that says `Fixes: #123`, where `#123` is the number of the issue you're fixing. + +Once you've filed the PR: + +* One or more maintainers will use GitHub's review feature to review your PR. +* If the maintainer asks for any changes, edit your changes, push, and ask for another review. +* If the maintainer decides to pass on your PR, they will thank you for the contribution and explain why they won't be accepting the changes. That's ok! We still really appreciate you taking the time to do it, and we don't take that lightly. πŸ’š +* If your PR gets accepted, it will be marked as such, and merged into the `latest` branch soon after. Your contribution will be distributed to the masses next time the maintainers [tag a release](#tag-a-release) + +## Contribute Code + +We like code commits a lot! They're super handy, and they keep the project going and doing the work it needs to do to be useful to others. + +Code contributions of just about any size are acceptable! + +The main difference between code contributions and documentation contributions is that contributing code requires inclusion of relevant tests for the code being added or changed. Contributions without accompanying tests will be held off until a test is added, unless the maintainers consider the specific tests to be either impossible, or way too much of a burden for such a contribution. + +To contribute code: + +* [Set up the project](#project-setup). +* Make any necessary changes to the source code. +* Include any [additional documentation](#contribute-documentation) the changes might need. +* Write tests that verify that your contribution works as expected. +* Write clear, concise commit message(s) using [conventional-changelog format](https://github.com/conventional-changelog/conventional-changelog-angular/blob/master/convention.md). +* Dependency updates, additions, or removals must be in individual commits, and the message must use the format: `(deps): PKG@VERSION`, where `` is any of the usual `conventional-changelog` prefixes, at your discretion. +* Go to https://github.com/TangoMan75/tools/pulls and open a new pull request with your changes. +* If your PR is connected to an open issue, add a line in your PR's description that says `Fixes: #123`, where `#123` is the number of the issue you're fixing. + +Once you've filed the PR: + +* Barring special circumstances, maintainers will not review PRs until all checks pass (Travis, AppVeyor, etc). +* One or more maintainers will use GitHub's review feature to review your PR. +* If the maintainer asks for any changes, edit your changes, push, and ask for another review. Additional tags (such as `needs-tests`) will be added depending on the review. +* If the maintainer decides to pass on your PR, they will thank you for the contribution and explain why they won't be accepting the changes. That's ok! We still really appreciate you taking the time to do it, and we don't take that lightly. πŸ’š +* If your PR gets accepted, it will be marked as such, and merged into the `latest` branch soon after. Your contribution will be distributed to the masses next time the maintainers [tag a release](#tag-a-release) + +## Provide Support on Issues + +Helping out other users with their questions is a really awesome way of contributing to any community. It's not uncommon for most of the issues on an open source projects being support-related questions by users trying to understand something they ran into, or find their way around a known bug. + +Sometimes, the `support` label will be added to things that turn out to actually be other things, like bugs or feature requests. In that case, suss out the details with the person who filed the original issue, add a comment explaining what the bug is, and change the label from `support` to `bug` or `feature`. If you can't do this yourself, @mention a maintainer so they can do it. + +In order to help other folks out with their questions: + +* Go to the issue tracker and [filter open issues by the `support` label](https://github.com/TangoMan75/tools/issues?q=is%3Aopen+is%3Aissue+label%3Asupport). +* Read through the list until you find something that you're familiar enough with to give an answer to. +* Respond to the issue with whatever details are needed to clarify the question, or get more details about what's going on. +* Once the discussion wraps up and things are clarified, either close the issue, or ask the original issue filer (or a maintainer) to close it for you. + +Some notes on picking up support issues: + +* Avoid responding to issues you don't know you can answer accurately. +* As much as possible, try to refer to past issues with accepted answers. Link to them from your replies with the `#123` format. +* Be kind and patient with users -- often, folks who have run into confusing things might be upset or impatient. This is ok. Try to understand where they're coming from, and if you're too uncomfortable with the tone, feel free to stay away or withdraw from the issue. (note: if the user is outright hostile or is violating the CoC, [refer to the Code of Conduct](CODE_OF_CONDUCT.md) to resolve the conflict). + +## Label Issues + +One of the most important tasks in handling issues is labeling them usefully and accurately. All other tasks involving issues ultimately rely on the issue being classified in such a way that relevant parties looking to do their own tasks can find them quickly and easily. + +In order to label issues, [open up the list of unlabeled issues](https://github.com/TangoMan75/tools/issues?q=is%3Aopen+is%3Aissue+no%3Alabel) and, **from newest to oldest**, read through each one and apply issue labels according to the table below. If you're unsure about what label to apply, skip the issue and try the next one: don't feel obligated to label each and every issue yourself! + +Label | Apply When | Notes +--- | --- | --- +`bug` | Cases where the code (or documentation) is behaving in a way it wasn't intended to. | If something is happening that surprises the *user* but does not go against the way the code is designed, it should use the `enhancement` label. +`critical` | Added to `bug` issues if the problem described makes the code completely unusable in a common situation. | +`documentation` | Added to issues or pull requests that affect any of the documentation for the project. | Can be combined with other labels, such as `bug` or `enhancement`. +`duplicate` | Added to issues or PRs that refer to the exact same issue as another one that's been previously labeled. | Duplicate issues should be marked and closed right away, with a message referencing the issue it's a duplicate of (with `#123`) +`enhancement` | Added to [feature requests](#request-a-feature), PRs, or documentation issues that are purely additive: the code or docs currently work as expected, but a change is being requested or suggested. | +`help wanted` | Applied by [Committers](#join-the-project-team) to issues and PRs that they would like to get outside help for. Generally, this means it's lower priority for the maintainer team to itself implement, but that the community is encouraged to pick up if they so desire | Never applied on first-pass labeling. +`in-progress` | Applied by [Committers](#join-the-project-team) to PRs that are pending some work before they're ready for review. | The original PR submitter should @mention the team member that applied the label once the PR is complete. +`performance` | This issue or PR is directly related to improving performance. | +`refactor` | Added to issues or PRs that deal with cleaning up or modifying the project for the betterment of it. | +`starter` | Applied by [Committers](#join-the-project-team) to issues that they consider good introductions to the project for people who have not contributed before. These are not necessarily "easy", but rather focused around how much context is necessary in order to understand what needs to be done for this project in particular. | Existing project members are expected to stay away from these unless they increase in priority. +`support` | This issue is either asking a question about how to use the project, clarifying the reason for unexpected behavior, or possibly reporting a `bug` but does not have enough detail yet to determine whether it would count as such. | The label should be switched to `bug` if reliable reproduction steps are provided. Issues primarily with unintended configurations of a user's environment are not considered bugs, even if they cause crashes. +`tests` | This issue or PR either requests or adds primarily tests to the project. | If a PR is pending tests, that will be handled through the [PR review process](#review-pull-requests) +`wontfix` | Labelers may apply this label to issues that clearly have nothing at all to do with the project or are otherwise entirely outside of its scope/sphere of influence. [Committers](#join-the-project-team) may apply this label and close an issue or PR if they decide to pass on an otherwise relevant issue. | The issue or PR should be closed as soon as the label is applied, and a clear explanation provided of why the label was used. Contributors are free to contest the labeling, but the decision ultimately falls on committers as to whether to accept something or not. + +## Clean Up Issues and PRs + +Issues and PRs can go stale after a while. Maybe they're abandoned. Maybe the team will just plain not have time to address them any time soon. + +In these cases, they should be closed until they're brought up again or the interaction starts over. + +To clean up issues and PRs: + +* Search the issue tracker for issues or PRs, and add the term `updated:<=YYYY-MM-DD`, where the date is 30 days before today. +* Go through each issue *from oldest to newest*, and close them if **all of the following are true**: + * not opened by a maintainer + * not marked as `critical` + * not marked as `starter` or `help wanted` (these might stick around for a while, in general, as they're intended to be available) + * no explicit messages in the comments asking for it to be left open + * does not belong to a milestone +* Leave a message when closing saying "Cleaning up stale issue. Please reopen or ping us if and when you're ready to resume this. See https://github.com/TangoMan75/tools/blob/latest/CONTRIBUTING.md#clean-up-issues-and-prs for more details." + +## Review Pull Requests + +While anyone can comment on a PR, add feedback, etc, PRs are only *approved* by team members with Issue Tracker or higher permissions. + +PR reviews use [GitHub's own review feature](https://help.github.com/articles/about-pull-request-reviews/), which manages comments, approval, and review iteration. + +Some notes: + +* You may ask for minor changes ("nitpicks"), but consider whether they are really blockers to merging: try to err on the side of "approve, with comments". +* *ALL PULL REQUESTS* should be covered by a test: either by a previously-failing test, an existing test that covers the entire functionality of the submitted code, or new tests to verify any new/changed behavior. All tests must also pass and follow established conventions. Test coverage should not drop, unless the specific case is considered reasonable by maintainers. +* Please make sure you're familiar with the code or documentation being updated, unless it's a minor change (spellchecking, minor formatting, etc). You may @mention another project member who you think is better suited for the review, but still provide a non-approving review of your own. +* Be extra kind: people who submit code/doc contributions are putting themselves in a pretty vulnerable position, and have put time and care into what they've done (even if that's not obvious to you!) -- always respond with respect, be understanding, but don't feel like you need to sacrifice your standards for their sake, either. Just don't be a jerk about it? + +## Merge Pull Requests + +Project Team will merge pull requests after review. + +## Tag A Release + +Project Team will bump tags/version with [setver](https://github.com/pforret/setver) as a patch or minor versionupdate. +Releases are done manually for important changes. + + +## Join the Project Team + +### Ways to Join + +There are many ways to contribute! Most of them don't require any official status unless otherwise noted. That said, there's a couple of positions that grant special repository abilities, and this section describes how they're granted and what they do. + +All of the below positions are granted based on the project team's needs, as well as their consensus opinion about whether they would like to work with the person and think that they would fit well into that position. The process is relatively informal, and it's likely that people who express interest in participating can just be granted the permissions they'd like. + +You can spot a collaborator on the repo by looking for the `[Collaborator]` or `[Owner]` tags next to their names. + +Permission | Description +--- | --- +Issue Tracker | Granted to contributors who express a strong interest in spending time on the project's issue tracker. These tasks are mainly [labeling issues](#label-issues), [cleaning up old ones](#clean-up-issues-and-prs), and [reviewing pull requests](#review-pull-requests), as well as all the usual things non-team-member contributors can do. Issue handlers should not merge pull requests, tag releases, or directly commit code themselves: that should still be done through the usual pull request process. Becoming an Issue Handler means the project team trusts you to understand enough of the team's process and context to implement it on the issue tracker. +Committer | Granted to contributors who want to handle the actual pull request merges, tagging new versions, etc. Committers should have a good level of familiarity with the codebase, and enough context to understand the implications of various changes, as well as a good sense of the will and expectations of the project team. +Admin/Owner | Granted to people ultimately responsible for the project, its community, etc. + +## Attribution + +This guide was generated using the WeAllJS `CONTRIBUTING.md` generator. [Make your own](https://npm.im/weallcontribute)! + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2aecc0e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Matthias Morin + +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..c694474 --- /dev/null +++ b/README.md @@ -0,0 +1,221 @@ +![GH language](https://img.shields.io/github/languages/top/TangoMan75/tools) +[![GH license](https://img.shields.io/github/license/TangoMan75/tools)]((https://github.com/TangoMan75/tools/blob/master/LICENSE)) +[![GH release](https://img.shields.io/github/v/release/TangoMan75/tools)](https://github.com/TangoMan75/tools/releases) +[![GH stars](https://img.shields.io/github/stars/TangoMan75/tools)](https://github.com/TangoMan75/tools/stargazers) +[![Bash CI](https://github.com/TangoMan75/tools/workflows/bash_unit%20CI/badge.svg)](https://github.com/TangoMan75/tools/actions/workflows/bash_unit.yml) +[![ShellCheck CI](https://github.com/TangoMan75/tools/workflows/ShellCheck%20CI/badge.svg)](https://github.com/TangoMan75/tools/actions/workflows/shellcheck.yml) +![visitors](https://visitor-badge.glitch.me/badge?page_id=TangoMan75.tools) + +TangoMan Bash Tool +================== + +**TangoMan Bash Tools** is an awesome collection of small useful shell scripts. + +⏳ TL;DR +-------- + +1. Copy "tools" folder into your project. +2. Source desired script in your own script. + +```bash +source ./tools/src/colors/colors.sh +``` + +3. Call desired function. +```bash +echo_info "Hello World !" +``` + +🎯 Features +----------- + +**TangoMan Bash Tool** provides the following features: + +### ⚑ colors.sh + +A semantic set of colors for shell scripts. + +![colors](./assets/print_colors.png) + +Try this in your script : + +```bash +#!/bin/bash + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/colors/colors.sh + +echo_primary 'This is primary text (97m)' +echo_secondary 'This is secondary text (94m)' +echo_success 'This is a success (32m)' +echo_danger 'This is a danger (31m)' +echo_warning 'This is a warning (33m)' +echo_info 'This is an info (95m)' +echo_light 'This is light (47;90m)' +echo_dark 'This is dark (40;37m)' +echo +echo_label 9 'label (32m):'; echo_primary 'This is a label' +echo_error 'This is an error (91m)' +``` + +![echo_colors](./assets/echo_colors.png) + +```bash +#!/bin/bash + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/colors/colors.sh + +alert_primary 'This is a primary alert (1;104;97m)' +alert_secondary 'This is a secondary alert (1;45;97m)' +alert_success 'This is a success alert (1;42;97m)' +alert_danger 'This is a danger alert (1;41;97m)' +alert_warning 'This is a warning alert (1;43;97m)' +alert_info 'This is a info alert (1;44;97m)' +alert_light 'This is a light alert (1;47;90m)' +alert_dark 'This is a dark alert (1;40;37m)' +``` + +![alert_colors](./assets/alert_colors.png) + +### ⚑ get_latest_release.sh + +Get repository latest release from github. + +### ⚑ get_latest_tag.sh + +Get repository latest tag from github. + +### ⚑ download.sh + +Download file with either curl or wget. + +### ⚑ check_install.sh + +This script is meant to quickly check for common apps installation. + +![check_install](./assets/check_install.png) + +### ⚑ check_root.sh + +Throws error when user doesn't own root privileges. + +### ⚑ check_versions.sh + +Check version of some development tools. + +### ⚑ compare_versions.sh + +Compare two semantic versions. + +### ⚑ hosts.sh + +Edit your local `/etc/hosts` config from shell scripts. + +### ⚑ is_installed.sh + +Returns true when given application or command is available. + +### ⚑ is_ubuntu.sh + +Returns true when current operating system runs one of ubuntu version. + +### ⚑ prompt_user.sh + +Opens a user prompt for given value. + +### ⚑ update_grub.sh + +Updates GRUB on multiple patforms. + +### ⚑ get_parameter.sh + +Read parameter from .yaml file. + +### ⚑ set_parameter.sh + +Set parameter to .yaml file. + +πŸš€ Installation +--------------- + +Just copy this folder into your project or import this repository into your git project as a submodule. + +```bash +# with https +git submodule add https://github.com/TangoMan75/tools.git ./tools +# with ssh +git submodule add git@github.com:TangoMan75/tools.git ./tools +``` + +πŸ”₯ Usage +-------- + +Place following snippet in your code. + +```bash +#!/bin/bash + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/colors/colors.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/colors/colors_sf.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/colors/colors_v6.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/git/get_latest_release.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/git/get_latest_tag.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/network/download.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/check_install.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/check_root.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/check_versions.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/compare_versions.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/hosts.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/is_installed.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/is_ubuntu.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/prompt_user.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/system/update_grub.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/yaml/get_parameter.sh +# shellcheck source=/dev/null +. "${CURDIR}"/tools/src/yaml/set_parameter.sh +``` + +🀝 Contributing +--------------- + +Thank you for your interest in contributing to **TangoMan Bash Tools**. + +Please review the [code of conduct](./CODE_OF_CONDUCT.md) and [contribution guidelines](./CONTRIBUTING.md) before starting to work on any features. + +If you want to open an issue, please check first if it was not [reported already](https://github.com/TangoMan75/tools/issues) before creating a new one. + +πŸ“œ License +---------- + +Copyrights (c) 2021 "Matthias Morin" <mat@tangoman.io> + +[![License](https://img.shields.io/badge/Licence-MIT-green.svg)](LICENSE) +Distributed under the MIT license. + +If you like **TangoMan Bash Tools** please star, follow or tweet: + +[![GitHub stars](https://img.shields.io/github/stars/TangoMan75/tools?style=social)](https://github.com/TangoMan75/tools/stargazers) +[![GitHub followers](https://img.shields.io/github/followers/TangoMan75?style=social)](https://github.com/TangoMan75) +[![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2FTangoMan75%2Ftools)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2FTangoMan75%2Ftools) + +... And check my other cool projects. diff --git a/assets/alert_colors.png b/assets/alert_colors.png new file mode 100644 index 0000000..e541257 Binary files /dev/null and b/assets/alert_colors.png differ diff --git a/assets/check_install.png b/assets/check_install.png new file mode 100644 index 0000000..3d06147 Binary files /dev/null and b/assets/check_install.png differ diff --git a/assets/echo_colors.png b/assets/echo_colors.png new file mode 100644 index 0000000..48396ff Binary files /dev/null and b/assets/echo_colors.png differ diff --git a/assets/print_colors.png b/assets/print_colors.png new file mode 100644 index 0000000..d5c2705 Binary files /dev/null and b/assets/print_colors.png differ diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..51c4432 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,310 @@ +#!/bin/sh +set -e + +#/* +# * This file is part of TangoMan Entrypoint package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan CI Manager +# * +# * Run Continuous Integration +# * +# * @license MIT +# * @author "Matthias Morin" +# * @version 2.4.1-light +# * @link https://github.com/TangoMan75/entrypoint +# */ + +## Return staged bash files +get_staged_bash_files() { + if [ ! -x "$(command -v git)" ]; then + echo_error "\"$(basename "${0}")\" requires git, try: 'sudo apt-get install -y git'\n" + exit 1 + fi + + if git rev-parse --verify HEAD >/dev/null 2>&1; then + against=HEAD + else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) + fi + + # --diff-filter + # A Added + # C Copied + # M Modified + # R Renamed + git diff-index --cached --name-only --diff-filter=ACMR "${against}" | grep \ + -e '\.bashrc' \ + -e '\.zshrc' \ + -e '\.bash_profile' \ + -e '\.sh$' || true +} + +## Return staged php files +get_staged_php_files() { + if [ ! -x "$(command -v git)" ]; then + echo_error "\"$(basename "${0}")\" requires git, try: 'sudo apt-get install -y git'\n" + exit 1 + fi + + if git rev-parse --verify HEAD >/dev/null 2>&1; then + against=HEAD + else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) + fi + + # --diff-filter + # A Added + # C Copied + # M Modified + # R Renamed + git diff-index --cached --name-only --diff-filter=ACMR "${against}" | grep -e '\.php$' +} + +## Install git hooks +hooks() { + echo_info 'rm -fr .git/hooks\n' + rm -fr .git/hooks + + echo_info 'cp -r .githooks .git/hooks\n' + cp -r .githooks .git/hooks + + echo_info 'chmod +x .git/hooks/*\n' + chmod +x .git/hooks/* +} + +## Install bash_unit framework +install() { + if [ ! -d ./tests ]; then + mkdir -p ./tests + + cat > "./tests/test_sample.sh" <3) printf "%s ", substr($0, 5)}' "$0" +} + +_get_docbloc() { + awk -v TAG="$1" '/^#\/\*\*$/,/^# \*\/$/{if($3=="@"TAG){for(i=4;i<=NF;++i){printf "%s ",$i}}}' "$0" +} + +_get_padding() { + awk -F '=' '/^[a-zA-Z0-9_]+=.+$/ { MATCH = $1 } + /^(function )? *[a-zA-Z0-9_]+ *\(\) *\{/ { + sub("^function ",""); gsub("[ ()]",""); + MATCH = substr($0, 1, index($0, "{")); + sub("{$", "", MATCH); + } { if (substr(PREV, 1, 3) == "## " && substr(MATCH, 1, 1) != "_" && length(MATCH) > LENGTH) LENGTH = length(MATCH) } + { PREV = $0 } END { print LENGTH+1 }' "$0" +} + +#-------------------------------------------------- + +_get_functions() { + # this regular expression matches functions with either bash or sh syntax + awk '/^(function )? *[a-zA-Z0-9_]+ *\(\) *\{/ { + sub("^function ",""); gsub("[ ()]",""); # remove leading "function ", round brackets and extra spaces + FUNCTION = substr($0, 1, index($0, "{")); # truncate string after opening curly brace + sub("{$", "", FUNCTION); # remove trailing curly brace + if (substr(PREV, 1, 3) == "## " && substr($0, 1, 1) != "_") print FUNCTION + } { PREV = $0 }' "$0" +} + +#-------------------------------------------------- + +_main() { + if [ $# -lt 1 ]; then + help + exit 0 + fi + + _execute='' + for _argument in "$@"; do + _is_valid=false + for _function in $(_get_functions); do + # get shorthand character + _shorthand="$(echo "${_function}" | awk '{$0=substr($0, 1, 1); print}')" + if [ "${_argument}" = "${_function}" ] || [ "${_argument}" = "${_shorthand}" ]; then + # append argument to the execute stack + _execute="${_execute} ${_function}" + _is_valid=true + break + fi + done + if [ "${_is_valid}" = false ]; then + printf '\033[1;31merror:\t\033[0;91m"%s" is not a valid command\033[0m\n' "${_argument}" + help + exit 1 + fi + done + + for _function in ${_execute}; do + eval "${_function}" + done +} + +_main "$@" diff --git a/src/colors/colors.sh b/src/colors/colors.sh new file mode 100755 index 0000000..b5dfb71 --- /dev/null +++ b/src/colors/colors.sh @@ -0,0 +1,81 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 7.0.2 +# */ + +# shellcheck disable=SC2034 +{ + PRIMARY='\033[97m'; SECONDARY='\033[94m'; SUCCESS='\033[32m'; DANGER='\033[31m'; WARNING='\033[33m'; INFO='\033[95m'; LIGHT='\033[47;90m'; DARK='\033[40;37m'; DEFAULT='\033[0m'; NL='\033[0m\n'; + ALERT_PRIMARY='\033[1;104;97m'; ALERT_SECONDARY='\033[1;45;97m'; ALERT_SUCCESS='\033[1;42;97m'; ALERT_DANGER='\033[1;41;97m'; ALERT_WARNING='\033[1;43;97m'; ALERT_INFO='\033[1;44;97m'; ALERT_LIGHT='\033[1;47;90m'; ALERT_DARK='\033[1;40;37m'; +} + +## Print primary (bright white text) +echo_primary() { printf "%b%b${NL}" "${PRIMARY}" "${*}"; } + +## Print secondary (bright blue text) +echo_secondary() { printf "%b%b${NL}" "${SECONDARY}" "${*}"; } + +## Print success (bright green text) +echo_success() { printf "%b%b${NL}" "${SUCCESS}" "${*}"; } + +## Print danger (red text) +echo_danger() { printf "%b%b${NL}" "${DANGER}" "${*}"; } + +## Print warning (orange text) +echo_warning() { printf "%b%b${NL}" "${WARNING}" "${*}"; } + +## Print info (bright purple text) +echo_info() { printf "%b%b${NL}" "${INFO}" "${*}"; } + +## Print light (black text over white background) +echo_light() { printf "%b%b${NL}" "${LIGHT}" "${*}"; } + +## Print dark (white text over black background) +echo_dark() { printf "%b%b${NL}" "${DARK}" "${*}"; } + +## Print label (green text with optional padding, no carriage return) +echo_label() { if [ $# -eq 2 ]; then printf "%b%-${1}s ${DEFAULT}" "${SUCCESS}" "$2"; else printf "%b%b ${DEFAULT}" "${SUCCESS}" "${*}"; fi } + +## Print error (red text, prefixed 'error:') +echo_error() { printf "%berror: %b${NL}" "${DANGER}" "${*}"; } + +## Print primary alert (bold white text over bright blue background) +alert_primary() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_PRIMARY}" '' "${ALERT_PRIMARY}" "${*}" "${ALERT_PRIMARY}" ''; } + +## Print secondary alert (bold white text over bright purple background) +alert_secondary() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_SECONDARY}" '' "${ALERT_SECONDARY}" "${*}" "${ALERT_SECONDARY}" ''; } + +## Print success alert (bold white text over bright green background) +alert_success() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_SUCCESS}" '' "${ALERT_SUCCESS}" "${*}" "${ALERT_SUCCESS}" ''; } + +## Print danger alert (bold white text over bright red background) +alert_danger() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_DANGER}" '' "${ALERT_DANGER}" "${*}" "${ALERT_DANGER}" ''; } + +## Print warning alert (bold white text over bright orange background) +alert_warning() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_WARNING}" '' "${ALERT_WARNING}" "${*}" "${ALERT_WARNING}" ''; } + +## Print info alert (bold white text over bright blue background) +alert_info() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_INFO}" '' "${ALERT_INFO}" "${*}" "${ALERT_INFO}" ''; } + +## Print light alert (black text over white background) +alert_light() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_LIGHT}" '' "${ALERT_LIGHT}" "${*}" "${ALERT_LIGHT}" ''; } + +## Print dark alert (bold white text over black background) +alert_dark() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_DARK}" '' "${ALERT_DARK}" "${*}" "${ALERT_DARK}" ''; } diff --git a/src/colors/colors_demo_sf.sh b/src/colors/colors_demo_sf.sh new file mode 100755 index 0000000..a76c00d --- /dev/null +++ b/src/colors/colors_demo_sf.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors Demo +# * +# * A demo to test colors +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# */ + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}/colors_sf.sh" + +padding=9 + +function help() { + alert_secondary 'Symfony Colors' + echo_warning "$(basename "$0" .sh)" + echo_primary ' A semantic set of colors for shell scripts inspired by Bootstrap and Symfony' + echo + echo_warning 'Description' + echo_label "${padding}" " ${FUNCNAME[0]}"; echo_primary 'Print help message' + echo_label "${padding}" ' command'; echo_primary 'This command does nothing' + echo_label "${padding}" ' foo'; echo_primary 'This command does foo' + echo_label "${padding}" ' bar'; echo_primary 'This command does bar' + echo +} + +function demo_echo() { + echo_primary 'This is primary text (97m)' + echo_secondary 'This is secondary text (94m)' + echo_success 'This is a success (32m)' + echo_danger 'This is a danger (31m)' + echo_warning 'This is a warning (33m)' + echo_info 'This is an info (95m)' + echo_light 'This is light (47;90m)' + echo_dark 'This is dark (40;37m)' + echo + echo_label "${padding}" 'label (32m):'; echo_primary 'This is a label' + echo_error '(1;31m) This is an error (91m)' + echo +} + +function demo_alert() { + alert_primary 'This is a primary alert (104;97m)' + alert_secondary 'This is a secondary alert (45;37m)' + alert_success 'This is a success alert (42;30m)' + alert_danger 'This is a danger alert (41;37m)' + alert_warning 'This is a warning alert (43;30m)' + alert_info 'This is a info alert (44;37m)' + alert_light 'This is a light alert (47;90m)' + alert_dark 'This is a dark alert (40;37m)' +} + +help +demo_echo +demo_alert + diff --git a/src/colors/colors_demo_v5.sh b/src/colors/colors_demo_v5.sh new file mode 100755 index 0000000..50755e8 --- /dev/null +++ b/src/colors/colors_demo_v5.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors Demo +# * +# * A demo to test colors +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# */ + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}/colors_v5.sh" + +function help() { + alert_secondary 'TangoMan Colors' + echo_title "$(basename "$0" .sh)" + echo_primary ' A semantic set of colors for shell scripts inspired by Bootstrap and Symfony' + echo + echo_title 'Description' + echo_label " ${FUNCNAME[0]}: "; echo_primary 'Print help message' + echo_label ' command:'; echo_primary 'This command does nothing' + echo_label ' foo: '; echo_primary 'This command does foo' + echo_label ' bar: '; echo_primary 'This command does bar' + echo +} + +function demo_echo() { + echo_title 'This is a title (33m)' + echo_primary 'This is primary text (97m)' + echo_secondary 'This is secondary text (96m)' + echo_success 'This is a success (92m)' + echo_danger 'This is a danger (31m)' + echo_warning 'This is a warning (93m)' + echo_info 'This is a info (95m)' + echo_light 'This is light (47;90m)' + echo_dark 'This is dark (40;37m)' + echo_muted 'This is muted (37m)' + + echo_prompt 'Is this a prompt (96m) ?'; echo_muted '[y/n]' + echo_label 'label (32m):'; echo_muted 'This is a label' + echo_error '(1;31m) This is an error (91m)' + echo +} + +function demo_alert() { + alert_primary 'This is a primary alert (1;104;97m)' + alert_secondary 'This is a secondary alert (1;45;97m)' + alert_success 'This is a success alert (1;42;37m)' + alert_danger 'This is a danger alert (1;41;37m)' + alert_warning 'This is a warning alert (1;43;97m)' + alert_info 'This is a info alert (1;44;97m)' + alert_light 'This is a light alert (1;47;90m)' + alert_dark 'This is a dark alert (1;40;37m)' +} + +help +demo_echo +demo_alert + diff --git a/src/colors/colors_demo_v6.sh b/src/colors/colors_demo_v6.sh new file mode 100755 index 0000000..db282d7 --- /dev/null +++ b/src/colors/colors_demo_v6.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors Demo +# * +# * A demo to test colors +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# */ + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}/colors_v6.sh" + +padding=9 + +function help() { + alert_secondary 'TangoMan Colors' + echo_warning "$(basename "$0" .sh)" + echo_primary ' A semantic set of colors for shell scripts inspired by Bootstrap and Symfony' + echo + echo_warning 'Description' + echo_label "${padding}" " ${FUNCNAME[0]}"; echo_primary 'Print help message' + echo_label "${padding}" ' command'; echo_primary 'This command does nothing' + echo_label "${padding}" ' foo'; echo_primary 'This command does foo' + echo_label "${padding}" ' bar'; echo_primary 'This command does bar' + echo +} + +function demo_echo() { + echo_primary 'This is primary text (97m)' + echo_secondary 'This is secondary text (94m)' + echo_success 'This is a success (32m)' + echo_danger 'This is a danger (31m)' + echo_warning 'This is a warning (33m)' + echo_info 'This is an info (95m)' + echo_light 'This is light (47;90m)' + echo_dark 'This is dark (40;37m)' + echo + echo_label "${padding}" 'label (32m):'; echo_primary 'This is a label' + echo_error '(1;31m) This is an error (91m)' + echo +} + +function demo_alert() { + alert_primary 'This is a primary alert (1;104;97m)' + alert_secondary 'This is a secondary alert (1;45;97m)' + alert_success 'This is a success alert (1;42;97m)' + alert_danger 'This is a danger alert (1;41;97m)' + alert_warning 'This is a warning alert (1;43;97m)' + alert_info 'This is a info alert (1;44;97m)' + alert_light 'This is a light alert (1;47;90m)' + alert_dark 'This is a dark alert (1;40;37m)' +} + +help +demo_echo +demo_alert + diff --git a/src/colors/colors_demo_v7.sh b/src/colors/colors_demo_v7.sh new file mode 100755 index 0000000..0105011 --- /dev/null +++ b/src/colors/colors_demo_v7.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors Demo +# * +# * A demo to test colors +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# */ + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}/colors_v7.sh" + +padding=9 + +function help() { + alert_secondary 'TangoMan Colors' + echo_warning "$(basename "$0" .sh)" + echo_primary ' A semantic set of colors for shell scripts inspired by Bootstrap and Symfony' + echo + echo_warning 'Description' + echo_label "${padding}" " ${FUNCNAME[0]}"; echo_primary 'Print help message' + echo_label "${padding}" ' command'; echo_primary 'This command does nothing' + echo_label "${padding}" ' foo'; echo_primary 'This command does foo' + echo_label "${padding}" ' bar'; echo_primary 'This command does bar' + echo +} + +function demo_echo() { + echo_primary 'This is primary text (97m)' + echo_secondary 'This is secondary text (94m)' + echo_success 'This is a success (32m)' + echo_danger 'This is a danger (31m)' + echo_warning 'This is a warning (33m)' + echo_info 'This is an info (95m)' + echo_light 'This is light (47;90m)' + echo_dark 'This is dark (40;37m)' + echo + echo_label "${padding}" 'label (32m):'; echo_primary 'This is a label' + echo_error 'This is an error (31m)' + echo +} + +function demo_alert() { + alert_primary 'This is a primary alert (1;104;97m)' + alert_secondary 'This is a secondary alert (1;45;97m)' + alert_success 'This is a success alert (1;42;97m)' + alert_danger 'This is a danger alert (1;41;97m)' + alert_warning 'This is a warning alert (1;43;97m)' + alert_info 'This is a info alert (1;44;97m)' + alert_light 'This is a light alert (1;47;90m)' + alert_dark 'This is a dark alert (1;40;37m)' +} + +help +demo_echo +demo_alert + diff --git a/src/colors/colors_sf.sh b/src/colors/colors_sf.sh new file mode 100755 index 0000000..9407873 --- /dev/null +++ b/src/colors/colors_sf.sh @@ -0,0 +1,76 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony (minified) +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 6.0.0-sf +# */ + +## Print primary (bright white text) +echo_primary() { printf '\033[97m%b\033[0m\n' "${*}"; } + +## Print secondary (bright blue text) +echo_secondary() { printf '\033[94m%b\033[0m\n' "${*}"; } + +## Print success (green text) +echo_success() { printf '\033[32m%b\033[0m\n' "${*}"; } + +## Print danger (red text) +echo_danger() { printf '\033[31m%b\033[0m\n' "${*}"; } + +## Print warning (orange text) +echo_warning() { printf '\033[33m%b\033[0m\n' "${*}"; } + +## Print info (bright purple text) +echo_info() { printf '\033[95m%b\033[0m\n' "${*}"; } + +## Print light (black text over white background) +echo_light() { printf '\033[47;90m%b\033[0m\n' "${*}"; } + +## Print dark (white text over black background) +echo_dark() { printf '\033[40;37m%b\033[0m\n' "${*}"; } + +## Print label (green text with padding, no carriage return) +echo_label() { printf '\033[32m%-*b \033[0m' "$1" "$2"; } + +## Print error (bright red text, prefixed with bold red 'error:') +echo_error() { printf '\033[1;31merror:\t\033[0;91m%s\033[0m\n' "${*}"; } + +## Print primary alert (bright white text over bright blue background) +alert_primary() { printf "\033[0m\n\033[104;97m%64s\033[0m\n\033[104;97m %-63s\033[0m\n\033[104;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print secondary alert (white text over bright purple background) +alert_secondary() { printf "\033[0m\n\033[45;37m%64s\033[0m\n\033[45;37m %-63s\033[0m\n\033[45;37m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print success alert (black text over bright green background) +alert_success() { printf "\033[0m\n\033[42;30m%64s\033[0m\n\033[42;30m %-63s\033[0m\n\033[42;30m%64s\033[0m\n\n" '' "[OK] ${*}" ''; } + +## Print danger alert (white text over bright red background) +alert_danger() { printf "\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\n" '!' "! [ERROR] ${*}" '!'; } + +## Print warning alert (white text over bright orange background) +alert_warning() { printf "\033[0m\n\033[43;30m%64s\033[0m\n\033[43;30m %-63s\033[0m\n\033[43;30m%64s\033[0m\n\n" '' "[WARNING] ${*}" ''; } + +## Print info alert (white text over bright blue background) +alert_info() { printf "\033[0m\n\033[44;37m%64s\033[0m\n\033[44;37m %-63s\033[0m\n\033[44;37m%64s\033[0m\n\n" '' "[INFO] ${*}" ''; } + +## Print light alert (black text over white background) +alert_light() { printf "\033[0m\n\033[47;90m%64s\033[0m\n\033[47;90m %-63s\033[0m\n\033[47;90m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print dark alert (white text over black background) +alert_dark() { printf "\033[0m\n\033[40;37m%64s\033[0m\n\033[40;37m %-63s\033[0m\n\033[40;37m%64s\033[0m\n\n" '' "${*}" ''; } + diff --git a/src/colors/colors_sfb.sh b/src/colors/colors_sfb.sh new file mode 100755 index 0000000..5056de8 --- /dev/null +++ b/src/colors/colors_sfb.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony (minified) +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 6.0.0-sf-b +# */ + +echo_primary() { printf '\033[97m%b\033[0m' "${*}"; } +echo_secondary() { printf '\033[94m%b\033[0m' "${*}"; } +echo_success() { printf '\033[32m%b\033[0m' "${*}"; } +echo_danger() { printf '\033[31m%b\033[0m' "${*}"; } +echo_warning() { printf '\033[33m%b\033[0m' "${*}"; } +echo_info() { printf '\033[95m%b\033[0m' "${*}"; } +echo_light() { printf '\033[47;90m%b\033[0m' "${*}"; } +echo_dark() { printf '\033[40;37m%b\033[0m' "${*}"; } + +echo_label() { printf '\033[32m%-*b \033[0m' "$1" "$2"; } +echo_error() { printf '\033[1;31merror:\t\033[0;91m%s\033[0m' "${*}"; } + +alert_primary() { printf "\033[0m\n\033[104;37m%64s\033[0m\n\033[104;37m %-63s\033[0m\n\033[104;37m%64s\033[0m\n\n" '' "${*}" ''; } +alert_secondary() { printf "\033[0m\n\033[45;37m%64s\033[0m\n\033[45;37m %-63s\033[0m\n\033[45;37m%64s\033[0m\n\n" '' "${*}" ''; } +alert_success() { printf "\033[0m\n\033[42;30m%64s\033[0m\n\033[42;30m %-63s\033[0m\n\033[42;30m%64s\033[0m\n\n" '' "[OK] ${*}" ''; } +alert_danger() { printf "\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\n" '!' "! [ERROR] ${*}" '!'; } +alert_warning() { printf "\033[0m\n\033[43;37m%64s\033[0m\n\033[43;37m %-63s\033[0m\n\033[43;37m%64s\033[0m\n\n" '' "[WARNING] ${*}" ''; } +alert_info() { printf "\033[0m\n\033[44;37m%64s\033[0m\n\033[44;37m %-63s\033[0m\n\033[44;37m%64s\033[0m\n\n" '' "[INFO] ${*}" ''; } +alert_light() { printf "\033[0m\n\033[47;90m%64s\033[0m\n\033[47;90m %-63s\033[0m\n\033[47;90m%64s\033[0m\n\n" '' "${*}" ''; } +alert_dark() { printf "\033[0m\n\033[40;37m%64s\033[0m\n\033[40;37m %-63s\033[0m\n\033[40;37m%64s\033[0m\n\n" '' "${*}" ''; } diff --git a/src/colors/colors_v1.sh b/src/colors/colors_v1.sh new file mode 100755 index 0000000..9c72f38 --- /dev/null +++ b/src/colors/colors_v1.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 1.0.0 +# */ + +# colors v1 +function echo_title() { echo -ne "\033[0;1;42m${*}\033[0m\n"; } +function echo_caption() { echo -ne "\033[0;1;44m${*}\033[0m\n"; } +function echo_bold() { echo -ne "\033[0;1;34m${*}\033[0m\n"; } +function echo_danger() { echo -ne "\033[0;31m${*}\033[0m\n"; } +function echo_success() { echo -ne "\033[0;32m${*}\033[0m\n"; } +function echo_warning() { echo -ne "\033[0;33m${*}\033[0m\n"; } +function echo_secondary() { echo -ne "\033[0;34m${*}\033[0m\n"; } +function echo_info() { echo -ne "\033[0;35m${*}\033[0m\n"; } +function echo_primary() { echo -ne "\033[0;36m${*}\033[0m\n"; } +function echo_error() { echo -ne "\033[0;1;31merror:\033[0;31m\t${*}\033[0m\n"; } +function echo_label() { echo -ne "\033[0;1;32m${*}:\033[0m\t"; } +function echo_prompt() { echo -ne "\033[0;36m${*}\033[0m "; } diff --git a/src/colors/colors_v2.sh b/src/colors/colors_v2.sh new file mode 100755 index 0000000..be47fd5 --- /dev/null +++ b/src/colors/colors_v2.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 2.0.0 +# */ + +# colors v2 +function echo_title() { echo -ne "\033[1;44;97m${*}\033[0m\n"; } +function echo_caption() { echo -ne "\033[1;46;97m${*}\033[0m\n"; } +function echo_primary() { echo -ne "\033[96m${*}\033[0m\n"; } +function echo_secondary() { echo -ne "\033[94m${*}\033[0m\n"; } +function echo_success() { echo -ne "\033[92m${*}\033[0m\n"; } +function echo_danger() { echo -ne "\033[31m${*}\033[0m\n"; } +function echo_warning() { echo -ne "\033[33m${*}\033[0m\n"; } +function echo_info() { echo -ne "\033[95m${*}\033[0m\n"; } +function echo_muted() { echo -ne "\033[37m${*}\033[0m\n"; } +function echo_light() { echo -ne "\033[47;90m${*}\033[0m\n"; } +function echo_dark() { echo -ne "\033[40;37m${*}\033[0m\n"; } +function echo_prompt() { echo -ne "\033[0;36m${*}\033[0m "; } +function echo_label() { echo -ne "\033[1;32m${*}:\033[0m\t"; } +function echo_error() { echo -ne "\033[1;31merror:\t\033[0;31m${*}\033[0m\n"; } +function echo_splash() { local hr; hr=" **$(printf "%${#1}s" | tr ' ' '*')** "; printf "\033[1;46;37m%s\033[0m\n" "${hr}"; printf "\033[1;46;37m * %s * \033[0m\n" "$1"; printf "\033[1;46;37m%s\033[0m\n$(tput el)\n" "${hr}"; } diff --git a/src/colors/colors_v3.sh b/src/colors/colors_v3.sh new file mode 100755 index 0000000..456a2aa --- /dev/null +++ b/src/colors/colors_v3.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 3.0.0 +# */ + +# colors v3 +function echo_title() { echo -ne "\033[33m${*}\033[0m\n"; } +function echo_primary() { echo -ne "\033[97m${*}\033[0m\n"; } +function echo_secondary() { echo -ne "\033[96m${*}\033[0m\n"; } +function echo_success() { echo -ne "\033[92m${*}\033[0m\n"; } +function echo_danger() { echo -ne "\033[31m${*}\033[0m\n"; } +function echo_warning() { echo -ne "\033[93m${*}\033[0m\n"; } +function echo_info() { echo -ne "\033[95m${*}\033[0m\n"; } +function echo_light() { echo -ne "\033[47;90m${*}\033[0m\n"; } +function echo_dark() { echo -ne "\033[40;37m${*}\033[0m\n"; } +function echo_muted() { echo -ne "\033[37m${*}\033[0m\n"; } +function echo_prompt() { echo -ne "\033[96m${*}\033[0m " ; } +function echo_label() { echo -ne "\033[32m${*}\033[0m " ; } +function echo_error() { echo -ne "\033[1;31merror:\t\033[0;91m${*}\033[0m\n"; } + +function alert_primary() { printf "\033[0m\n\033[1;104;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_secondary() { printf "\033[0m\n\033[1;45;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_success() { printf "\033[0m\n\033[1;42;37m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_danger() { printf "\033[0m\n\033[1;41;37m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_warning() { printf "\033[0m\n\033[1;43;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_info() { printf "\033[0m\n\033[1;44;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_light() { printf "\033[0m\n\033[1;47;90m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +function alert_dark() { printf "\033[0m\n\033[1;40;37m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } diff --git a/src/colors/colors_v4.sh b/src/colors/colors_v4.sh new file mode 100755 index 0000000..0ff9d0b --- /dev/null +++ b/src/colors/colors_v4.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 4.0.0 +# */ + +# colors v4 +echo_title() { printf "\033[33m%s\033[0m\n" "${*}"; } +echo_primary() { printf "\033[97m%s\033[0m\n" "${*}"; } +echo_secondary() { printf "\033[96m%s\033[0m\n" "${*}"; } +echo_success() { printf "\033[92m%s\033[0m\n" "${*}"; } +echo_danger() { printf "\033[31m%s\033[0m\n" "${*}"; } +echo_warning() { printf "\033[93m%s\033[0m\n" "${*}"; } +echo_info() { printf "\033[95m%s\033[0m\n" "${*}"; } +echo_light() { printf "\033[47;90m%s\033[0m\n" "${*}"; } +echo_dark() { printf "\033[40;37m%s\033[0m\n" "${*}"; } +echo_muted() { printf "\033[37m%s\033[0m\n" "${*}"; } +echo_prompt() { printf "\033[96m%s\033[0m " "${*}"; } +echo_label() { printf "\033[32m%s\033[0m " "${*}"; } +echo_error() { printf "\033[1;31merror:\t\033[0;91m%s\033[0m\n" "${*}"; } + +alert_primary() { printf "\033[0m\n\033[1;104;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_secondary() { printf "\033[0m\n\033[1;45;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_success() { printf "\033[0m\n\033[1;42;37m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_danger() { printf "\033[0m\n\033[1;41;37m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_warning() { printf "\033[0m\n\033[1;43;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_info() { printf "\033[0m\n\033[1;44;97m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_light() { printf "\033[0m\n\033[1;47;90m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } +alert_dark() { printf "\033[0m\n\033[1;40;37m$(tput el)\n%4s${*}$(tput el)\n$(tput el)\n\033[0m$(tput el)\n"; } diff --git a/src/colors/colors_v5.sh b/src/colors/colors_v5.sh new file mode 100755 index 0000000..30a671a --- /dev/null +++ b/src/colors/colors_v5.sh @@ -0,0 +1,84 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 5.0.0 +# */ + +## Print title (orange text) +echo_title() { printf "\033[33m%s\033[0m\n" "${*}"; } + +## Print primary (bright white text) +echo_primary() { printf "\033[97m%s\033[0m\n" "${*}"; } + +## Print secondary (bright cyan text) +echo_secondary() { printf "\033[96m%s\033[0m\n" "${*}"; } + +## Print success (bright green text) +echo_success() { printf "\033[92m%s\033[0m\n" "${*}"; } + +## Print danger (red text) +echo_danger() { printf "\033[31m%s\033[0m\n" "${*}"; } + +## Print warning (bright orange text) +echo_warning() { printf "\033[93m%s\033[0m\n" "${*}"; } + +## Print info (bright purple text) +echo_info() { printf "\033[95m%s\033[0m\n" "${*}"; } + +## Print light (black text over white background) +echo_light() { printf "\033[47;90m%s\033[0m\n" "${*}"; } + +## Print dark (white text over black background) +echo_dark() { printf "\033[40;37m%s\033[0m\n" "${*}"; } + +## Print muted (grey text) +echo_muted() { printf "\033[37m%s\033[0m\n" "${*}"; } + +## Print prompt (bright cyan text, no carriage return) +echo_prompt() { printf "\033[96m%s\033[0m " "${*}"; } + +## Print label (green text, no carriage return) +echo_label() { printf "\033[32m%s\033[0m " "${*}"; } + +## Print error (bright red text, no carriage return, prefixed with bold red 'error:') +echo_error() { printf "\033[1;31merror:\t\033[0;91m%s\033[0m\n" "${*}"; } + +## Print primary alert (white text over bright blue background) +alert_primary() { printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print secondary alert (white text over bright purple background) +alert_secondary() { printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print success alert (white text over bright green background) +alert_success() { printf "\033[0m\n\033[1;42;37m%64s\033[0m\n\033[1;42;37m %-63s\033[0m\n\033[1;42;37m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print danger alert (white text over bright red background) +alert_danger() { printf "\033[0m\n\033[1;41;37m%64s\033[0m\n\033[1;41;37m %-63s\033[0m\n\033[1;41;37m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print warning alert (white text over bright orange background) +alert_warning() { printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print info alert (white text over bright blue background) +alert_info() { printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print light alert (black text over white background) +alert_light() { printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print dark alert (white text over black background) +alert_dark() { printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "${*}" ''; } diff --git a/src/colors/colors_v6.sh b/src/colors/colors_v6.sh new file mode 100755 index 0000000..71e1ab4 --- /dev/null +++ b/src/colors/colors_v6.sh @@ -0,0 +1,75 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version v6.0.2 +# */ + +## Print primary (bright white text) +echo_primary() { printf "\033[97m%b\033[0m\n" "${*}"; } + +## Print secondary (bright blue text) +echo_secondary() { printf "\033[94m%b\033[0m\n" "${*}"; } + +## Print success (bright green text) +echo_success() { printf "\033[32m%b\033[0m\n" "${*}"; } + +## Print danger (red text) +echo_danger() { printf "\033[31m%b\033[0m\n" "${*}"; } + +## Print warning (orange text) +echo_warning() { printf "\033[33m%b\033[0m\n" "${*}"; } + +## Print info (bright purple text) +echo_info() { printf "\033[95m%b\033[0m\n" "${*}"; } + +## Print light (black text over white background) +echo_light() { printf "\033[47;90m%b\033[0m\n" "${*}"; } + +## Print dark (white text over black background) +echo_dark() { printf "\033[40;37m%b\033[0m\n" "${*}"; } + +## Print label (green text with padding, no carriage return) +echo_label() { printf "\033[32m%-${1}b \033[0m" "$2"; } + +## Print error (bright red text, prefixed with bold red 'error:') +echo_error() { printf '\033[1;31merror:\t\033[0;91m%s\033[0m\n' "${*}"; } + +## Print primary alert (bold white text over bright blue background) +alert_primary() { printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print secondary alert (bold white text over bright purple background) +alert_secondary() { printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print success alert (bold white text over bright green background) +alert_success() { printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print danger alert (bold white text over bright red background) +alert_danger() { printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print warning alert (bold white text over bright orange background) +alert_warning() { printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print info alert (bold white text over bright blue background) +alert_info() { printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print light alert (black text over white background) +alert_light() { printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "${*}" ''; } + +## Print dark alert (bold white text over black background) +alert_dark() { printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "${*}" ''; } diff --git a/src/colors/colors_v6b.sh b/src/colors/colors_v6b.sh new file mode 100755 index 0000000..0197bfc --- /dev/null +++ b/src/colors/colors_v6b.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 6.0.2-b +# */ + +echo_primary() { printf "\033[97m%b\033[0m" "${*}"; } +echo_secondary() { printf "\033[94m%b\033[0m" "${*}"; } +echo_success() { printf "\033[32m%b\033[0m" "${*}"; } +echo_danger() { printf "\033[31m%b\033[0m" "${*}"; } +echo_warning() { printf "\033[33m%b\033[0m" "${*}"; } +echo_info() { printf "\033[95m%b\033[0m" "${*}"; } +echo_light() { printf "\033[47;90m%b\033[0m" "${*}"; } +echo_dark() { printf "\033[40;37m%b\033[0m" "${*}"; } + +echo_label() { printf "\033[32m%-${1}b \033[0m" "$2"; } +echo_error() { printf '\033[1;31merror:\t\033[0;91m%s\033[0m' "${*}"; } + +alert_primary() { printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "${*}" ''; } +alert_secondary() { printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "${*}" ''; } +alert_success() { printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "${*}" ''; } +alert_danger() { printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "${*}" ''; } +alert_warning() { printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "${*}" ''; } +alert_info() { printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "${*}" ''; } +alert_light() { printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "${*}" ''; } +alert_dark() { printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "${*}" ''; } diff --git a/src/colors/colors_v7.sh b/src/colors/colors_v7.sh new file mode 100755 index 0000000..b5dfb71 --- /dev/null +++ b/src/colors/colors_v7.sh @@ -0,0 +1,81 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 7.0.2 +# */ + +# shellcheck disable=SC2034 +{ + PRIMARY='\033[97m'; SECONDARY='\033[94m'; SUCCESS='\033[32m'; DANGER='\033[31m'; WARNING='\033[33m'; INFO='\033[95m'; LIGHT='\033[47;90m'; DARK='\033[40;37m'; DEFAULT='\033[0m'; NL='\033[0m\n'; + ALERT_PRIMARY='\033[1;104;97m'; ALERT_SECONDARY='\033[1;45;97m'; ALERT_SUCCESS='\033[1;42;97m'; ALERT_DANGER='\033[1;41;97m'; ALERT_WARNING='\033[1;43;97m'; ALERT_INFO='\033[1;44;97m'; ALERT_LIGHT='\033[1;47;90m'; ALERT_DARK='\033[1;40;37m'; +} + +## Print primary (bright white text) +echo_primary() { printf "%b%b${NL}" "${PRIMARY}" "${*}"; } + +## Print secondary (bright blue text) +echo_secondary() { printf "%b%b${NL}" "${SECONDARY}" "${*}"; } + +## Print success (bright green text) +echo_success() { printf "%b%b${NL}" "${SUCCESS}" "${*}"; } + +## Print danger (red text) +echo_danger() { printf "%b%b${NL}" "${DANGER}" "${*}"; } + +## Print warning (orange text) +echo_warning() { printf "%b%b${NL}" "${WARNING}" "${*}"; } + +## Print info (bright purple text) +echo_info() { printf "%b%b${NL}" "${INFO}" "${*}"; } + +## Print light (black text over white background) +echo_light() { printf "%b%b${NL}" "${LIGHT}" "${*}"; } + +## Print dark (white text over black background) +echo_dark() { printf "%b%b${NL}" "${DARK}" "${*}"; } + +## Print label (green text with optional padding, no carriage return) +echo_label() { if [ $# -eq 2 ]; then printf "%b%-${1}s ${DEFAULT}" "${SUCCESS}" "$2"; else printf "%b%b ${DEFAULT}" "${SUCCESS}" "${*}"; fi } + +## Print error (red text, prefixed 'error:') +echo_error() { printf "%berror: %b${NL}" "${DANGER}" "${*}"; } + +## Print primary alert (bold white text over bright blue background) +alert_primary() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_PRIMARY}" '' "${ALERT_PRIMARY}" "${*}" "${ALERT_PRIMARY}" ''; } + +## Print secondary alert (bold white text over bright purple background) +alert_secondary() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_SECONDARY}" '' "${ALERT_SECONDARY}" "${*}" "${ALERT_SECONDARY}" ''; } + +## Print success alert (bold white text over bright green background) +alert_success() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_SUCCESS}" '' "${ALERT_SUCCESS}" "${*}" "${ALERT_SUCCESS}" ''; } + +## Print danger alert (bold white text over bright red background) +alert_danger() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_DANGER}" '' "${ALERT_DANGER}" "${*}" "${ALERT_DANGER}" ''; } + +## Print warning alert (bold white text over bright orange background) +alert_warning() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_WARNING}" '' "${ALERT_WARNING}" "${*}" "${ALERT_WARNING}" ''; } + +## Print info alert (bold white text over bright blue background) +alert_info() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_INFO}" '' "${ALERT_INFO}" "${*}" "${ALERT_INFO}" ''; } + +## Print light alert (black text over white background) +alert_light() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_LIGHT}" '' "${ALERT_LIGHT}" "${*}" "${ALERT_LIGHT}" ''; } + +## Print dark alert (bold white text over black background) +alert_dark() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_DARK}" '' "${ALERT_DARK}" "${*}" "${ALERT_DARK}" ''; } diff --git a/src/colors/colors_v7b.sh b/src/colors/colors_v7b.sh new file mode 100755 index 0000000..0458eab --- /dev/null +++ b/src/colors/colors_v7b.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 7.0.2-b +# */ + +# shellcheck disable=SC2034 +{ + PRIMARY='\033[97m'; SECONDARY='\033[94m'; SUCCESS='\033[32m'; DANGER='\033[31m'; WARNING='\033[33m'; INFO='\033[95m'; LIGHT='\033[47;90m'; DARK='\033[40;37m'; DEFAULT='\033[0m'; NL='\033[0m\n'; + ALERT_PRIMARY='\033[1;104;97m'; ALERT_SECONDARY='\033[1;45;97m'; ALERT_SUCCESS='\033[1;42;97m'; ALERT_DANGER='\033[1;41;97m'; ALERT_WARNING='\033[1;43;97m'; ALERT_INFO='\033[1;44;97m'; ALERT_LIGHT='\033[1;47;90m'; ALERT_DARK='\033[1;40;37m'; +} + +echo_primary() { printf "%b%b${DEFAULT}" "${PRIMARY}" "${*}"; } +echo_secondary() { printf "%b%b${DEFAULT}" "${SECONDARY}" "${*}"; } +echo_success() { printf "%b%b${DEFAULT}" "${SUCCESS}" "${*}"; } +echo_danger() { printf "%b%b${DEFAULT}" "${DANGER}" "${*}"; } +echo_warning() { printf "%b%b${DEFAULT}" "${WARNING}" "${*}"; } +echo_info() { printf "%b%b${DEFAULT}" "${INFO}" "${*}"; } +echo_light() { printf "%b%b${DEFAULT}" "${LIGHT}" "${*}"; } +echo_dark() { printf "%b%b${DEFAULT}" "${DARK}" "${*}"; } + +echo_label() { if [ $# -eq 2 ]; then printf "%b%-${1}s ${DEFAULT}" "${SUCCESS}" "$2"; else printf "%b%b ${DEFAULT}" "${SUCCESS}" "${*}"; fi } +echo_error() { printf "%berror: %b${DEFAULT}" "${DANGER}" "${*}"; } + +alert_primary() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_PRIMARY}" '' "${ALERT_PRIMARY}" "${*}" "${ALERT_PRIMARY}" ''; } +alert_secondary() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_SECONDARY}" '' "${ALERT_SECONDARY}" "${*}" "${ALERT_SECONDARY}" ''; } +alert_success() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_SUCCESS}" '' "${ALERT_SUCCESS}" "${*}" "${ALERT_SUCCESS}" ''; } +alert_danger() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_DANGER}" '' "${ALERT_DANGER}" "${*}" "${ALERT_DANGER}" ''; } +alert_warning() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_WARNING}" '' "${ALERT_WARNING}" "${*}" "${ALERT_WARNING}" ''; } +alert_info() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_INFO}" '' "${ALERT_INFO}" "${*}" "${ALERT_INFO}" ''; } +alert_light() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_LIGHT}" '' "${ALERT_LIGHT}" "${*}" "${ALERT_LIGHT}" ''; } +alert_dark() { printf "${NL}%b%64s${NL}%b %-63s${NL}%b%64s${NL}\n" "${ALERT_DARK}" '' "${ALERT_DARK}" "${*}" "${ALERT_DARK}" ''; } diff --git a/src/colors/colors_v7c.sh b/src/colors/colors_v7c.sh new file mode 100755 index 0000000..5e78fdb --- /dev/null +++ b/src/colors/colors_v7c.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 7.0.2-c +# */ + +echo_primary() { printf '%b%b\033[0m' '\033[97m' "${*}"; } +echo_secondary() { printf '%b%b\033[0m' '\033[94m' "${*}"; } +echo_success() { printf '%b%b\033[0m' '\033[32m' "${*}"; } +echo_danger() { printf '%b%b\033[0m' '\033[31m' "${*}"; } +echo_warning() { printf '%b%b\033[0m' '\033[33m' "${*}"; } +echo_info() { printf '%b%b\033[0m' '\033[95m' "${*}"; } +echo_light() { printf '%b%b\033[0m' '\033[47;90m' "${*}"; } +echo_dark() { printf '%b%b\033[0m' '\033[40;37m' "${*}"; } + +echo_label() { if [ $# -eq 2 ]; then printf "%b%-${1}s \033[0m" '\033[32m' "$2"; else printf "%b%b \033[0m" '\033[32m' "${*}"; fi } +echo_error() { printf '%berror: %b\033[0m' '\033[31m' "${*}"; } + +alert_primary() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;104;97m' '' '\033[1;104;97m' "${*}" '\033[1;104;97m' ''; } +alert_secondary() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;45;97m' '' '\033[1;45;97m' "${*}" '\033[1;45;97m' ''; } +alert_success() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;42;97m' '' '\033[1;42;97m' "${*}" '\033[1;42;97m' ''; } +alert_danger() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;41;97m' '' '\033[1;41;97m' "${*}" '\033[1;41;97m' ''; } +alert_warning() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;43;97m' '' '\033[1;43;97m' "${*}" '\033[1;43;97m' ''; } +alert_info() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;44;97m' '' '\033[1;44;97m' "${*}" '\033[1;44;97m' ''; } +alert_light() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;47;90m' '' '\033[1;47;90m' "${*}" '\033[1;47;90m' ''; } +alert_dark() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;40;37m' '' '\033[1;40;37m' "${*}" '\033[1;40;37m' ''; } diff --git a/src/colors/colors_v7d.sh b/src/colors/colors_v7d.sh new file mode 100755 index 0000000..99e892f --- /dev/null +++ b/src/colors/colors_v7d.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors +# * +# * A semantic set of colors for shell scripts inspired by Bootstrap and Symfony +# * +# * @author "Matthias Morin" +# * @licence MIT +# * @link https://github.com/TangoMan75/tools +# * @version 7.0.2-d +# */ + +echo_primary() { printf '%b%b\033[0m\n' '\033[97m' "${*}"; } +echo_secondary() { printf '%b%b\033[0m\n' '\033[94m' "${*}"; } +echo_success() { printf '%b%b\033[0m\n' '\033[32m' "${*}"; } +echo_danger() { printf '%b%b\033[0m\n' '\033[31m' "${*}"; } +echo_warning() { printf '%b%b\033[0m\n' '\033[33m' "${*}"; } +echo_info() { printf '%b%b\033[0m\n' '\033[95m' "${*}"; } +echo_light() { printf '%b%b\033[0m\n' '\033[47;90m' "${*}"; } +echo_dark() { printf '%b%b\033[0m\n' '\033[40;37m' "${*}"; } + +echo_label() { if [ $# -eq 2 ]; then printf "%b%-${1}s \033[0m" '\033[32m' "$2"; else printf "%b%b \033[0m" '\033[32m' "${*}"; fi } +echo_error() { printf '%berror: %b\033[0m\n' '\033[31m' "${*}"; } + +alert_primary() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;104;97m' '' '\033[1;104;97m' "${*}" '\033[1;104;97m' ''; } +alert_secondary() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;45;97m' '' '\033[1;45;97m' "${*}" '\033[1;45;97m' ''; } +alert_success() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;42;97m' '' '\033[1;42;97m' "${*}" '\033[1;42;97m' ''; } +alert_danger() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;41;97m' '' '\033[1;41;97m' "${*}" '\033[1;41;97m' ''; } +alert_warning() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;43;97m' '' '\033[1;43;97m' "${*}" '\033[1;43;97m' ''; } +alert_info() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;44;97m' '' '\033[1;44;97m' "${*}" '\033[1;44;97m' ''; } +alert_light() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;47;90m' '' '\033[1;47;90m' "${*}" '\033[1;47;90m' ''; } +alert_dark() { printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;40;37m' '' '\033[1;40;37m' "${*}" '\033[1;40;37m' ''; } diff --git a/src/colors/print_colors.sh b/src/colors/print_colors.sh new file mode 100755 index 0000000..2cd519b --- /dev/null +++ b/src/colors/print_colors.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Colors Demo +# * +# * A demo to test colors +# * +# * colors: {30..37} +# * high intensity foreground: {90..97} +# * bold: 1 +# * underline: 4 +# * background: {40..47} +# * high intensity background: {100..107} +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Print color range table +function print_colors() { + for FOREGROUND in {30..37} {90..97}; do + for ATTR in 0 1 4; do + for BACKGROUND in 0 {40..47} {100..107}; do + if [ "${BACKGROUND}" -eq 0 ] && [ "${ATTR}" -eq 0 ]; then + echo -en "\033[${FOREGROUND}m" + printf "%11s" "[${FOREGROUND}m " + echo -en "\033[0m" + elif [ "${ATTR}" -eq 0 ]; then + echo -en "\033[${BACKGROUND};${FOREGROUND}m" + printf "%11s" "[${BACKGROUND};${FOREGROUND}m " + echo -en "\033[0m" + elif [ "${BACKGROUND}" -eq 0 ]; then + echo -en "\033[${ATTR};${FOREGROUND}m" + printf "%11s" "[${ATTR};${FOREGROUND}m " + echo -en "\033[0m" + else + echo -en "\033[$ATTR;${BACKGROUND};${FOREGROUND}m" + printf "%11s" "[${ATTR};${BACKGROUND};${FOREGROUND}m " + echo -en "\033[0m" + fi + done + echo + done + done +} + +print_colors diff --git a/src/git/get_latest_release.sh b/src/git/get_latest_release.sh new file mode 100755 index 0000000..03cc170 --- /dev/null +++ b/src/git/get_latest_release.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Get repository latest release from github +function get_latest_release() { + if [ ! -x "$(command -v curl)" ]; then + echo_error "\"${FUNCNAME[0]}\" requires curl, try: 'sudo apt-get install -y curl'" + + return 1 + fi + + local REPOSITORY=$1 + + OPTIND=0 + while getopts :h OPTION; do + case "${OPTION}" in + h|*) echo_warning "${FUNCNAME[0]}"; + echo_label 16 'description:'; echo_primary 'Get repository latest release' + echo_label 16 'usage:'; echo_primary "${FUNCNAME[0]} [owner/repository] -h (help)" + return 0;; + esac + done + + if [ "$#" -lt 1 ]; then + echo_error 'some mandatory parameter is missing' + echo_label 8 'usage:'; echo_primary "${FUNCNAME[0]} [owner/repository] -h (help)" + return 1 + fi + + if [ "$#" -gt 1 ]; then + echo_error "too many arguments ($#)" + echo_label 8 'usage:'; echo_primary "${FUNCNAME[0]} [owner/repository] -h (help)" + return 1 + fi + + curl --silent "https://api.github.com/repos/${REPOSITORY}/releases/latest" | + grep '"tag_name":' | # find tag name + sed -E 's/.*"[vV]?([^"]+)".*/\1/' # remove quotes and leading v if any +} diff --git a/src/git/get_latest_tag.sh b/src/git/get_latest_tag.sh new file mode 100755 index 0000000..94703ab --- /dev/null +++ b/src/git/get_latest_tag.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Get repository latest tag from github +function get_latest_tag() { + if [ ! -x "$(command -v curl)" ]; then + echo_error "\"${FUNCNAME[0]}\" requires curl, try: 'sudo apt-get install -y curl'" + + return 1 + fi + + local REPOSITORY=$1 + + OPTIND=0 + while getopts :h OPTION; do + case "${OPTION}" in + h|*) echo_warning "${FUNCNAME[0]}"; + echo_label 16 'description:'; echo_primary 'Get repository latest tag' + echo_label 16 'usage:'; echo_primary "${FUNCNAME[0]} [owner/repository] -h (help)" + return 0;; + esac + done + + if [ "$#" -lt 1 ]; then + echo_error 'some mandatory parameter is missing' + echo_label 8 'usage:'; echo_primary "${FUNCNAME[0]} [owner/repository] -h (help)" + return 1 + fi + + if [ "$#" -gt 1 ]; then + echo_error "too many arguments ($#)" + echo_label 8 'usage:'; echo_primary "${FUNCNAME[0]} [owner/repository] -h (help)" + return 1 + fi + + curl --silent "https://api.github.com/repos/${REPOSITORY}/tags" | + grep -m 1 '"name":' | # find first tag name + sed -E 's/.*"[vV]?([^"]+)".*/\1/' # remove quotes and leading v if any +} diff --git a/src/network/download.sh b/src/network/download.sh new file mode 100755 index 0000000..bb7d260 --- /dev/null +++ b/src/network/download.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Download file with either curl or wget +function download() { + local DOWNLOADER + + if [ ! -x "$(command -v curl)" ]; then + DOWNLOADER="curl" + elif [ ! -x "$(command -v wget)" ]; then + DOWNLOADER="wget" + else + echo_error "\"$(basename "${0}")\" requires curl or wget, try: 'sudo apt-get install -y curl'" + + return 1 + fi + + case ${DOWNLOADER} in + 'curl') + # -s sient -S show errors -L follow redirects + echo_info "curl -sSL \"$1\"" + curl -sSL "$1" + ;; + 'wget') + # -q sient + echo_info "wget -q \"$1\"" + wget -q "$1" + ;; + esac +} + diff --git a/src/system/check_install.sh b/src/system/check_install.sh new file mode 100755 index 0000000..487cb48 --- /dev/null +++ b/src/system/check_install.sh @@ -0,0 +1,153 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Check Install +# * +# * This script is meant to quickly check for common apps installation +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}"/../colors/colors.sh +# shellcheck source=/dev/null +. "${CURDIR}"/is_installed.sh + +## Check availability of some development tools +check_install() { + alert_primary 'Check Install' + + echo_label 17 'ansible' + is_installed 'ansible' + + echo_label 17 'apache2' + is_installed 'apache2' + + echo_label 17 'aws' + is_installed 'aws' + + echo_label 17 'bundle' + is_installed 'bundle' + + echo_label 17 'chef' + is_installed 'chef' + + echo_label 17 'curl' + is_installed 'curl' + + echo_label 17 'docker' + is_installed 'docker' + + echo_label 17 'docker-compose' + is_installed 'docker-compose' + + echo_label 17 'gawk' + is_installed 'gawk' + + echo_label 17 'gem' + is_installed 'gem' + + echo_label 17 'git' + is_installed 'git' + + echo_label 17 'iptables' + is_installed 'iptables' + + echo_label 17 'make' + is_installed 'make' + + echo_label 17 'mokutil' + is_installed 'mokutil' + + echo_label 17 'mysql' + is_installed 'mysql' + + echo_label 17 'nfs-common' + is_installed 'nfs-common' + + echo_label 17 'nfs-kernel-server' + is_installed 'nfs-kernel-server' + + echo_label 17 'netstat' + is_installed 'netstat' + + echo_label 17 'nginx' + is_installed 'nginx' + + echo_label 17 'nmap' + is_installed 'nmap' + + echo_label 17 'nodejs' + is_installed 'nodejs' + + echo_label 17 'npm' + is_installed 'npm' + + echo_label 17 'openssl' + is_installed 'openssl' + + echo_label 17 'perl' + is_installed 'perl' + + echo_label 17 'psql' + is_installed 'psql' + + echo_label 17 'php' + is_installed 'php' + + echo_label 17 'pip' + is_installed 'pip' + + echo_label 17 'pip3' + is_installed 'pip3' + + echo_label 17 'python' + is_installed 'python' + + echo_label 17 'python3' + is_installed 'python3' + + echo_label 17 'ruby' + is_installed 'ruby' + + echo_label 17 'rvm' + is_installed 'rvm' + + echo_label 17 'shellcheck' + is_installed 'shellcheck' + + echo_label 17 'sqlite3' + is_installed 'sqlite3' + + echo_label 17 'ufw' + is_installed 'ufw' + + echo_label 17 'vagrant' + is_installed 'vagrant' + + echo_label 17 'vboxmanage' + is_installed 'vboxmanage' + + echo_label 17 'wine' + is_installed 'wine' + + echo_label 17 'wget' + is_installed 'wget' + + echo_label 17 'yarn' + is_installed 'yarn' + + echo_label 17 'zsh' + is_installed 'zsh' +} diff --git a/src/system/check_root.sh b/src/system/check_root.sh new file mode 100755 index 0000000..1d65c38 --- /dev/null +++ b/src/system/check_root.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Throws error when user doesn't own root privileges +function check_root() { + if [ "$(id | awk '{print $1}')" != 'uid=0(root)' ];then + echo_error "user \"${USER}\" does not own root privileges." + + return 1 + fi +} + diff --git a/src/system/check_versions.sh b/src/system/check_versions.sh new file mode 100755 index 0000000..e113a4c --- /dev/null +++ b/src/system/check_versions.sh @@ -0,0 +1,314 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Check Install +# * +# * This script is meant to quickly check for common apps installation +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +CURDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +# shellcheck source=/dev/null +. "${CURDIR}"/../colors/colors.sh +# shellcheck source=/dev/null +. "${CURDIR}"/is_installed.sh + +## Check version of some development tools +check_versions() { + alert_primary 'Check Versions' + + echo_info 'ansible\n' + if [ "$(is_installed 'ansible')" = true ]; then + ansible --version | head -n 1 + else + echo_danger 'ansible is not installed on your system\n' + fi + + echo_info 'apache2\n' + if [ "$(is_installed 'apache2')" = true ] && [ -n "$(apache2 --version 2>/dev/null)" ]; then + apache2 --version 2>/dev/null + else + echo_danger 'apache2 is not installed on your system\n' + fi + + echo_info 'aws\n' + if [ "$(is_installed 'aws')" = true ]; then + aws --version + else + echo_danger 'aws is not installed on your system\n' + fi + + echo_info 'bundle\n' + if [ "$(is_installed 'bundle')" = true ]; then + bundle --version + else + echo_danger 'bundle is not installed on your system\n' + fi + + echo_info 'chef\n' + if [ "$(is_installed 'chef')" = true ]; then + chef --version + else + echo_danger 'chef is not installed on your system\n' + fi + + echo_info 'composer\n' + if [ "$(is_installed 'php')" = true ]; then + if [ -n "$(command -v composer)" ]; then + $(command -v composer) --version + else + echo_danger 'composer is not installed on your system\n' + fi + else + echo_danger 'unable to show composer version, php not installed\n' + fi + + echo_info 'curl\n' + if [ "$(is_installed 'curl')" = true ]; then + curl --version | head -n1 + else + echo_danger 'curl is not installed on your system\n' + fi + + echo_info 'docker\n' + if [ "$(is_installed 'docker')" = true ]; then + docker --version + else + echo_danger 'docker is not installed on your system\n' + fi + + echo_info 'docker-compose\n' + if [ "$(is_installed 'compose')" = true ]; then + docker-compose --version + else + echo_danger 'docker-compose is not installed on your system\n' + fi + + echo_info 'gem\n' + if [ "$(is_installed 'gem')" = true ]; then + gem --version + else + echo_danger 'gem is not installed on your system\n' + fi + + echo_info 'git\n' + if [ "$(is_installed 'git')" = true ]; then + git --version + else + echo_danger 'git is not installed on your system\n' + fi + + echo_info 'iptables\n' + if [ "$(is_installed 'iptables')" = true ]; then + iptables --version + else + echo_danger 'iptables is not installed on your system\n' + fi + + echo_info 'make\n' + if [ "$(is_installed 'make')" = true ]; then + make --version | head -n1 + else + echo_danger 'make is not installed on your system\n' + fi + + echo_info 'mysql\n' + if [ "$(is_installed 'mysql')" = true ]; then + mysql --version + else + echo_danger 'mysql is not installed on your system\n' + fi + + echo_info 'nfs-common\n' + if [ "$(is_installed 'nfs-common')" = true ]; then + dpkg -la | grep 'nfs-common' + else + echo_danger 'nfs-common is not installed on your system\n' + fi + + echo_info 'nfs-kernel-server\n' + if [ "$(is_installed 'nfs-kernel-server')" = true ]; then + dpkg -la | grep 'nfs-kernel-server' + else + echo_danger 'nfs-kernel-server is not installed on your system\n' + fi + + echo_info 'netstat\n' + if [ "$(is_installed 'netstat')" = true ]; then + netstat --version | head -n1 + else + echo_danger 'netstat is not installed on your system\n' + fi + + echo_info 'nginx\n' + if [ "$(is_installed 'nginx')" = true ]; then + nginx --version + else + echo_danger 'nginx is not installed on your system\n' + fi + + echo_info 'node\n' + if [ "$(is_installed 'node')" = true ]; then + node --version + else + echo_danger 'node is not installed on your system\n' + fi + + echo_info 'npm\n' + if [ "$(is_installed 'npm')" = true ]; then + npm --version + else + echo_danger 'npm is not installed on your system\n' + fi + + echo_info 'perl\n' + if [ "$(is_installed 'perl')" = true ]; then + perl --version | head -n2 | tail -n1 + else + echo_danger 'perl is not installed on your system\n' + fi + + echo_info 'openssl\n' + if [ "$(is_installed 'openssl')" = true ]; then + openssl version + else + echo_danger 'openssl is not installed on your system\n' + fi + + echo_info 'php\n' + if [ "$(is_installed 'php')" = true ]; then + php --version + else + echo_danger 'php is not installed on your system\n' + fi + + echo_info 'php-modules\n' + if [ "$(is_installed 'php')" = true ]; then + php -m + else + echo_danger 'unable to show php-modules, php not installed\n' + fi + + echo_info 'pip\n' + if [ "$(is_installed 'pip')" = true ]; then + pip --version + else + echo_danger 'pip is not installed on your system\n' + fi + + echo_info 'pip3\n' + if [ "$(is_installed 'pip3')" = true ]; then + pip3 --version + else + echo_danger 'pip3 is not installed on your system\n' + fi + + echo_info 'psql\n' + if [ "$(is_installed 'psql')" = true ]; then + psql --version + else + echo_danger 'PostgreSQL is not installed on your system\n' + fi + + echo_info 'python\n' + if [ "$(is_installed 'python')" = true ]; then + python --version + else + echo_danger 'python is not installed on your system\n' + fi + + echo_info 'python3\n' + if [ "$(is_installed 'python3')" = true ]; then + python3 --version + else + echo_danger 'python3 is not installed on your system\n' + fi + + echo_info 'ruby\n' + if [ "$(is_installed 'ruby')" = true ]; then + ruby --version + else + echo_danger 'ruby is not installed on your system\n' + fi + + echo_info 'rvm\n' + if [ "$(is_installed 'rvm')" = true ]; then + rvm version + else + echo_danger 'rvm is not installed on your system\n' + fi + + echo_info 'shellcheck\n' + if [ "$(is_installed 'shellcheck')" = true ]; then + shellcheck --version | head -2 | tail -1 + else + echo_danger 'shellcheck is not installed on your system\n' + fi + + echo_info 'sqlite3\n' + if [ "$(is_installed 'sqlite3')" = true ]; then + sqlite3 --version + else + echo_danger 'sqlite3 is not installed on your system\n' + fi + + echo_info 'ufw\n' + if [ "$(is_installed 'ufw')" = true ]; then + ufw --version | head -n1 + else + echo_danger 'ufw is not installed on your system\n' + fi + + echo_info 'vagrant\n' + if [ "$(is_installed 'vagrant')" = true ]; then + vagrant --version + else + echo_danger 'vagrant is not installed on your system\n' + fi + + echo_info 'virtual box\n' + if [ "$(is_installed 'VBoxManage')" = true ]; then + VBoxManage --version + else + echo_danger 'virtual box is not installed on your system\n' + fi + + echo_info 'wget\n' + if [ "$(is_installed 'wget')" = true ]; then + wget --version | head -n1 + else + echo_danger 'wget is not installed on your system\n' + fi + + echo_info 'wine\n' + if [ "$(is_installed 'wine')" = true ]; then + wine --version + else + echo_danger 'wine is not installed on your system\n' + fi + + echo_info 'yarn\n' + if [ "$(is_installed 'yarn')" = true ]; then + yarn --version + else + echo_danger 'yarn is not installed on your system\n' + fi + + echo_info 'zsh\n' + if [ "$(is_installed 'zsh')" = true ]; then + zsh --version + else + echo_danger 'zsh is not installed on your system\n' + fi +} diff --git a/src/system/compare_versions.sh b/src/system/compare_versions.sh new file mode 100755 index 0000000..ac842fb --- /dev/null +++ b/src/system/compare_versions.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Compare two semantic versions +compare_versions() { + local INPUT + + for INPUT in "$1" "$2"; do + if [[ ! "${INPUT}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo -ne "\033[1;31merror:\t\033[0;31minvalid version (${INPUT})\033[0m\n" + return 1 + fi + done + + local VERSION_1=() + local VERSION_2=() + + # # split each version string with dot character (option 1, not zsh compliant) + # BKP=${IFS} + # IFS='.' + # read -r -a VERSION_1 <<< "$1" + # read -r -a VERSION_2 <<< "$2" + # IFS=${BKP} + + # # zsh handles arrays in a weird fashion (option 2, not bash compliant) + # VERSION_1=("${(@s/./)1}") + # VERSION_2=("${(@s/./)2}") + + # # split each version string with dot character (option 3, non working option) + # BKP="${IFS}" + # IFS="." + # while read -r ITEM; do VERSION_1+=("${ITEM}"); done < <(echo "$1") + # while read -r ITEM; do VERSION_2+=("${ITEM}"); done < <(echo "$2") + # IFS="${BKP}" + + # split each version string with dot character (option 4, short syntax but not shellcheck valid) + # shellcheck disable=2207 + VERSION_1=($(echo "$1" | tr '.' ' ')) + # shellcheck disable=2207 + VERSION_2=($(echo "$2" | tr '.' ' ')) + + # compare each number + for KEY in {0..2}; do + if [ "${VERSION_1[$KEY]}" -eq "${VERSION_2[$KEY]}" ]; then + continue + fi + if [ "${VERSION_1[$KEY]}" -gt "${VERSION_2[$KEY]}" ]; then + echo '>' + return 0 + elif [ "${VERSION_1[$KEY]}" -lt "${VERSION_2[$KEY]}" ]; then + echo '<' + return 0 + fi + done + + echo '=' + return 0 +} \ No newline at end of file diff --git a/src/system/hosts.sh b/src/system/hosts.sh new file mode 100755 index 0000000..98e21f3 --- /dev/null +++ b/src/system/hosts.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * Tangoman Hosts +# * +# * Edit hosts config from shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## set your /etc/hosts +function set_hosts() { + local HOST_NAME + local IP_ADDRESS=127.0.0.1 + local DELETE=false + + local ARGUMENTS=() + local OPTARG + local OPTION + while [ "${#}" -gt 0 ]; do + OPTIND=0 + while getopts :i:dh OPTION; do + case "${OPTION}" in + d) DELETE=true;; + i) IP_ADDRESS="${OPTARG}";; + h) echo_label 'description'; echo_primary 'Create / delete hosts in /etc/hosts' + echo_label 'usage'; echo_primary 'set-hosts [hosts] -i (ip) -d (delete) -h (help)' + return 0;; + :) echo_error "\"${OPTARG}\" requires value" + return 1;; + \?) echo_error "invalid option \"${OPTARG}\"" + return 1;; + esac + done + if [ "${OPTIND}" -gt 1 ]; then + shift $(( OPTIND-1 )) + fi + if [ "${OPTIND}" -eq 1 ]; then + ARGUMENTS+=("${1}") + shift + fi + done + + if [ "${#ARGUMENTS[@]}" -eq 0 ]; then + echo_error 'some mandatory parameter is missing' + echo_label 'usage'; echo_primary 'set-hosts [hosts] -i (ip) -d (delete) -h (help)' + return 1 + fi + + for HOST_NAME in "${ARGUMENTS[@]}"; do + + # find "hostname" + if < /etc/hosts grep -q -P "\s+${HOST_NAME}$";then + # remove every occurence + # shellcheck disable=SC2002 + cat /etc/hosts | grep -vP "\s+${HOST_NAME}$" | sudo tee /etc/hosts >/dev/null + + if [ "${DELETE}" = true ];then + echo_danger "Deleting host: \"${IP_ADDRESS} ${HOST_NAME}\"" + else + echo_warning "Updating host: \"${IP_ADDRESS} ${HOST_NAME}\"" + sudo /bin/sh -c "echo \"${IP_ADDRESS} ${HOST_NAME}\">> /etc/hosts" + fi + else + if [ "${DELETE}" = true ];then + echo_error "\"${HOST_NAME}\" not found" + else + echo_success "Creating host: \"${IP_ADDRESS} ${HOST_NAME}\"" + sudo /bin/sh -c "echo \"${IP_ADDRESS} ${HOST_NAME}\">> /etc/hosts" + fi + fi + done +} diff --git a/src/system/is_installed.sh b/src/system/is_installed.sh new file mode 100755 index 0000000..ab41ce1 --- /dev/null +++ b/src/system/is_installed.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Check app installed +function is_installed() { + + if [ -x "$(command -v "$1")" ]; then + echo true + + return 0 + fi + + # maybe it's a debian package + if dpkg -s "$1" 2>/dev/null | grep -q 'Status: install ok installed'; then + echo true + + return 0 + fi + + # or maybe it's a linuxbrew package + if [ -x /home/linuxbrew/.linuxbrew/bin/"$1" ]; then + echo true + + return 0 + fi + + echo false +} diff --git a/src/system/is_root.sh b/src/system/is_root.sh new file mode 100644 index 0000000..3d3e4df --- /dev/null +++ b/src/system/is_root.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Check user is root +is_root() { + if [ "$(id | awk '{print $1}')" = 'uid=0(root)' ];then + echo true + return 0 + fi + + echo false +} + diff --git a/src/system/is_ubuntu.sh b/src/system/is_ubuntu.sh new file mode 100755 index 0000000..6e02edb --- /dev/null +++ b/src/system/is_ubuntu.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Check system is ubuntu +function is_ubuntu() { + local RELEASE + RELEASE="$(lsb_release -cs 2>/dev/null)" + + for CODENAME in edgy feisty gutsy hardy intrepid jaunty karmic lucid maverick natty oneiric precise quantal raring saucy trusty utopic vivid wily xenial yakkety zesty artful bionic cosmic disco eoan focal + do + if [ "${RELEASE}" = "${CODENAME}" ]; then + echo true + return 0 + fi + done + echo false +} diff --git a/src/system/prompt_user.sh b/src/system/prompt_user.sh new file mode 100755 index 0000000..f829c02 --- /dev/null +++ b/src/system/prompt_user.sh @@ -0,0 +1,122 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Prompt user for parameter +function prompt_user() { + local ARGUMENTS=() + local PARAMETER + local BOOLEAN=false + local YES_NO=false + + local OPTARG + local OPTION + while [ "${#}" -gt 0 ]; do + OPTIND=0 + while getopts :d:byh OPTION; do + case "${OPTION}" in + d) DEFAULT_VALUE="${OPTARG}";; + b) BOOLEAN=true;; + y) YES_NO=true;; + h) echo_label 'description:'; echo_primary 'Prompt user for parameter value' + echo_label 'usage'; echo_primary 'promt_user [parameter] -b (boolean) -y (yes no) -d [default_value] -h (help)' + return 0;; + :) echo_error "\"${OPTARG}\" requires value" + return 1;; + \?) echo_error "invalid option \"${OPTARG}\"" + return 1;; + esac + done + if [ "${OPTIND}" -gt 1 ]; then + shift $(( OPTIND-1 )) + fi + if [ "${OPTIND}" -eq 1 ]; then + ARGUMENTS+=("${1}") + shift + fi + done + + if [ "${#ARGUMENTS[@]}" -eq 0 ]; then + echo_error 'some mandatory parameter is missing' + echo_label 'usage'; echo_primary 'promt_user [parameter] -b (boolean) -y (yes no) -d [default_value] -h (help)' + return 1 + fi + + if [ "${#ARGUMENTS[@]}" -gt 1 ]; then + echo_error "too many arguments (${#ARGUMENTS[@]})" + echo_label 'usage'; echo_primary 'promt_user [parameter] -d [default_value] -h (help)' + return 1 + fi + + PARAMETER="${ARGUMENTS[0]}" + + if [ "${BOOLEAN}" == true ]; then + if [ -n "${DEFAULT_VALUE}" ]; then + read -rp "${PARAMETER} (true/false) [${DEFAULT_VALUE}]: " NEW_VALUE + if [[ "${NEW_VALUE}" =~ ^[Tt][Rr]?[Uu]?[Ee]?$ ]]; then + echo true + elif [[ "${NEW_VALUE}" =~ ^[Ff][Aa]?[Ll]?[Ss]?[Ee]?$ ]]; then + echo false + else + echo "${DEFAULT_VALUE}" + fi + else + read -rp "${PARAMETER}: (true/false) [false]" NEW_VALUE + if [[ "${NEW_VALUE}" =~ ^[Tt][Rr]?[Uu]?[Ee]?$ ]]; then + echo true + else + echo false + fi + fi + return 0 + fi + + if [ "${YES_NO}" == true ]; then + if [ -n "${DEFAULT_VALUE}" ]; then + read -rp "${PARAMETER} (yes/no) [${DEFAULT_VALUE}]: " NEW_VALUE + if [[ "${NEW_VALUE}" =~ ^[Yy][Ee]?[Ss]?$ ]]; then + echo 'yes' + elif [[ "${NEW_VALUE}" =~ ^[Nn][Oo]?$ ]]; then + echo 'no' + else + echo "${DEFAULT_VALUE}" + fi + else + read -rp "${PARAMETER}: (yes/no) [no]" NEW_VALUE + if [[ "${NEW_VALUE}" =~ ^[Yy][Ee]?[Ss]?$ ]]; then + echo 'yes' + else + echo 'no' + fi + fi + return 0 + fi + + if [ -n "${DEFAULT_VALUE}" ]; then + read -rp "${PARAMETER} [${DEFAULT_VALUE}]: " NEW_VALUE + if [ -n "${NEW_VALUE}" ]; then + echo "${NEW_VALUE}" + else + echo "${DEFAULT_VALUE}" + fi + else + read -rp "${PARAMETER}: " NEW_VALUE + echo "${NEW_VALUE}" + fi +} diff --git a/src/system/update_grub.sh b/src/system/update_grub.sh new file mode 100755 index 0000000..01ae9e5 --- /dev/null +++ b/src/system/update_grub.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Bash Tools +# * +# * A set of tools for shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Updates GRUB on multiple patforms +function update_grub() { + # Update grub config + echo_primary 'Updating grub config...' + if [ -x "$(command -v update-grub)" ]; then + echo_info 'update-grub' + update-grub + + elif [ -x "$(command -v grub-mkconfig)" ]; then + echo_info 'grub-mkconfig -o /boot/grub/grub.cfg' + grub-mkconfig -o /boot/grub/grub.cfg + + elif [ -x "$(command -v grub2-mkconfig)" ]; then + if [ -x "$(command -v zypper)" ]; then + echo_info 'grub2-mkconfig -o /boot/grub2/grub.cfg' + grub2-mkconfig -o /boot/grub2/grub.cfg + + elif [ -x "$(command -v dnf)" ]; then + echo_info 'grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg' + grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg + fi + fi +} diff --git a/src/yaml/get_parameter.sh b/src/yaml/get_parameter.sh new file mode 100755 index 0000000..435a346 --- /dev/null +++ b/src/yaml/get_parameter.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Yaml +# * +# * A light tool to roughly parse yaml files from shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Read parameter from .yaml file +get_parameter() { + local ARGUMENTS=() + local FILE_PATH + local PARAMETER + local SEPARATOR=':' + + local OPTARG + local OPTION + while [ "${#}" -gt 0 ]; do + OPTIND=0 + while getopts :f:s:h OPTION; do + case "${OPTION}" in + f) FILE_PATH="${OPTARG}";; + s) SEPARATOR="$(echo "${OPTARG}"|sed -E 's/^[[:space:]]*//'|sed -E 's/[[:space:]]*$//'|tr -d "\t")";; + h) echo_label 'description:'; echo_primary 'Return parameter value from given yml file' + echo_label 'usage'; echo_primary 'get_parameter [parameter] -f [file_path] -s [separator] -h (help)' + return 0;; + :) echo_error "\"${OPTARG}\" requires value" + return 1;; + \?) echo_error "invalid option \"${OPTARG}\"" + return 1;; + esac + done + if [ "${OPTIND}" -gt 1 ]; then + shift $(( OPTIND-1 )) + fi + if [ "${OPTIND}" -eq 1 ]; then + ARGUMENTS+=("${1}") + shift + fi + done + + if [ "${#ARGUMENTS[@]}" -eq 0 ]; then + echo_error 'some mandatory parameter is missing' + echo_label 'usage'; echo_primary 'get_parameter [parameter] -f [file_path] -s [separator] -h (help)' + return 1 + fi + + if [ "${#ARGUMENTS[@]}" -gt 1 ]; then + echo_error "too many arguments (${#ARGUMENTS[@]})" + echo_label 'usage'; echo_primary 'get_parameter [parameter] -f [file_path] -s [separator] -h (help)' + return 1 + fi + + # check file exists + if [ ! -f "${FILE_PATH}" ]; then + echo_error "Could not find \"${FILE_PATH}\"" + return 1 + fi + + PARAMETER="${ARGUMENTS[0]}" + + # 1. `grep -v '#'`: ignore comments + # 2. `grep -m1`: return first match only + # 3. `sed`: remove parameter from match + # 4. trim leading and trailing whitespaces and remove all tabulations + # sed -E 's/^[[:space:]]*//' | sed -E 's/[[:space:]]*$//' | tr -d "\t" + < "${FILE_PATH}" grep -v '#' | grep -E -m1 "^\s*${PARAMETER}\s*${SEPARATOR}" | sed "s/${PARAMETER}${SEPARATOR}//g" | sed -E 's/^[[:space:]]*//' | sed -E 's/[[:space:]]*$//' | tr -d "\t" +} diff --git a/src/yaml/set_parameter.sh b/src/yaml/set_parameter.sh new file mode 100755 index 0000000..5dc8062 --- /dev/null +++ b/src/yaml/set_parameter.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +#/** +# * TangoMan Yaml +# * +# * A light tool to roughly parse yaml files from shell scripts +# * +# * @licence MIT +# * @author "Matthias Morin" +# */ + +## Set parameter to .yaml file +set_parameter() { + local ARGUMENTS=() + local FILE_PATH + local PARAMETER + local SEPARATOR=':' + local SPACE=' ' + local VALUE + + local OPTARG + local OPTION + while [ "${#}" -gt 0 ]; do + OPTIND=0 + while getopts :f:s:nh OPTION; do + case "${OPTION}" in + f) FILE_PATH="${OPTARG}";; + s) SEPARATOR="$(echo "${OPTARG}"|sed -E 's/^[[:space:]]*//'|sed -E 's/[[:space:]]*$//'|tr -d "\t")";; + n) SPACE='';; + h) echo_label 'description:'; echo_primary 'Return parameter value from given yml file' + echo_label 'usage'; echo_primary 'set_parameter [parameter] [value] -f [file_path] -s [separator] -n (no_space) -h (help)' + return 0;; + :) echo_error "\"${OPTARG}\" requires value" + return 1;; + \?) echo_error "invalid option \"${OPTARG}\"" + return 1;; + esac + done + if [ "${OPTIND}" -gt 1 ]; then + shift $(( OPTIND-1 )) + fi + if [ "${OPTIND}" -eq 1 ]; then + ARGUMENTS+=("${1}") + shift + fi + done + + if [ "${#ARGUMENTS[@]}" -eq 0 ]; then + echo_error 'some mandatory parameter is missing' + echo_label 'usage'; echo_primary 'set_parameter [parameter] [value] -f [file_path] -s [separator] -n (no_space) -h (help)' + return 1 + fi + + if [ "${#ARGUMENTS[@]}" -gt 2 ]; then + echo_error "too many arguments (${#ARGUMENTS[@]})" + echo_label 'usage'; echo_primary 'set_parameter [parameter] [value] -f [file_path] -s [separator] -n (no_space) -h (help)' + return 1 + fi + + if [ ! -f "${FILE_PATH}" ]; then + if [ ! -f "${FILE_PATH}.dist" ]; then + echo_error "file not found: ${FILE_PATH}" + return 1 + else + echo_info "cp \"${FILE_PATH}.dist\" \"${FILE_PATH}\"" + cp "${FILE_PATH}.dist" "${FILE_PATH}" + fi + fi + + PARAMETER="${ARGUMENTS[0]}" + + # shellcheck disable=SC2143 + if [ -z "$(< "${FILE_PATH}" grep -v "#" | grep "${PARAMETER}${SEPARATOR}")" ]; then + echo_error "invalid parameter \"${PARAMETER}${SEPARATOR}\"" + return 1 + fi + + # escape forward slashes if any + VALUE=$(echo "${ARGUMENTS[1]}" | sed "s/\//\\\\\//g") + REGEXP="${PARAMETER}${SEPARATOR}.+\$" + sed -i -E s/"${REGEXP}/${PARAMETER}${SEPARATOR}${SPACE}${VALUE}"/ "${FILE_PATH}" +} diff --git a/tests/bash_unit b/tests/bash_unit new file mode 100755 index 0000000..75b3b01 --- /dev/null +++ b/tests/bash_unit @@ -0,0 +1,445 @@ +#!/usr/bin/env bash +# +# bash unit testing enterprise edition framework for professionals +# Copyright (C) 2011-2016 Pascal Grange +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# https://github.com/pgrange/bash_unit + +VERSION=v1.7.1 + +ESCAPE=$(printf "\033") +NOCOLOR="${ESCAPE}[0m" +RED="${ESCAPE}[91m" +GREEN="${ESCAPE}[92m" +YELLOW="${ESCAPE}[93m" +BLUE="${ESCAPE}[94m" + +# Make bash_unit immune to some basic unix commands faking +CAT="$(command -v cat)" +SED="$(command -v sed)" +GREP="$(command -v grep)" +RM="$(command -v rm)" + +fail() { + local message + local stdout + local stderr + + message=${1:-} + stdout=${2:-} + stderr=${3:-} + + # shellcheck disable=SC2154 + notify_test_failed "$__bash_unit_current_test__" "$message" + [[ -n $stdout ]] && [ -s "$stdout" ] && notify_stdout < "$stdout" + [[ -n $stderr ]] && [ -s "$stderr" ] && notify_stderr < "$stderr" + + stacktrace | notify_stack + exit 1 +} + +assert() { + local assertion + local message + + assertion=$1 + message=${2:-} + + _assert_expression \ + "$assertion" \ + "[ \$status == 0 ]" \ + "\"$message\"" +} + +assert_fails() { + local assertion + local message + + assertion=$1 + message=${2:-} + + _assert_expression \ + "$assertion" \ + "[ \$status != 0 ]" \ + "\"$message\"" +} + +assert_fail() { + #deprecated, use assert_fails instead + assert_fails "$@" +} + +assert_status_code() { + local expected_status + local assertion + local message + + expected_status=$1 + assertion="$2" + message="${3:-}" + + _assert_expression \ + "$assertion" \ + "[ \$status == $expected_status ]" \ + "\"$message\" expected status code $expected_status but was \$status" +} + +_assert_expression() { + local assertion + local condition + local message + + assertion=$1 + condition=$2 + message=$3 + + ( + local stdout + local stderr + + stdout=$(mktemp) + stderr=$(mktemp) + + trap '$RM -f \"$stdout\" \"$stderr\"' EXIT + + local status + + eval "($assertion)" >"$stdout" 2>"$stderr" && status=$? || status=$? + if ! eval "$condition" + then + fail "$(eval echo "$message")" "$stdout" "$stderr" + fi + ) || exit $? +} + +assert_equals() { + local expected + local actual + local message + + expected=$1 + actual=$2 + message=${3:-} + + [[ -z $message ]] || message="$message\n" + + if [ "$expected" != "$actual" ] + then + fail "$message expected [$expected] but was [$actual]" + fi +} + +assert_not_equals() { + local unexpected + local actual + local message + + unexpected=$1 + actual=$2 + message=${3:-} + + [[ -z $message ]] || message="$message\n" + + [ "$unexpected" != "$actual" ] || \ + fail "$message expected different value than [$unexpected] but was the same" +} + +fake() { + local command + + command=$1 + + shift + if [ $# -gt 0 ] + then + eval "function $command() { export FAKE_PARAMS=\"\$@\" ; $* ; }" + else + eval "function $command() { echo \"$($CAT)\" ; }" + fi + export -f "${command?}" +} + +stacktrace() { + local i + + i=1 + + # shellcheck disable=SC2128 + while [ -n "${BASH_SOURCE[$i]:-}" ] + do + echo "${BASH_SOURCE[$i]}":${BASH_LINENO[$((i-1))]}:"${FUNCNAME[$i]}"\(\) + i=$((i + 1)) + done | "$GREP" -v "^$BASH_SOURCE" +} + +run_test_suite() { + local failure + + failure=0 + + declare -F | "$GREP" ' setup_suite$' >/dev/null && setup_suite + + for pending_test in $(set | "$GREP" -E '^(pending|todo).* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::') + do + notify_test_starting "$pending_test" + notify_test_pending "$pending_test" + done + + for test in $(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::') + do + ( + local status + status=0 + declare -F | "$GREP" ' setup$' >/dev/null && setup + (__bash_unit_current_test__="$test" run_test) || status=$? + declare -F | "$GREP" ' teardown$' >/dev/null && teardown + exit $status + ) + failure=$(( $? || failure)) + done + + declare -F | "$GREP" ' teardown_suite$' >/dev/null && teardown_suite + + return $failure +} + +run_test() { + set -e + notify_test_starting "$__bash_unit_current_test__" + "$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__" +} + +usage() { + echo "$1" >&2 + echo "$0 [-f ] [-p ] [-p ]... ..." >&2 + echo >&2 + echo "Runs tests in test files that match s" >&2 + echo " is optional only supported value is tap" >&2 + echo "-v to get current version information" >&2 + echo "See https://github.com/pgrange/bash_unit" >&2 + exit 1 +} + +# Formating + +pretty_success() { + pretty_format "$GREEN" "\u2713" "${1:-}" +} + +pretty_warning() { + pretty_format "$YELLOW" "\u2717" "$1" +} + +pretty_failure() { + pretty_format "$RED" "\u2717" "${1:-}" +} + +pretty_format() { + local color + local pretty_symbol + local alt_symbol + local term_utf8 + + color="$1" + pretty_symbol="$2" + alt_symbol="$3" + term_utf8=false + + if is_terminal && [[ "$LANG" =~ .*UTF-8.* ]] + then + term_utf8=true + fi + ( + $CAT + if $term_utf8 + then + echo -en " $pretty_symbol " + else + [[ -n "$alt_symbol" ]] && echo -en " $alt_symbol " + fi + ) | color "$color" +} + +color() { + _start_color() { + if is_terminal ; then echo -en "$color" ; fi + } + _stop_color() { + if is_terminal ; then echo -en "$NOCOLOR" ; fi + } + local color + + color=$1 + + shift + _start_color + if [ $# -gt 0 ] + then + echo "$@" + else + $CAT + fi + _stop_color +} + +is_terminal() { + [ -t 1 ] || [[ "${FORCE_COLOR}" == true ]] +} + +text_format() { + notify_suite_starting() { + local test_file + test_file="$1" + echo "Running tests in $test_file" + } + notify_test_starting() { + local test + test="$1" + echo -n "Running $test... " | color "$BLUE" + } + notify_test_pending() { + echo -n "PENDING" | pretty_warning + echo + } + + notify_test_succeeded() { + echo -n "SUCCESS" | pretty_success + echo + } + notify_test_failed() { + local message + message="$2" + echo -n "FAILURE" | pretty_failure + echo + [[ -z $message ]] || printf "%s\n" "$message" + } + notify_stdout() { + "$SED" 's:^:out> :' | color "$GREEN" + } + notify_stderr() { + "$SED" 's:^:err> :' | color "$RED" + } + notify_stack() { + color "$YELLOW" + } +} + +tap_format() { + notify_suite_starting() { + local test_file + test_file="$1" + echo "# Running tests in $test_file" + } + notify_test_starting() { + echo -n + } + notify_test_pending() { + local test + test="$1" + echo -n "ok" | pretty_warning - + echo -n "$test" | color "$BLUE" + echo " # skip test to be written" | color "$YELLOW" + } + notify_test_succeeded() { + local test + test="$1" + echo -n "ok" | pretty_success - + echo "$test" | color "$BLUE" + } + notify_test_failed() { + local test + local message + test="$1" + message="$2" + echo -n "not ok" | pretty_failure - + echo "$test" | color "$BLUE" + [[ -z $message ]] || printf "%s\n" "$(echo "$message" | "$SED" -u -e 's/^/# /')" + } + notify_stdout() { + "$SED" 's:^:# out> :' | color "$GREEN" + } + notify_stderr() { + "$SED" 's:^:# err> :' | color "$RED" + } + notify_stack() { + "$SED" 's:^:# :' | color "$YELLOW" + } +} + +output_format=text +test_pattern="" +separator="" +while getopts "vp:f:" option +do + case "$option" in + p) + test_pattern="${test_pattern}${separator}${OPTARG}" + separator="|" + ;; + f) + output_format="${OPTARG}" + ;; + v) + echo "bash_unit $VERSION" + exit + ;; + ?) + usage + ;; + esac +done +shift $((OPTIND-1)) + +for test_file in "$@" +do + test -e "$test_file" || usage "file does not exist: $test_file" + test -r "$test_file" || usage "can not read file: $test_file" +done + +case "$output_format" in + text) + text_format + ;; + tap) + tap_format + ;; + *) + usage "unsupproted output format: $output_format" + ;; +esac + +#run tests received as parameters +failure=0 +for test_file in "$@" +do + notify_suite_starting "$test_file" + ( + set -e # Ensure bash_unit with exit with failure + # in case of syntax error. + if [[ "${STICK_TO_CWD}" != true ]] + then + cd "$(dirname "$test_file")" + # shellcheck source=/dev/null + source "$(basename "$test_file")" + else + # shellcheck source=/dev/null + source "$test_file" + fi + set +e + run_test_suite + ) + failure=$(( $? || failure)) +done + +exit $failure diff --git a/tests/fixtures/fixture.yaml b/tests/fixtures/fixture.yaml new file mode 100644 index 0000000..a20eeb1 --- /dev/null +++ b/tests/fixtures/fixture.yaml @@ -0,0 +1,4 @@ +# comment +foobar: + foo: bar + bar: baz diff --git a/tests/test_check_install.sh b/tests/test_check_install.sh new file mode 100644 index 0000000..d4c25a2 --- /dev/null +++ b/tests/test_check_install.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/check_install.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_check_install_returns_expected_result() { + assert_status_code 127 "$(check_install)" +} diff --git a/tests/test_check_root.sh b/tests/test_check_root.sh new file mode 100644 index 0000000..8d927b5 --- /dev/null +++ b/tests/test_check_root.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/check_root.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} diff --git a/tests/test_check_versions.sh b/tests/test_check_versions.sh new file mode 100644 index 0000000..78649e2 --- /dev/null +++ b/tests/test_check_versions.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/check_versions.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +# test_check_versions_returns_expected_result() { +# assert_status_code 127 "$(check_versions)" +# } diff --git a/tests/test_colors_sf.sh b/tests/test_colors_sf.sh new file mode 100644 index 0000000..df80688 --- /dev/null +++ b/tests/test_colors_sf.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_sf.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m\n' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m\n' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m\n' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m\n' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m\n' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m\n' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m\n' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m\n' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-*b \033[0m' 10 foobar)" "$(echo_label 10 foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[1;31merror:\t\033[0;91m%s\033[0m\n' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[104;97m%64s\033[0m\n\033[104;97m %-63s\033[0m\n\033[104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[45;37m%64s\033[0m\n\033[45;37m %-63s\033[0m\n\033[45;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[42;30m%64s\033[0m\n\033[42;30m %-63s\033[0m\n\033[42;30m%64s\033[0m\n\n" '' "[OK] foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\n" '!' "! [ERROR] foobar" '!';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[43;30m%64s\033[0m\n\033[43;30m %-63s\033[0m\n\033[43;30m%64s\033[0m\n\n" '' "[WARNING] foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[44;37m%64s\033[0m\n\033[44;37m %-63s\033[0m\n\033[44;37m%64s\033[0m\n\n" '' "[INFO] foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[47;90m%64s\033[0m\n\033[47;90m %-63s\033[0m\n\033[47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[40;37m%64s\033[0m\n\033[40;37m %-63s\033[0m\n\033[40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_sfb.sh b/tests/test_colors_sfb.sh new file mode 100644 index 0000000..3e0d666 --- /dev/null +++ b/tests/test_colors_sfb.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_sf.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-*b \033[0m' 10 foobar)" "$(echo_label 10 foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[1;31merror:\t\033[0;91m%s\033[0m' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[104;97m%64s\033[0m\n\033[104;97m %-63s\033[0m\n\033[104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[45;37m%64s\033[0m\n\033[45;37m %-63s\033[0m\n\033[45;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[42;30m%64s\033[0m\n\033[42;30m %-63s\033[0m\n\033[42;30m%64s\033[0m\n\n" '' "[OK] foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\033[41;37m %-63s\033[0m\n\n" '!' "! [ERROR] foobar" '!';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[43;30m%64s\033[0m\n\033[43;30m %-63s\033[0m\n\033[43;30m%64s\033[0m\n\n" '' "[WARNING] foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[44;37m%64s\033[0m\n\033[44;37m %-63s\033[0m\n\033[44;37m%64s\033[0m\n\n" '' "[INFO] foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[47;90m%64s\033[0m\n\033[47;90m %-63s\033[0m\n\033[47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[40;37m%64s\033[0m\n\033[40;37m %-63s\033[0m\n\033[40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v1.sh b/tests/test_colors_v1.sh new file mode 100644 index 0000000..8d21ecb --- /dev/null +++ b/tests/test_colors_v1.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v1.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_title_return_expected_string() { + assert_status_code 127 "$(echo_title foobar)" + assert_equals "$(echo -ne "\033[0;1;42mfoobar\033[0m\n")" "$(echo_title foobar)" +} +test_echo_caption_return_expected_string() { + assert_status_code 127 "$(echo_caption foobar)" + assert_equals "$(echo -ne "\033[0;1;44mfoobar\033[0m\n")" "$(echo_caption foobar)" +} +test_echo_bold_return_expected_string() { + assert_status_code 127 "$(echo_bold foobar)" + assert_equals "$(echo -ne "\033[0;1;34mfoobar\033[0m\n")" "$(echo_bold foobar)" +} +test_echo_danger_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(echo -ne "\033[0;31mfoobar\033[0m\n")" "$(echo_danger foobar)" +} +test_echo_success_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(echo -ne "\033[0;32mfoobar\033[0m\n")" "$(echo_success foobar)" +} +test_echo_warning_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(echo -ne "\033[0;33mfoobar\033[0m\n")" "$(echo_warning foobar)" +} +test_echo_secondary_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(echo -ne "\033[0;34mfoobar\033[0m\n")" "$(echo_secondary foobar)" +} +test_echo_info_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(echo -ne "\033[0;35mfoobar\033[0m\n")" "$(echo_info foobar)" +} +test_echo_primary_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(echo -ne "\033[0;36mfoobar\033[0m\n")" "$(echo_primary foobar)" +} +test_echo_error_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(echo -ne "\033[0;1;31merror:\033[0;31m\tfoobar\033[0m\n")" "$(echo_error foobar)" +} +test_echo_label_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(echo -ne "\033[0;1;32mfoobar:\033[0m\t")" "$(echo_label foobar)" +} +test_echo_prompt_return_expected_string() { + assert_status_code 127 "$(echo_prompt foobar)" + assert_equals "$(echo -ne "\033[0;36mfoobar\033[0m ")" "$(echo_prompt foobar)" +} + diff --git a/tests/test_colors_v2.sh b/tests/test_colors_v2.sh new file mode 100644 index 0000000..5448548 --- /dev/null +++ b/tests/test_colors_v2.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v2.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_title_should_return_expected_string() { + assert_status_code 127 "$(echo_title foobar)" + assert_equals "$(echo -ne "\033[1;44;97mfoobar\033[0m\n")" "$(echo_title foobar)" +} + +test_echo_caption_should_return_expected_string() { + assert_status_code 127 "$(echo_caption foobar)" + assert_equals "$(echo -ne "\033[1;46;97mfoobar\033[0m\n")" "$(echo_caption foobar)" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m\n")" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(echo -ne "\033[94mfoobar\033[0m\n")" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(echo -ne "\033[92mfoobar\033[0m\n")" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(echo -ne "\033[31mfoobar\033[0m\n")" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(echo -ne "\033[33mfoobar\033[0m\n")" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(echo -ne "\033[95mfoobar\033[0m\n")" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(echo -ne "\033[47;90mfoobar\033[0m\n")" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(echo -ne "\033[40;37mfoobar\033[0m\n")" "$(echo_dark foobar)" +} + +test_echo_prompt_should_return_expected_string() { + assert_status_code 127 "$(echo_prompt foobar)" + assert_equals "$(echo -ne "\033[0;36mfoobar\033[0m ")" "$(echo_prompt foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(echo -ne "\033[1;32mfoobar:\033[0m\t")" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(echo -ne "\033[1;31merror:\t\033[0;31mfoobar\033[0m\n")" "$(echo_error foobar)" +} + diff --git a/tests/test_colors_v3.sh b/tests/test_colors_v3.sh new file mode 100644 index 0000000..2a1d2bd --- /dev/null +++ b/tests/test_colors_v3.sh @@ -0,0 +1,133 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v3.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_title_should_return_expected_string() { + assert_status_code 127 "$(echo_title foobar)" + assert_equals "$(echo -ne "\033[33mfoobar\033[0m\n")" "$(echo_title foobar)" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(echo -ne "\033[97mfoobar\033[0m\n")" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m\n")" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(echo -ne "\033[92mfoobar\033[0m\n")" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(echo -ne "\033[31mfoobar\033[0m\n")" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(echo -ne "\033[93mfoobar\033[0m\n")" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(echo -ne "\033[95mfoobar\033[0m\n")" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(echo -ne "\033[47;90mfoobar\033[0m\n")" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(echo -ne "\033[40;37mfoobar\033[0m\n")" "$(echo_dark foobar)" +} + +test_echo_muted_should_return_expected_string() { + assert_status_code 127 "$(echo_muted foobar)" + assert_equals "$(echo -ne "\033[37mfoobar\033[0m\n")" "$(echo_muted foobar)" +} + +test_echo_prompt_should_return_expected_string() { + assert_status_code 127 "$(echo_prompt foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m " )" "$(echo_prompt foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(echo -ne "\033[32mfoobar\033[0m " )" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(echo -ne "\033[1;31merror:\t\033[0;91mfoobar\033[0m\n")" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;37m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;37m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v4.sh b/tests/test_colors_v4.sh new file mode 100644 index 0000000..40ee2f9 --- /dev/null +++ b/tests/test_colors_v4.sh @@ -0,0 +1,133 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v4.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_title_should_return_expected_string() { + assert_status_code 127 "$(echo_title foobar)" + assert_equals "$(echo -ne "\033[33mfoobar\033[0m\n")" "$(echo_title foobar)" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(echo -ne "\033[97mfoobar\033[0m\n")" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m\n")" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(echo -ne "\033[92mfoobar\033[0m\n")" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(echo -ne "\033[31mfoobar\033[0m\n")" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(echo -ne "\033[93mfoobar\033[0m\n")" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(echo -ne "\033[95mfoobar\033[0m\n")" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(echo -ne "\033[47;90mfoobar\033[0m\n")" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(echo -ne "\033[40;37mfoobar\033[0m\n")" "$(echo_dark foobar)" +} + +test_echo_muted_should_return_expected_string() { + assert_status_code 127 "$(echo_muted foobar)" + assert_equals "$(echo -ne "\033[37mfoobar\033[0m\n")" "$(echo_muted foobar)" +} + +test_echo_prompt_should_return_expected_string() { + assert_status_code 127 "$(echo_prompt foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m " )" "$(echo_prompt foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(echo -ne "\033[32mfoobar\033[0m " )" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(echo -ne "\033[1;31merror:\t\033[0;91mfoobar\033[0m\n")" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;37m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;37m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m$(tput el)\n%4sfoobar$(tput el)\n$(tput el)\n\033[0m$(tput el)\n")" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v5.sh b/tests/test_colors_v5.sh new file mode 100644 index 0000000..9370872 --- /dev/null +++ b/tests/test_colors_v5.sh @@ -0,0 +1,133 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v5.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_title_should_return_expected_string() { + assert_status_code 127 "$(echo_title foobar)" + assert_equals "$(echo -ne "\033[33mfoobar\033[0m\n")" "$(echo_title foobar)" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(echo -ne "\033[97mfoobar\033[0m\n")" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m\n")" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(echo -ne "\033[92mfoobar\033[0m\n")" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(echo -ne "\033[31mfoobar\033[0m\n")" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(echo -ne "\033[93mfoobar\033[0m\n")" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(echo -ne "\033[95mfoobar\033[0m\n")" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(echo -ne "\033[47;90mfoobar\033[0m\n")" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(echo -ne "\033[40;37mfoobar\033[0m\n")" "$(echo_dark foobar)" +} + +test_echo_muted_should_return_expected_string() { + assert_status_code 127 "$(echo_muted foobar)" + assert_equals "$(echo -ne "\033[37mfoobar\033[0m\n")" "$(echo_muted foobar)" +} + +test_echo_prompt_should_return_expected_string() { + assert_status_code 127 "$(echo_prompt foobar)" + assert_equals "$(echo -ne "\033[96mfoobar\033[0m " )" "$(echo_prompt foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(echo -ne "\033[32mfoobar\033[0m " )" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(echo -ne "\033[1;31merror:\t\033[0;91mfoobar\033[0m\n")" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;37m%64s\033[0m\n\033[1;42;37m %-63s\033[0m\n\033[1;42;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;37m%64s\033[0m\n\033[1;41;37m %-63s\033[0m\n\033[1;41;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v6.sh b/tests/test_colors_v6.sh new file mode 100644 index 0000000..21c8918 --- /dev/null +++ b/tests/test_colors_v6.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v6.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m\n' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m\n' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m\n' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m\n' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m\n' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m\n' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m\n' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-*b \033[0m' 10 foobar)" "$(echo_label 10 foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[1;31merror:\t\033[0;91m%s\033[0m\n' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v6b.sh b/tests/test_colors_v6b.sh new file mode 100644 index 0000000..38869fb --- /dev/null +++ b/tests/test_colors_v6b.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v6.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-*b \033[0m' 10 foobar)" "$(echo_label 10 foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[1;31merror:\t\033[0;91m%s\033[0m' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v7.sh b/tests/test_colors_v7.sh new file mode 100644 index 0000000..0964e1b --- /dev/null +++ b/tests/test_colors_v7.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v7.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m\n' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m\n' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m\n' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m\n' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m\n' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m\n' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m\n' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-10s \033[0m' foobar)" "$(echo_label 10 foobar)" +} + +test_echo_label_without_padding_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(printf '\033[32m%b \033[0m' foobar)" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[31merror: %b\033[0m\n' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v7b.sh b/tests/test_colors_v7b.sh new file mode 100644 index 0000000..ba00fba --- /dev/null +++ b/tests/test_colors_v7b.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v7b.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-10s \033[0m' foobar)" "$(echo_label 10 foobar)" +} + +test_echo_label_without_padding_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(printf '\033[32m%b \033[0m' foobar)" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[31merror: %b\033[0m' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v7c.sh b/tests/test_colors_v7c.sh new file mode 100644 index 0000000..3cf3072 --- /dev/null +++ b/tests/test_colors_v7c.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v7c.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-10s \033[0m' foobar)" "$(echo_label 10 foobar)" +} + +test_echo_label_without_padding_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(printf '\033[32m%b \033[0m' foobar)" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[31merror: %b\033[0m' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_colors_v7d.sh b/tests/test_colors_v7d.sh new file mode 100644 index 0000000..cbe0ad3 --- /dev/null +++ b/tests/test_colors_v7d.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/colors/colors_v7d.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_echo_primary_should_return_expected_string() { + assert_status_code 127 "$(echo_primary foobar)" + assert_equals "$(printf '\033[97m%b\033[0m\n' foobar)" "$(echo_primary foobar)" +} + +test_echo_secondary_should_return_expected_string() { + assert_status_code 127 "$(echo_secondary foobar)" + assert_equals "$(printf '\033[94m%b\033[0m' foobar)" "$(echo_secondary foobar)" +} + +test_echo_success_should_return_expected_string() { + assert_status_code 127 "$(echo_success foobar)" + assert_equals "$(printf '\033[32m%b\033[0m\n' foobar)" "$(echo_success foobar)" +} + +test_echo_danger_should_return_expected_string() { + assert_status_code 127 "$(echo_danger foobar)" + assert_equals "$(printf '\033[31m%b\033[0m\n' foobar)" "$(echo_danger foobar)" +} + +test_echo_warning_should_return_expected_string() { + assert_status_code 127 "$(echo_warning foobar)" + assert_equals "$(printf '\033[33m%b\033[0m\n' foobar)" "$(echo_warning foobar)" +} + +test_echo_info_should_return_expected_string() { + assert_status_code 127 "$(echo_info foobar)" + assert_equals "$(printf '\033[95m%b\033[0m\n' foobar)" "$(echo_info foobar)" +} + +test_echo_light_should_return_expected_string() { + assert_status_code 127 "$(echo_light foobar)" + assert_equals "$(printf '\033[47;90m%b\033[0m\n' foobar)" "$(echo_light foobar)" +} + +test_echo_dark_should_return_expected_string() { + assert_status_code 127 "$(echo_dark foobar)" + assert_equals "$(printf '\033[40;37m%b\033[0m\n' foobar)" "$(echo_dark foobar)" +} + +test_echo_label_should_return_expected_string() { + assert_status_code 127 "$(echo_label 10 foobar)" + assert_equals "$(printf '\033[32m%-10s \033[0m' foobar)" "$(echo_label 10 foobar)" +} + +test_echo_label_without_padding_should_return_expected_string() { + assert_status_code 127 "$(echo_label foobar)" + assert_equals "$(printf '\033[32m%b \033[0m' foobar)" "$(echo_label foobar)" +} + +test_echo_error_should_return_expected_string() { + assert_status_code 127 "$(echo_error foobar)" + assert_equals "$(printf '\033[31merror: %b\033[0m\n' foobar)" "$(echo_error foobar)" +} + +test_alert_primary_should_return_expected_string() { + assert_status_code 127 "$(alert_primary)" + assert_equals "$(printf "\033[0m\n\033[1;104;97m%64s\033[0m\n\033[1;104;97m %-63s\033[0m\n\033[1;104;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_primary foobar)" +} + +test_alert_secondary_should_return_expected_string() { + assert_status_code 127 "$(alert_secondary)" + assert_equals "$(printf "\033[0m\n\033[1;45;97m%64s\033[0m\n\033[1;45;97m %-63s\033[0m\n\033[1;45;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_secondary foobar)" +} + +test_alert_success_should_return_expected_string() { + assert_status_code 127 "$(alert_success)" + assert_equals "$(printf "\033[0m\n\033[1;42;97m%64s\033[0m\n\033[1;42;97m %-63s\033[0m\n\033[1;42;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_success foobar)" +} + +test_alert_danger_should_return_expected_string() { + assert_status_code 127 "$(alert_danger)" + assert_equals "$(printf "\033[0m\n\033[1;41;97m%64s\033[0m\n\033[1;41;97m %-63s\033[0m\n\033[1;41;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_danger foobar)" +} + +test_alert_warning_should_return_expected_string() { + assert_status_code 127 "$(alert_warning)" + assert_equals "$(printf "\033[0m\n\033[1;43;97m%64s\033[0m\n\033[1;43;97m %-63s\033[0m\n\033[1;43;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_warning foobar)" +} + +test_alert_info_should_return_expected_string() { + assert_status_code 127 "$(alert_info)" + assert_equals "$(printf "\033[0m\n\033[1;44;97m%64s\033[0m\n\033[1;44;97m %-63s\033[0m\n\033[1;44;97m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_info foobar)" +} + +test_alert_light_should_return_expected_string() { + assert_status_code 127 "$(alert_light)" + assert_equals "$(printf "\033[0m\n\033[1;47;90m%64s\033[0m\n\033[1;47;90m %-63s\033[0m\n\033[1;47;90m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_light foobar)" +} + +test_alert_dark_should_return_expected_string() { + assert_status_code 127 "$(alert_dark)" + assert_equals "$(printf "\033[0m\n\033[1;40;37m%64s\033[0m\n\033[1;40;37m %-63s\033[0m\n\033[1;40;37m%64s\033[0m\n\n" '' "foobar" '';)" "$(alert_dark foobar)" +} diff --git a/tests/test_compare_versions.sh b/tests/test_compare_versions.sh new file mode 100644 index 0000000..10dce48 --- /dev/null +++ b/tests/test_compare_versions.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +parent_directory="$(dirname "$(pwd)")" +src_file="../src/system/compare_versions.sh" + +# shellcheck source=/dev/null +. "${src_file}" +# shellcheck source=/dev/null +. "${parent_directory}/src/colors/colors.sh" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_missing_arguments_should_return_expected_error() { + assert_fails "$(compare_versions)" +} + +test_empty_arguments_should_return_expected_error() { + assert_fails "$(compare_versions '' '')" +} + +test_invalid_argument_should_return_expected_error() { + assert_fails "$(compare_versions foobar 1.0.0)" +} + +test_invalid_second_argument_should_return_expected_error() { + assert_fails "$(compare_versions 1.0.0 foobar)" +} + +test_missing_second_argument_should_return_expected_error() { + assert_fails "$(compare_versions 0.1.0)" +} + +test_empty_second_argument_should_return_expected_error() { + assert_fails "$(compare_versions 0.1.0 '')" +} + +test_compare_versions_should_return_equal() { + assert_equals '=' "$(compare_versions 0.1.0 0.1.0)" +} + +test_check_major_version_should_return_lower_than() { + assert_equals '<' "$(compare_versions 1.0.0 2.0.0)" +} + +test_check_major_version_should_return_greater_than() { + assert_equals '>' "$(compare_versions 2.0.0 1.0.0)" +} + +test_check_minor_version_should_return_lower_than() { + assert_equals '<' "$(compare_versions 2.0.0 2.1.0)" +} + +test_check_minor_version_should_return_greater_than() { + assert_equals '>' "$(compare_versions 2.1.0 2.0.0)" +} + +test_check_minor_version_should_return_greater_than_alt() { + assert_equals '>' "$(compare_versions 2.1.0 2.0.1)" +} + +test_check_minor_version_should_return_lower_than() { + assert_equals '<' "$(compare_versions 2.0.0 2.1.0)" +} + +test_check_minor_version_should_return_lower_than_alt() { + assert_equals '<' "$(compare_versions 2.0.1 2.1.0)" +} + +test_check_minor_version_should_return_greater_than() { + assert_equals '>' "$(compare_versions 2.1.0 2.0.0)" +} + +test_check_minor_version_should_return_greater_than_alt() { + assert_equals '>' "$(compare_versions 2.1.0 2.0.1)" +} + +test_check_patch_version_should_return_lower_than() { + assert_equals '<' "$(compare_versions 2.0.0 2.1.0)" +} + +test_check_patch_version_should_return_lower_than_alt() { + assert_equals '<' "$(compare_versions 2.0.1 2.1.0)" +} + +test_check_patch_version_should_return_greater_than() { + assert_equals '>' "$(compare_versions 2.1.0 2.0.0)" +} + +test_check_patch_version_should_return_greater_than_alt() { + assert_equals '>' "$(compare_versions 2.1.0 2.0.1)" +} + diff --git a/tests/test_get_latest_release.sh b/tests/test_get_latest_release.sh new file mode 100644 index 0000000..1947147 --- /dev/null +++ b/tests/test_get_latest_release.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +parent_directory="$(dirname "$(pwd)")" +src_file="../src/git/get_latest_release.sh" + +# shellcheck source=/dev/null +. "${src_file}" +# shellcheck source=/dev/null +. "${parent_directory}/src/colors/colors.sh" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_get_latest_release_missing_arguments_should_return_expected_error() { + assert_fails "$(get_latest_release)" +} diff --git a/tests/test_get_latest_tag.sh b/tests/test_get_latest_tag.sh new file mode 100644 index 0000000..856573e --- /dev/null +++ b/tests/test_get_latest_tag.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +parent_directory="$(dirname "$(pwd)")" +src_file="../src/git/get_latest_tag.sh" + +# shellcheck source=/dev/null +. "${src_file}" +# shellcheck source=/dev/null +. "${parent_directory}/src/colors/colors.sh" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_get_latest_tag_missing_arguments_should_return_expected_error() { + assert_fails "$(get_latest_tag)" +} diff --git a/tests/test_get_parameter.sh b/tests/test_get_parameter.sh new file mode 100644 index 0000000..a23e079 --- /dev/null +++ b/tests/test_get_parameter.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +fixtures_directory="$(pwd)/fixtures" +parent_directory="$(dirname "$(pwd)")" +src_file="../src/yaml/get_parameter.sh" + +# shellcheck source=/dev/null +. "${src_file}" +# shellcheck source=/dev/null +. "${parent_directory}/src/colors/colors.sh" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_get_parameter_should_return_expected_result() { + assert_equals 'bar' "$(get_parameter foo -f "${fixtures_directory}/fixture.yaml")" +} diff --git a/tests/test_hosts.sh b/tests/test_hosts.sh new file mode 100644 index 0000000..85ad0f4 --- /dev/null +++ b/tests/test_hosts.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/hosts.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + diff --git a/tests/test_is_installed.sh b/tests/test_is_installed.sh new file mode 100644 index 0000000..cc8bc19 --- /dev/null +++ b/tests/test_is_installed.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/is_installed.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_is_installed_should_return_false() { + assert_equals false "$(is_installed foobar)" +} diff --git a/tests/test_is_ubuntu.sh b/tests/test_is_ubuntu.sh new file mode 100644 index 0000000..ad44c95 --- /dev/null +++ b/tests/test_is_ubuntu.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/is_ubuntu.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + diff --git a/tests/test_prompt_user.sh b/tests/test_prompt_user.sh new file mode 100644 index 0000000..64b6e37 --- /dev/null +++ b/tests/test_prompt_user.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/prompt_user.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + diff --git a/tests/test_set_parameter.sh b/tests/test_set_parameter.sh new file mode 100644 index 0000000..b1ac236 --- /dev/null +++ b/tests/test_set_parameter.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +fixtures_directory="$(pwd)/fixtures" +parent_directory="$(dirname "$(pwd)")" +src_file="../src/yaml/set_parameter.sh" +# shellcheck source=/dev/null +. "${src_file}" + +# shellcheck source=/dev/null +. "${parent_directory}/src/yaml/get_parameter.sh" +# shellcheck source=/dev/null +. "${parent_directory}/src/colors/colors.sh" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +} + +test_set_parameter_should_return_expected_error() { + assert_fails "$(set_parameter baz tango -f "${fixtures_directory}/fixture.yaml")" +} + +test_set_parameter_should_return_expected_result() { + set_parameter bar tango -f "${fixtures_directory}/fixture.yaml" + assert_equals 'tango' "$(get_parameter bar -f "${fixtures_directory}/fixture.yaml")" + + set_parameter bar baz -f "${fixtures_directory}/fixture.yaml" + assert_equals 'baz' "$(get_parameter bar -f "${fixtures_directory}/fixture.yaml")" +} diff --git a/tests/test_update_grub.sh b/tests/test_update_grub.sh new file mode 100644 index 0000000..53b544d --- /dev/null +++ b/tests/test_update_grub.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +#/* +# * This file is part of TangoMan Bash Tools package. +# * +# * Copyright (c) 2021 "Matthias Morin" +# * +# * This source file is subject to the MIT license that is bundled +# * with this source code in the file LICENSE. +# */ + +# https://github.com/pgrange/bash_unit +# +# assert "test -e /tmp/the_file" +# assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" +# assert_status_code 25 code +# assert_equals "a string" "another string" "a string should be another string" +# assert_not_equals "a string" "a string" "a string should be different from another string" +# fake ps echo hello world + +src_file="../src/system/update_grub.sh" + +# shellcheck source=/dev/null +. "${src_file}" + +test_script_execution_should_return_expected_status_code() { + assert_status_code 0 "${src_file}" +}