Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exit code in addition of print for "test" #85

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ $ oauth2l info --token $(oauth2l fetch --scope pubsub)
### test

Test a token. This sets an exit code of 0 for a valid token and 1 otherwise,
which can be useful in shell pipelines.
which can be useful in shell pipelines. It also prints the exit code.

```bash
$ oauth2l test --token ya29.zyxwvutsrqpnmolkjihgfedcba
0
$ echo $?
0
$ oauth2l test --token ya29.justkiddingmadethisoneup
1
$ echo $?
1
```
Expand Down
2 changes: 1 addition & 1 deletion integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestCLI(t *testing.T) {
"test; invalid token",
[]string{"test", "--token", "invalid-token"},
"test-invalid-token.golden",
false,
true,
},
{
"reset",
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func main() {
}

// Tasks that verify the existing token.
infoTasks := map[string]func(string){
infoTasks := map[string](func(string) int){
"info": util.Info,
"test": util.Test,
}
Expand Down Expand Up @@ -371,7 +371,7 @@ func main() {
}
}

task(token)
os.Exit(task(token))
} else if cmd == "reset" {
setCacheLocation(opts.Reset.Cache)
util.Reset()
Expand Down
7 changes: 5 additions & 2 deletions util/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,26 @@ func Curl(settings *sgauth.Settings, args ...string) {
}

// Fetches the information of the given token.
func Info(token string) {
func Info(token string) int {
info, err := getTokenInfo(token)
if err != nil {
fmt.Print(err)
} else {
fmt.Println(info)
}
return 0
}

// Tests the given token. Returns 0 for valid tokens.
// Otherwise returns 1.
func Test(token string) {
func Test(token string) int {
_, err := getTokenInfo(token)
if err != nil {
fmt.Println(1)
return 1
} else {
fmt.Println(0)
return 0
}
}

Expand Down