@@ -4,35 +4,15 @@ import libhostmgr
44
55struct SyncAuthorizedKeysCommand : AsyncParsableCommand , FollowsCommandPolicies {
66
7+ enum Constants {
8+ static let s3Key = " authorized_keys "
9+ }
10+
711 static let configuration = CommandConfiguration (
812 commandName: Configuration . SchedulableSyncCommand. authorizedKeys. rawValue,
913 abstract: " Set this machine's authorized_keys file "
1014 )
1115
12- @Option (
13- name: . shortAndLong,
14- help: " The S3 bucket containing the `authorized_keys` file "
15- )
16- var bucket : String = Configuration . shared. authorizedKeysBucket
17-
18- @Option (
19- name: . shortAndLong,
20- help: " The S3 region for the bucket "
21- )
22- var region : String = Configuration . shared. authorizedKeysRegion
23-
24- @Option (
25- name: . shortAndLong,
26- help: " The S3 path to the authorized_keys file "
27- )
28- var key : String = " authorized_keys "
29-
30- @Option (
31- name: . shortAndLong,
32- help: " The path to your authorized_keys file on disk (defaults to ~/.ssh/authorized_keys) "
33- )
34- var destination : String = Paths . authorizedKeysFilePath. path
35-
3616 @OptionGroup
3717 var options : SharedSyncOptions
3818
@@ -44,25 +24,36 @@ struct SyncAuthorizedKeysCommand: AsyncParsableCommand, FollowsCommandPolicies {
4424 ]
4525
4626 func run( ) async throws {
27+ let destination = Paths . authorizedKeysFilePath
28+
4729 try to ( evaluateCommandPolicies ( ) , unless: options. force)
48- logger. debug ( " Job schedule allows for running " )
4930
50- logger . info ( " Downloading file from s3:// \( bucket ) / \( key ) in \( region ) to \( destination ) " )
31+ Console . heading ( " Syncing Authorized Keys " )
5132
52- let s3Manager = S3Manager ( bucket: self . bucket, region: self . region)
33+ let s3Manager = S3Manager (
34+ bucket: Configuration . shared. authorizedKeysBucket,
35+ region: Configuration . shared. authorizedKeysRegion
36+ )
5337
54- guard let object = try await s3Manager. lookupObject ( atPath: key ) else {
55- logger . error ( " Unable to locate authorized_keys file – exiting " )
38+ guard let object = try await s3Manager. lookupObject ( atPath: Constants . s3Key ) else {
39+ Console . error ( " Unable to locate authorized_keys file – exiting " )
5640 throw ExitCode ( rawValue: 1 )
5741 }
5842
59- let url = URL ( fileURLWithPath: self . destination)
60- try await s3Manager. download ( object: object, to: url, progressCallback: nil )
43+ let progressBar = Console . startFileDownload ( object)
44+
45+ try await s3Manager. download (
46+ object: object,
47+ to: destination,
48+ progressCallback: progressBar. update
49+ )
6150
6251 /// Fix the permissions on the file, if needed
52+ Console . info ( " Setting file permissions on \( destination) " )
6353 try FileManager . default. setAttributes ( [
6454 . posixPermissions: 0o600
65- ] , ofItemAtPath: self . destination)
55+ ] , ofItemAt: destination)
56+ Console . success ( " Authorized Key Sync Complete " )
6657
6758 try recordLastRun ( )
6859 }
0 commit comments