forked from mas-cli/mas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mas-cli#698 from rgoldberg/666-config
Add `config` command
- Loading branch information
Showing
13 changed files
with
187 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// Config.swift | ||
// mas | ||
// | ||
// Created by Ross Goldberg on 2025-01-03. | ||
// Copyright © 2024 mas-cli. All rights reserved. | ||
// | ||
|
||
import ArgumentParser | ||
import CommerceKit | ||
|
||
private let unknown = "unknown" | ||
|
||
extension MAS { | ||
/// Displays mas config & related system info. | ||
struct Config: ParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
abstract: "Display mas config & related system info" | ||
) | ||
|
||
@Flag(help: "Output as Markdown") | ||
var markdown = false | ||
|
||
/// Runs the command. | ||
func run() throws { | ||
if markdown { | ||
print("```text") | ||
} | ||
print( | ||
""" | ||
mas ▁▁▁▁ \(Package.version) | ||
from ▁▁▁ \(Package.installMethod) | ||
origin ▁ \(Package.gitOrigin) | ||
rev ▁▁▁▁ \(Package.gitRevision) | ||
swift ▁▁ \(Package.swiftVersion) | ||
driver ▁ \(Package.swiftDriverVersion) | ||
region ▁ \(Storefront.isoRegion?.alpha2 ?? unknown) | ||
macos ▁▁ \( | ||
ProcessInfo.processInfo.operatingSystemVersionString.dropFirst(8) | ||
.replacingOccurrences(of: "Build ", with: "") | ||
) | ||
mac ▁▁▁▁ \(macModel()) | ||
cpu ▁▁▁▁ \(cpuBrandString()) | ||
arch ▁▁▁ \(architecture()) | ||
""" | ||
) | ||
if markdown { | ||
print("```") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func macModel() -> String { | ||
var name = [CTL_HW, HW_MODEL] | ||
|
||
var size = 0 | ||
guard sysctl(&name, u_int(name.count), nil, &size, nil, 0) == 0 else { | ||
perror("sysctl") | ||
return unknown | ||
} | ||
|
||
var buffer = [CChar](repeating: 0, count: size) | ||
guard sysctl(&name, u_int(name.count), &buffer, &size, nil, 0) == 0 else { | ||
perror("sysctl") | ||
return unknown | ||
} | ||
|
||
return String(cString: buffer) | ||
} | ||
|
||
private func cpuBrandString() -> String { | ||
configValue("machdep.cpu.brand_string") | ||
} | ||
|
||
private func architecture() -> String { | ||
configValue("hw.machine") | ||
} | ||
|
||
private func configValue(_ name: String) -> String { | ||
var size = 0 | ||
guard sysctlbyname(name, nil, &size, nil, 0) == 0 else { | ||
perror("sysctlbyname") | ||
return unknown | ||
} | ||
|
||
var buffer = [CChar](repeating: 0, count: size) | ||
guard sysctlbyname(name, &buffer, &size, nil, 0) == 0 else { | ||
perror("sysctlbyname") | ||
return unknown | ||
} | ||
|
||
return String(cString: buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/zsh -Ndefgku | ||
# | ||
# script/generate_package_swift | ||
# mas | ||
# | ||
# Generates a file to provide the mas version to Swift code. | ||
# | ||
|
||
. "${0:a:h}/_setup_script" | ||
|
||
# Write version to Swift singleton | ||
cat <<EOF >Sources/mas/Package.swift | ||
/// Generated by \`script/generate_package_swift\`. | ||
enum Package { | ||
static let version = "$(script/version)" | ||
static let installMethod = "${1:-unknown}" | ||
static let gitOrigin = "$(git remote get-url origin)" | ||
static let gitRevision = "$(git rev-parse HEAD)" | ||
static let swiftVersion = "$(printf %s "${${$(swift --version 2>/dev/null)#Apple Swift version }%%$'\n'*}")" | ||
static let swiftDriverVersion = "$(printf %s "${${$((swift --version 3>&2 2>&1 1>&3) 2>/dev/null)#swift-driver version: }% }")" | ||
} | ||
EOF |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.