2020-09-22 06:12:50 +00:00
|
|
|
import Cocoa
|
|
|
|
import SwiftUI
|
|
|
|
import SecretKit
|
2022-01-02 00:43:29 +00:00
|
|
|
import SecureEnclaveSecretKit
|
|
|
|
import SmartCardSecretKit
|
2020-09-22 06:12:50 +00:00
|
|
|
import Brief
|
|
|
|
|
|
|
|
@main
|
|
|
|
struct Secretive: App {
|
|
|
|
|
|
|
|
private let storeList: SecretStoreList = {
|
|
|
|
let list = SecretStoreList()
|
|
|
|
list.add(store: SecureEnclave.Store())
|
|
|
|
list.add(store: SmartCard.Store())
|
|
|
|
return list
|
|
|
|
}()
|
|
|
|
private let agentStatusChecker = AgentStatusChecker()
|
|
|
|
private let justUpdatedChecker = JustUpdatedChecker()
|
2022-02-12 06:52:45 +00:00
|
|
|
private let updaterController = UpdaterCommunicationController()
|
2020-09-22 06:12:50 +00:00
|
|
|
|
|
|
|
@AppStorage("defaultsHasRunSetup") var hasRunSetup = false
|
|
|
|
@State private var showingSetup = false
|
|
|
|
@State private var showingCreation = false
|
|
|
|
|
|
|
|
@SceneBuilder var body: some Scene {
|
|
|
|
WindowGroup {
|
2022-02-12 07:31:03 +00:00
|
|
|
ContentView<UpdateChecker, AgentStatusChecker>(showingCreation: $showingCreation, runningSetup: $showingSetup, hasRunSetup: $hasRunSetup)
|
2020-09-22 06:12:50 +00:00
|
|
|
.environmentObject(storeList)
|
2022-02-12 07:31:03 +00:00
|
|
|
.environmentObject(UpdateChecker(checkOnLaunch: hasRunSetup))
|
2020-09-22 06:12:50 +00:00
|
|
|
.environmentObject(agentStatusChecker)
|
|
|
|
.onAppear {
|
2022-02-17 06:02:57 +00:00
|
|
|
updaterController.installUpdate(url: URL(string: "https://github.com/maxgoedjen/secretive/releases/download/v2.1.1/Secretive.zip")!)
|
2020-09-22 06:12:50 +00:00
|
|
|
if !hasRunSetup {
|
|
|
|
showingSetup = true
|
2020-11-12 01:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 06:55:52 +00:00
|
|
|
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
|
|
|
|
guard hasRunSetup else { return }
|
2020-11-12 01:00:15 +00:00
|
|
|
agentStatusChecker.check()
|
2020-11-13 06:55:52 +00:00
|
|
|
if agentStatusChecker.running && justUpdatedChecker.justUpdated {
|
|
|
|
// Relaunch the agent, since it'll be running from earlier update still
|
|
|
|
reinstallAgent()
|
2021-12-31 22:59:12 +00:00
|
|
|
} else if !agentStatusChecker.running && !agentStatusChecker.developmentBuild {
|
2020-11-13 06:55:52 +00:00
|
|
|
forceLaunchAgent()
|
2020-09-22 06:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.commands {
|
|
|
|
CommandGroup(after: CommandGroupPlacement.newItem) {
|
|
|
|
Button("New Secret") {
|
|
|
|
showingCreation = true
|
|
|
|
}
|
|
|
|
.keyboardShortcut(KeyboardShortcut(KeyEquivalent("N"), modifiers: [.command, .shift]))
|
|
|
|
}
|
|
|
|
CommandGroup(replacing: .help) {
|
|
|
|
Button("Help") {
|
|
|
|
NSWorkspace.shared.open(Constants.helpURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CommandGroup(after: .help) {
|
|
|
|
Button("Setup Secretive") {
|
|
|
|
showingSetup = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SidebarCommands()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-13 06:55:52 +00:00
|
|
|
extension Secretive {
|
|
|
|
|
|
|
|
private func reinstallAgent() {
|
|
|
|
justUpdatedChecker.check()
|
|
|
|
LaunchAgentController().install {
|
|
|
|
// Wait a second for launchd to kick in (next runloop isn't enough).
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
|
agentStatusChecker.check()
|
|
|
|
if !agentStatusChecker.running {
|
|
|
|
forceLaunchAgent()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func forceLaunchAgent() {
|
|
|
|
// We've run setup, we didn't just update, launchd is just not doing it's thing.
|
|
|
|
// Force a launch directly.
|
|
|
|
LaunchAgentController().forceLaunch { _ in
|
|
|
|
agentStatusChecker.check()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:12:50 +00:00
|
|
|
|
|
|
|
private enum Constants {
|
|
|
|
static let helpURL = URL(string: "https://github.com/maxgoedjen/secretive/blob/main/FAQ.md")!
|
|
|
|
}
|
|
|
|
|