{"id":17037812,"url":"https://github.com/qiuzhifei/swift-commands","last_synced_at":"2025-04-12T12:50:32.013Z","repository":{"id":47106739,"uuid":"392275128","full_name":"QiuZhiFei/swift-commands","owner":"QiuZhiFei","description":"Swift utilities for running commands.","archived":false,"fork":false,"pushed_at":"2024-06-02T08:51:19.000Z","size":151,"stargazers_count":53,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-08T09:37:10.007Z","etag":null,"topics":["commands","macos","spm","subprocess","swift","swift-commands","swift-package-manager"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/QiuZhiFei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-03T10:17:58.000Z","updated_at":"2024-06-02T08:51:23.000Z","dependencies_parsed_at":"2024-10-14T09:05:15.610Z","dependency_job_id":null,"html_url":"https://github.com/QiuZhiFei/swift-commands","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"44e6aac62a1e2fd42cae7391f6cfa4eb7a798a22"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QiuZhiFei%2Fswift-commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QiuZhiFei%2Fswift-commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QiuZhiFei%2Fswift-commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QiuZhiFei%2Fswift-commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QiuZhiFei","download_url":"https://codeload.github.com/QiuZhiFei/swift-commands/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248571329,"owners_count":21126516,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["commands","macos","spm","subprocess","swift","swift-commands","swift-package-manager"],"created_at":"2024-10-14T08:55:12.320Z","updated_at":"2025-04-12T12:50:32.007Z","avatar_url":"https://github.com/QiuZhiFei.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Swift Commands\n![SPM](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)\n![CocoaPods](https://img.shields.io/cocoapods/v/Commands?color=green)\n[![CI Status](https://img.shields.io/github/actions/workflow/status/qiuzhifei/swift-commands/swift.yml)](https://github.com/qiuzhifei/swift-commands/actions)\n[![License](https://img.shields.io/github/license/qiuzhifei/swift-commands)](https://github.com/qiuzhifei/swift-commands/blob/main/LICENSE)\n![Platform](https://img.shields.io/badge/platforms-macOS%2011.0-orange)\n\nSwift utilities for running commands.\n\nThe `Commands` module allows you to take a system command as a string and return the standard output.\n\n[API documentation can be found here](https://qiuzhifei.github.io/swift-commands/).\n\n## Usage\n```\nimport Commands\n```\n\n### Bash\nExecute shell commands.\n```swift\nlet result = Commands.Task.run(\"bash -c ls\")\n```\nOr\n```swift\nlet result = Commands.Task.run([\"bash\", \"-c\", \"ls\"])\n```\nOr\n```swift\nlet result = Commands.Bash.run(\"ls\")\n```\n\n### Python\nExecute python scripts.\n```swift\nlet result = Commands.Task.run(\"python main.py\")\n```\nExecute python commands.\n```swift\nlet result = Commands.Task.run(\"python -c import base64; print(base64.b64encode(b'qiuzhifei').decode('ascii'))\")\n```\nOr\n```swift\nlet result = Commands.Python.run(\"import base64; print(base64.b64encode(b'qiuzhifei').decode('ascii'))\")\n```\n\n### Ruby\nExecute ruby scripts.\n```swift\nlet result = Commands.Task.run(\"ruby main.rb\")\n```\nExecute ruby commands.\n```swift\nlet result = Commands.Task.run(\"ruby -e require 'base64'; puts Base64.encode64('qiuzhifei')\")\n```\nOr\n```swift\nlet result = Commands.Ruby.run(\"require 'base64'; puts Base64.encode64('qiuzhifei')\")\n```\n\n### Alias\nCreate a shortcut name for a command.\n```swift\nlet node = Commands.Alias(\"/usr/local/bin/node\", dashc: \"-e\")\nlet result = node.run(\"console.log('qiuzhifei')\")\n```\n\n### Setting global environment variables\n```swift\nCommands.ENV.global[\"http_proxy\"] = \"http://127.0.0.1:7890\"\n```\n```swift\nCommands.ENV.global.add(PATH: \"/Users/zhifeiqiu/.rvm/bin\")\n```\n\n### Making Commands\n```swift\nlet request: Commands.Request = \"ruby -v\"\n```\nOr\n```swift\nlet request: Commands.Request = [\"ruby\", \"-v\"]\n```\nOr\n```swift\nlet request = Commands.Request(executableURL: \"ruby\", arguments: \"-v\")\n```\nChange environment variables\n```swift\nvar request: Commands.Request = \"ruby -v\"\nrequest.environment?.add(PATH: \"/usr/local/bin\")\nrequest.environment?[\"http_proxy\"] = \"http://127.0.0.1:7890\"\nrequest.environment?[\"https_proxy\"] = \"http://127.0.0.1:7890\"\nrequest.environment?[\"all_proxy\"] = \"socks5://127.0.0.1:7890\"\n\nlet result = Commands.Task.run(request)\n```\n\n### Result Handler\nReturns the `Commands.Result` of running cmd in a subprocess.\n```swift\nlet result = Commands.Task.run(\"ruby -v\")\nswitch result {\ncase .Success(let request, let response):\n  debugPrint(\"command: \\(request.absoluteCommand), success output: \\(response.output)\")\ncase .Failure(let request, let response):\n  debugPrint(\"command: \\(request.absoluteCommand), failure output: \\(response.errorOutput)\")\n}\n```\n\n## Adding Commands as a Dependency\nTo use the `Commands` library in a SwiftPM project, \nadd the following line to the dependencies in your `Package.swift` file:\n\n```swift\nlet package = Package(\n    // name, platforms, products, etc.\n    dependencies: [\n        .package(url: \"https://github.com/qiuzhifei/swift-commands\", from: \"0.6.0\"),\n        // other dependencies\n    ],\n    targets: [\n        .target(name: \"\u003ccommand-line-tool\u003e\", dependencies: [\n            .product(name: \"Commands\", package: \"swift-commands\"),\n        ]),\n        // other targets\n    ]\n)\n```\n\n## CocoaPods (OS X 11.0+)\nYou can use [CocoaPods](http://cocoapods.org/) to install `Commands` by adding it to your `Podfile`:\n```ruby\npod 'Commands',        '~\u003e 0.7.0'\n```\n\n## QuickStart\n```shell\ngit clone https://github.com/QiuZhiFei/swift-commands\ncd swift-commands \u0026\u0026 open Package.swift\n```\n\n## References\n- [https://github.com/apple/swift-argument-parser](https://github.com/apple/swift-argument-parser)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqiuzhifei%2Fswift-commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqiuzhifei%2Fswift-commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqiuzhifei%2Fswift-commands/lists"}