Skip to content

Commit

Permalink
Merge pull request #52 from toshi0383/exit-code
Browse files Browse the repository at this point in the history
set error status code on error
  • Loading branch information
toshi0383 authored Jan 15, 2018
2 parents 3ba1485 + 03f9053 commit 55b58b1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## master
##### Bugfix & Breaking
* [fixed] set error status code on error [#52](https://github.com/toshi0383/cmdshelf/pull/52)
[Toshihiro Suzuki](https://github.com/toshi0383)

* [fixed] Quoted parameter is unquoted [#49](https://github.com/toshi0383/cmdshelf/issues/49)
[Toshihiro Suzuki](https://github.com/toshi0383)
[#50](https://github.com/toshi0383/cmdshelf/issues/50)
Expand Down
6 changes: 3 additions & 3 deletions Sources/cmdshelf/Error.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Foundation

struct CmdshelfError: Error, CustomStringConvertible {
let message: String
init(_ message: String) {
let message: String?
init(_ message: String? = nil) {
self.message = message
}
var description: String {
return message
return message ?? ""
}
}
33 changes: 24 additions & 9 deletions Sources/cmdshelf/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let blob = BlobCommand()

let cat = command(VaradicAliasArgument()) { (aliases) in
if aliases.isEmpty {
shellOut(to: "cat")
exit(shellOut(to: "cat"))
} else {
let config = try Configuration()
config.cloneRemotesIfNeeded()
Expand All @@ -36,23 +36,31 @@ let cat = command(VaradicAliasArgument()) { (aliases) in
continue
}
if context.location.hasPrefix("curl ") {
shellOut(to: context.location)
if shellOut(to: context.location) > 0 {
failure = true
}
} else {
shellOut(to: "cat \(context.location)")
if shellOut(to: "cat \(context.location)") > 0 {
failure = true
}
}
}
if failure {
exit(1)
throw CmdshelfError()
}
}
}

func printHelpMessage() {
queuedPrintln(SubCommand.help.helpMessage)
}

let help = command(SubCommandArgument()) { (subCommand) in
if let subCommand = subCommand {
queuedPrintln(subCommand.helpMessage)
return
}
queuedPrintln(SubCommand.help.helpMessage)
printHelpMessage()
}

let list = command(
Expand All @@ -72,13 +80,13 @@ let run = command(AliasParameterArgument()) { (aliasParam) in
// Search in blobs and remote
guard let context = config.getContexts(for: alias, remoteName: remoteName).first else {
queuedPrintlnError(Message.noSuchCommand(aliasParam.alias.originalValue))
exit(1)
throw CmdshelfError()
}
let singleQuoted = parameters.map { "\'\($0)\'" }.joined(separator: " ")
if context.location.hasPrefix("curl ") {
shellOut(to: "bash <(\(context.location))", argument: singleQuoted)
exit(shellOut(to: "bash <(\(context.location))", argument: singleQuoted))
} else {
shellOut(to: context.location, argument: singleQuoted)
exit(shellOut(to: context.location, argument: singleQuoted))
}
}

Expand Down Expand Up @@ -109,9 +117,16 @@ let c = command(SubCommandConvertibleArgument())

do {
try exec()

} catch let error as CmdshelfError {
if let msg = error.message {
queuedPrintlnError(msg)
}
exit(1)

} catch {
queuedPrintlnError(error)
help.run()
exit(1)
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/main.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash
#
# WARNING!: currently this test modifies your local environment
#
STATUS=0
set +e

Expand Down Expand Up @@ -126,6 +129,51 @@ fi

rm $TMP_006 $TEST_006_SH

## 007: [run] exit status code
before_each

$CMDSHELF run no-such-command 2> /dev/null
exit_status=$?

if [ $exit_status -ne 1 ]
then
echo Exit code is expected to be 1 but was $exit_status
echo 007 FAILED
STATUS=1
fi

## 008: [cat] exit status code
before_each

$CMDSHELF cat nsc01 nsc02 2> /dev/null
exit_status=$?

if [ $exit_status -ne 1 ]
then
echo Exit code is expected to be 1 but was $exit_status
echo 008 FAILED
STATUS=1
fi

## 009: [run] underlying exit status code
before_each

TEST_009_SH=~/.cmdshelf/remote/_cmdshelf-remote/009.sh
printf "#!/bin/bash\nexit 1" > $TEST_009_SH
chmod +x $TEST_009_SH

$CMDSHELF run 009.sh
exit_status=$?

if [ $exit_status -ne 1 ]
then
echo Exit code is expected to be 1 but was $exit_status
echo 009 FAILED
STATUS=1
fi

rm $TEST_009_SH

# Cleanup
after_all

Expand Down

0 comments on commit 55b58b1

Please sign in to comment.