Skip to content
This repository was archived by the owner on Jul 18, 2019. It is now read-only.
Merged
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
28 changes: 28 additions & 0 deletions cmds/signin/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/pkg/browser"
"github.com/spf13/cobra"
"github.com/taskcluster/taskcluster-cli/cmds/root"
"github.com/taskcluster/taskcluster-cli/config"
tcclient "github.com/taskcluster/taskcluster-client-go"
"github.com/taskcluster/taskcluster-client-go/auth"
graceful "gopkg.in/tylerb/graceful.v1"
)

Expand All @@ -38,6 +41,7 @@ tools using those libraries can also benefit from this signin method.`,

RunE: cmdSignin,
}
cmd.Flags().Bool("check", false, "Check whether you are already signed in")
cmd.Flags().Bool("csh", false, "Output csh-style environment variables (default is Bourne shell)")
cmd.Flags().StringP("name", "n", "cli", "Name of the credential to create/reset.")
cmd.Flags().String("expires", "1d", "Lifetime for this client (keep it short to avoid risk from accidental disclosure).")
Expand All @@ -61,6 +65,10 @@ tools using those libraries can also benefit from this signin method.`,
}

func cmdSignin(cmd *cobra.Command, _ []string) error {
if check, _ := cmd.Flags().GetBool("check"); check {
return checkSignin()
}

// Load configuration
fmt.Fprintln(cmd.OutOrStderr(), "Starting")

Expand Down Expand Up @@ -141,3 +149,23 @@ func cmdSignin(cmd *cobra.Command, _ []string) error {

return nil
}

// Return an appropriate exit code based on whether we have credentials.
// Useful for shell scripting around 'taskcluster signin' calls.
func checkSignin() error {
var creds *tcclient.Credentials
if config.Credentials != nil {
creds = config.Credentials.ToClientCredentials()
}
auth := auth.New(creds)
result, err := auth.CurrentScopes()
if err != nil {
// Don't want an os.Exit() in case it causes scripting loops when used.
return fmt.Errorf("failed to check credentials: %s", err)
}
if len(result.Scopes) == 0 {
fmt.Println("No valid credentials were found")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More accurate to say "No valid credentials were found, or credentials have no scopes".

I think this is a good way to check for access all the same, but if someone found themselves in the weird case of having a clientId with no scopes, the message might help them figure out what's going on.

os.Exit(1)
}
return nil
}