-
Notifications
You must be signed in to change notification settings - Fork 21
Add valkey client cli application #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ae4066
Add valkey client cli application
adam-fowler 281c2bc
Update usage message
adam-fowler ede5fa5
Merge branch 'main' into cli
adam-fowler aeaa626
Add `@available`
adam-fowler b2ce4cd
Cleanup extracting host and port from commandline
adam-fowler 0e7dbd7
Parse speech marks when splitting
adam-fowler 4143c47
Merge branch 'main' into cli
adam-fowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // | ||
| // This source file is part of the valkey-swift project | ||
| // Copyright (c) 2026 the valkey-swift project authors | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| import Logging | ||
| import Valkey | ||
|
|
||
| #if canImport(Darwin) | ||
| import Darwin | ||
| #elseif os(Windows) | ||
| import ucrt | ||
| #elseif canImport(Glibc) | ||
| import Glibc | ||
| #elseif canImport(Musl) | ||
| import Musl | ||
| #else | ||
| #error("Platform not supported") | ||
| #endif | ||
|
|
||
| @main | ||
| struct App { | ||
| static func usage() -> Never { | ||
| print("Usage: ValkeyCLI <host/ip> <port>") | ||
| exit(-1) | ||
| } | ||
|
|
||
| static func main() async throws { | ||
| let arguments = CommandLine.arguments | ||
| guard arguments.count > 2 else { usage() } | ||
| guard let port = Int(arguments[2]) else { usage() } | ||
| print("Connecting to \(arguments[1]):\(port)") | ||
| let valkeyClient = ValkeyClient(.hostname(arguments[1], port: port), logger: Logger(label: "ValkeyCLI")) | ||
| async let _ = valkeyClient.run() | ||
| while true { | ||
| print("> ", terminator: "") | ||
| guard let input = readLine() else { | ||
| print("") | ||
| return | ||
| } | ||
| let split = input.split(separator: " ") | ||
| guard let commandName = split.first else { continue } | ||
| let command = ValkeyRawCommand(String(commandName), parameters: split.dropFirst().map { String($0) }) | ||
|
nilanshu-sharma marked this conversation as resolved.
|
||
| do { | ||
| let response = try await valkeyClient.execute(command) | ||
| print(response.value.descriptionWith(redact: false)) | ||
| } catch { | ||
| if error.errorCode == .commandError { | ||
| print(error.message ?? "Unknown Error") | ||
| } else { | ||
| throw error | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Wrapper for Valkey command that returns the response as a `RESPToken` | ||
| @usableFromInline | ||
| struct ValkeyRawCommand: ValkeyCommand { | ||
|
nilanshu-sharma marked this conversation as resolved.
|
||
| @usableFromInline | ||
| let command: String | ||
| @usableFromInline | ||
| let parameters: [String] | ||
|
|
||
| @inlinable | ||
| static var name: String { "RAW" } | ||
|
|
||
| @inlinable | ||
| init(_ command: String, parameters: [String]) { | ||
| self.command = command | ||
| self.parameters = parameters | ||
| } | ||
|
|
||
| @usableFromInline | ||
| var keysAffected: [ValkeyKey] { [] } | ||
|
|
||
| @inlinable | ||
| func encode(into commandEncoder: inout ValkeyCommandEncoder) { | ||
| commandEncoder.encodeArray(command, parameters) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.