Skip to content

Commit

Permalink
Add a repl command
Browse files Browse the repository at this point in the history
  • Loading branch information
iainsmith committed Apr 30, 2020
1 parent bf82ac6 commit 8c48fbc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `swift docker test` defaults to using the swift-tools-version specified in Package.swift

### Added
- `swift docker repl` - Print the command to start the swift repl in a container
- `swift docker vapor` - Run your vapor application in a container
- `swift docker run` - mirrors swift run command
- `swift docker build` - mirrors swift build
Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ swift docker write-dockerfile # Write a ./Dockerfile to the repo
* [x] Uses the swift docker image that matches the Package.swift manifest.
* [x] Quickly free up space - `swift docker cleanup`
* [x] Create a dockerfile for your project
* [x] Quickly print a command to run the swift repl in the container - `swift docker repl`
* [ ] Create a .dockerignore file to avoid adding .git directory to the image
* [ ] Support multistage slim builds
* [ ] Log output to a file
Expand Down Expand Up @@ -70,26 +71,30 @@ And install docker if you don't have it already
## Usage

```bash
OVERVIEW: Build and test your swift packages in docker
OVERVIEW: A simple workflow for building & testing swift packages with docker

Simple commands for working with the official swift docker images
https://hub.docker.com/_/swift
Run swift docker <subcommand> --help for subcommand details
Reference - Offical docker images: https://hub.docker.com/_/swift

examples:
Examples:

swift docker test #test the package in the current directory
swift docker test --swift 5.1 # test your package against swift:5.1
swift docker test --path ~/code/my-package # test a package in a directory
swift docker write-dockerfile --swift 5.2.2-slim
swift docker cleanup # Remove all images created with swift docker test
swift docker test
swift docker build -- --configuration release
swift docker run your-executable --flag1
swift docker vapor

USAGE: swift-docker <subcommand>

OPTIONS:
-h, --help Show help information.

SUBCOMMANDS:
build Build your swift package in a docker container.
test Test your swift package in a docker container.
run Run your swift package in a docker container.
vapor Run your vapor web application in a container.
build-image Build a docker image for your swift package.
repl print the command to run the swift repl in a container.
cleanup Remove temporary docker images.
write-dockerfile Write a dockerfile to disk.
```
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDockerLib/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct SwiftDockerCLI: ParsableCommand {
swift docker vapor
""",
shouldDisplay: true,
subcommands: [BuildCommand.self, TestCommand.self, RunCommand.self, VaporCommand.self, BuildImageCommand.self, CleanupCommand.self, WriteDockerfileCommand.self]
subcommands: [BuildCommand.self, TestCommand.self, RunCommand.self, VaporCommand.self, BuildImageCommand.self, ReplCommand.self, CleanupCommand.self, WriteDockerfileCommand.self]
)

public init() {}
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftDockerLib/DockerCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ protocol DockerCommand {
}

extension DockerCommand {
var lldbPermissions: String {
"--cap-add=SYS_PTRACE --security-opt seccomp=unconfined --security-opt apparmor=unconfined"
}

func makeLabels(action: ActionLabel) -> String {
let projectLabel = FolderLabel.label(with: options.projectName)
return """
Expand Down
57 changes: 57 additions & 0 deletions Sources/SwiftDockerLib/ReplyCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import ArgumentParser
import Foundation
import TSCBasic

public struct ReplCommand: ParsableCommand, DockerCommand {
public static var configuration = CommandConfiguration(
commandName: "repl",
abstract: "print the command to run the swift repl in a container.",
discussion: """
""",
shouldDisplay: true
)

@OptionGroup()
var options: CLIOptions
var output: OutputDestination = TerminalController(stream: stdoutStream) ?? stdoutStream
var shell: ShellProtocol.Type = ShellRunner.self

public func run() throws {
let labels = makeLabels(action: .buildForTesting) // Do we want different labels
try removeVolumeIfNeeded()
try createVolumeIfNeeded(labels: labels)

output.writeLine("-> swift - \(options.dockerBaseImage.fullName)")
var cmd = "swift"

if !options.args.isEmpty { cmd += " \(options.args.joined(separator: " "))" }
let command = makeDockerRunCommand(cmd: cmd, labels: "", dockerFlags: "\(lldbPermissions) -it")
// TODO: Figure out if we can start an interactive repl directly
let message = """
Run the below command to start the swift repl in the container.
\(command)
"""
if options.verbose {
output.writeLine(message)
} else {
output.writeLine(command)
}
}

public init() {}

init(
options: CLIOptions,
output: OutputDestination = TerminalController(stream: stdoutStream) ?? stdoutStream,
shell: ShellProtocol.Type = ShellRunner.self
) {
self.options = options
self.output = output
self.shell = shell
}

enum CodingKeys: String, CodingKey {
case options
}
}

0 comments on commit 8c48fbc

Please sign in to comment.