Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 81f54f3

Browse files
committed
Entire roundtrip
1 parent 65ab091 commit 81f54f3

3 files changed

Lines changed: 58 additions & 19 deletions

File tree

ComputeInstanceStatus/AppDelegate.swift

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,92 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2222

2323
var statusItem: NSStatusItem!
2424
let popover = NSPopover()
25+
let menu = NSMenu(title: "Menu")
26+
let statusMenuItem = NSMenuItem(title: "Status unknown", action: nil, keyEquivalent: "")
27+
let startMenuItem = NSMenuItem(title: "Start", action: #selector(start), keyEquivalent: "")
28+
let stopMenuItem = NSMenuItem(title: "Stop", action: #selector(stop), keyEquivalent: "")
2529

2630
var lastStatus: Status? = nil {
2731
didSet {
2832
switch lastStatus {
2933
case .some(.running):
3034
statusItem.button?.title = "🌝"
35+
statusMenuItem.title = "Running"
36+
startMenuItem.isHidden = true
37+
stopMenuItem.isHidden = false
3138
case .some(.staging):
3239
statusItem.button?.title = "🌜"
40+
statusMenuItem.title = "Staging"
41+
startMenuItem.isHidden = true
42+
stopMenuItem.isHidden = false
3343
case .some(.stopping):
3444
statusItem.button?.title = "🌛"
45+
statusMenuItem.title = "Stopping"
46+
startMenuItem.isHidden = true
47+
stopMenuItem.isHidden = true
48+
case .some(.terminated):
49+
statusItem.button?.title = "🌚"
50+
statusMenuItem.title = "Terminated"
51+
startMenuItem.isHidden = false
52+
stopMenuItem.isHidden = true
3553
default:
3654
statusItem.button?.title = "🌚"
55+
statusMenuItem.title = "Status unknown"
56+
startMenuItem.isHidden = true
57+
stopMenuItem.isHidden = true
3758
}
3859
}
3960
}
40-
61+
4162
func applicationDidFinishLaunching(_ aNotification: Notification) {
42-
4363
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
64+
statusMenuItem.isEnabled = false
4465

45-
let menu = NSMenu(title: "Menu")
66+
startMenuItem.isHidden = true
67+
stopMenuItem.isHidden = true
68+
69+
menu.addItem(statusMenuItem)
70+
menu.addItem(NSMenuItem.separator())
71+
menu.addItem(startMenuItem)
72+
menu.addItem(stopMenuItem)
4673
menu.addItem(withTitle: "Preferences", action: #selector(openPreferences), keyEquivalent: "")
4774

4875
statusItem.button?.title = "🌚"
4976
statusItem.button?.target = self
5077
statusItem.menu = menu
5178

52-
// Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { [weak self] _ in
53-
// GCPService.getInstanceState().then { [weak self] status in
54-
// self?.lastStatus = status
55-
// }.trap { err in
56-
// print("Error: \(err)")
57-
// }
58-
// }
79+
Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { [weak self] _ in
80+
self?.runCommand(command: "status")
81+
}
5982
}
6083

84+
@objc func start() {
85+
runCommand(command: "start")
86+
}
87+
88+
@objc func stop(){
89+
runCommand(command: "stop")
90+
}
6191

6292
@objc func openPreferences() {
6393
let vc = PreferencesViewController.instantiate()
6494
let window = NSWindow(contentViewController: vc)
6595
window.title = "Preferences"
6696
window.orderFront(nil)
6797
}
98+
99+
private func runCommand(command: String) {
100+
guard let serviceAccountPath = UserDefaultsService.serviceAccountFileBookmark,
101+
let zone = UserDefaultsService.zone,
102+
let instanceName = UserDefaultsService.instanceName
103+
else { return }
104+
105+
GCPService.runCommand(serviceAccountPath.path, zone, instanceName, command)
106+
.then { [weak self] status in
107+
self?.lastStatus = status
108+
}.trap { err in
109+
print("Error: \(err)")
110+
}
111+
}
68112
}
69113

ComputeInstanceStatus/GCPService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Promissum
1111

1212
class GCPService {
1313

14-
static func getInstanceState(_ serviceAccountPath: String, _ zone: String, _ instanceName: String) -> Promise<Status, Error> {
14+
static func runCommand(_ serviceAccountPath: String, _ zone: String, _ instanceName: String, _ command: String = "status") -> Promise<Status, Error> {
1515
guard
1616
let cliPath = Bundle.main.path(forResource: "index-macos", ofType: nil)
1717
else {
@@ -23,7 +23,7 @@ class GCPService {
2323
DispatchQueue.global(qos: .background).async {
2424
let task = Process()
2525
task.launchPath = cliPath
26-
task.arguments = ["status", "-z", zone, "-i", instanceName]
26+
task.arguments = [command, "-z", zone, "-i", instanceName]
2727
task.environment = ["GOOGLE_APPLICATION_CREDENTIALS": serviceAccountPath]
2828

2929
let pipe = Pipe()

ComputeInstanceStatus/PreferencesViewController.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,10 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate {
5555
return
5656
}
5757

58-
do {
59-
self.serviceAccountFileBookmark = url
60-
} catch {
61-
print("Don't handle this for now")
62-
}
58+
self.serviceAccountFileBookmark = url
6359
}
6460

6561
@IBAction func didTouchTestConnection(_ sender: Any) {
66-
var isStale = false
6762
guard
6863
let serviceAccountFileBookmark = serviceAccountFileBookmark,
6964
let zone = zone,
@@ -73,7 +68,7 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate {
7368
self.imageView.isHidden = true
7469
progressIndicator.startAnimation(nil)
7570

76-
GCPService.getInstanceState(serviceAccountFileBookmark.path, zone, instanceName)
71+
GCPService.runCommand(serviceAccountFileBookmark.path, zone, instanceName)
7772
.then { [weak self] state in
7873
self?.imageView.isHidden = false
7974
self?.imageView.image = NSImage(named: "NSStatusAvailable")

0 commit comments

Comments
 (0)