Skip to content

Commit

Permalink
Add overwrite option to mint install (#186)
Browse files Browse the repository at this point in the history
* Add overwrite option to mint install

* Change the overwrite option from Flag to Key

* Update Sources/MintCLI/Commands/InstallCommand.swift

Co-authored-by: Yonas Kolb <yonaskolb@users.noreply.github.com>

* Add changelog

Co-authored-by: Yonas Kolb <yonaskolb@users.noreply.github.com>
  • Loading branch information
Econa77 and yonaskolb committed Oct 23, 2020
1 parent b7210cf commit e79bd62
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

## Master

#### Added
- Added `--overwrite` option to `mint install` [#186](https://github.com/yonaskolb/Mint/pull/186) @Econa77

## 0.15.0

#### Added
Expand Down
5 changes: 4 additions & 1 deletion Sources/MintCLI/Commands/InstallCommand.swift
Expand Up @@ -12,6 +12,9 @@ class InstallCommand: PackageCommand {
@Flag("-f", "--force", description: "Force a reinstall even if the package is already installed")
var force: Bool

@Key("-o", "--overwrite", description: "Automatically overwrite a symlinked executable that is not installed by mint without asking. Either (y/n)")
var overwrite: Bool?

init(mint: Mint) {
super.init(mint: mint,
name: "install",
Expand All @@ -21,6 +24,6 @@ class InstallCommand: PackageCommand {

override func execute(package: PackageReference) throws {
let link = !noLink
try mint.install(package: package, executable: executable, force: force, link: link)
try mint.install(package: package, executable: executable, force: force, link: link, overwrite: overwrite)
}
}
21 changes: 13 additions & 8 deletions Sources/MintKit/Mint.swift
Expand Up @@ -268,7 +268,7 @@ public class Mint {

@discardableResult
/// returns if the package was installed
public func install(package: PackageReference, executable: String? = nil, beforeOtherCommand: Bool = false, force: Bool = false, link: Bool = false, noInstall: Bool = false) throws -> Bool {
public func install(package: PackageReference, executable: String? = nil, beforeOtherCommand: Bool = false, force: Bool = false, link: Bool = false, noInstall: Bool = false, overwrite: Bool? = nil) throws -> Bool {

try resolvePackage(package)

Expand All @@ -286,11 +286,11 @@ public class Mint {
}
if link {
if let executable = executable {
try linkPackage(package, executable: executable)
try linkPackage(package, executable: executable, overwrite: overwrite)
} else {
let executables = try packagePath.getExecutables()
for executable in executables {
try linkPackage(package, executable: executable)
try linkPackage(package, executable: executable, overwrite: overwrite)
}
}
}
Expand Down Expand Up @@ -388,10 +388,10 @@ public class Mint {

if link {
if let executable = executable {
try linkPackage(package, executable: executable)
try linkPackage(package, executable: executable, overwrite: overwrite)
} else {
for executable in executables {
try linkPackage(package, executable: executable)
try linkPackage(package, executable: executable, overwrite: overwrite)
}
}
}
Expand Down Expand Up @@ -425,18 +425,23 @@ public class Mint {
}
}

func linkPackage(_ package: PackageReference, executable: String) throws {
func linkPackage(_ package: PackageReference, executable: String, overwrite: Bool?) throws {

let packagePath = PackagePath(path: packagesPath, package: package, executable: executable)
let installPath = linkPath + packagePath.executable!

let installStatus = try InstallStatus(path: installPath, mintPackagesPath: packagesPath)

if let warning = installStatus.warning {
let ok = Input.confirmation("🌱 \(warning)\nOverwrite it with Mint's symlink?".yellow)
if !ok {
if let overwrite = overwrite, !overwrite {
return
}
if overwrite == nil {
let ok = Input.confirmation("🌱 \(warning)\nOverwrite it with Mint's symlink?".yellow)
if !ok {
return
}
}
}

try? installPath.delete()
Expand Down

0 comments on commit e79bd62

Please sign in to comment.