SshConfig
The SshConfig makes it quick and easy to load, parse, and decode/encode the SSH configs. It also helps to resolve the properties by hostname and use them safely in your apps (thanks for Optional and static types in Swift).
Features
- Parsing a SSH config;
- Encode/Decode your config in JSON or text format;
- Resolve the SSH properties for a host by its alias;
- The static type checking SSH properties (yeah...If, in the beginning, I knew that there would be almost 100 properties, I would not begin to describe them!).
Usage
Let's try to load, parse and decode an SSH config file and look at a base scenario of how to work with it.
~/.ssh/config file content:
Host gitlab.com github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
User xxlabaza
Host my*
User admin
Port 2021
Host *
SetEnv POPA=3000
Code example:
The same ssh.Config
instance can be constructed programmatically, like this:
NOTE: I use variadic list of closures for setting needed properties for
ssh.Properties
instance, which creates inside thessh.Host
initializer. I found that approach more similar to the original ssh config file format, plus you can ignore thessh.Properties
's fields order.
More code samples and examples are available in the website and in the tests (especially in UsageExample.swift file).
Installation
Requirements
Swift 5.1+
iOS | watchOS | tvOS | macOS |
---|---|---|---|
13+ | 6+ | 13+ | 10.15+ |
Swift Package Manager
NOTE: the instructions below are for using
SwiftPM
without theXcode UI
. It's the easiest to go to your Project Settings -> Swift Packages and add SshConfig from there.
Swift Package Manager - is the recommended installation method. All you need is to add the following as a dependency to your Package.swift
file:
So, your Package.swift
may look like below:
And then import wherever needed:
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate SshConfig into your Xcode project using CocoaPods, specify it in your Podfile:
Eventually, your Podfile should look like this one:
And then run:
pod install
After installing the cocoapod into your project import SshConfig with: