add better error messages

This commit is contained in:
Jay Gabriels
2019-08-04 14:48:31 -07:00
parent c1ac184209
commit 16ed2f63cf
7 changed files with 192 additions and 33 deletions

View File

@@ -11,6 +11,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
### Fixed
### Security
## [1.1.5] - 08/03/19
### Added
- Tests for configs
### Changed
- Error messages
### Deprecated
### Removed
### Fixed
### Security
## [1.1.4] - 08/03/19
### Added
### Changed

View File

@@ -77,7 +77,7 @@ $ security find-internet-password -s gitlab.com | grep "acct" | awk -F\" '{ pri
- If org is behind SSO a normal token will not work. You will need to add SSO to the [Github token](https://help.github.com/articles/authorizing-a-personal-access-token-for-use-with-a-saml-single-sign-on-organization/)
## Bitbucket Setup
To configure with bitbucket you will need to create a new [app password](https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html) and update your `$HOME/ghorg/conf.yaml` to use those values or set the command line args.
To configure with bitbucket you will need to create a new [app password](https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html) and update your `$HOME/ghorg/conf.yaml` or use the (--token, -t) and (--bitbucket-username) flags.
## Known issues
- When cloning if you see something like `Username for 'https://gitlab.com': ` the command won't finish. I haven't been able to identify the reason for this occuring. The fix for this is to make sure your token is in the osxkeychain. See the troubleshooting section for how to set this up.

View File

@@ -92,8 +92,17 @@ var cloneCmd = &cobra.Command{
}
}
configs.VerifyTokenSet()
configs.VerifyConfigsSetCorrectly()
err := configs.VerifyTokenSet()
if err != nil {
colorlog.PrintError(err)
os.Exit(1)
}
err = configs.VerifyConfigsSetCorrectly()
if err != nil {
colorlog.PrintError(err)
os.Exit(1)
}
args = argz

View File

@@ -15,6 +15,6 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of Ghorg",
Long: `All software has versions. This is Ghorg's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v1.1.4")
fmt.Println("v1.1.5")
},
}

View File

@@ -1,6 +1,7 @@
package configs
import (
"errors"
"fmt"
"log"
"os"
@@ -13,6 +14,29 @@ import (
"github.com/spf13/viper"
)
var (
// ErrNoGitHubToken error message when token is not found
ErrNoGitHubToken = errors.New("Could not find a valid github token. GHORG_GITHUB_TOKEN or (--token, -t) flag must be set. Create a personal access token, then set it in your $HOME/ghorg/conf.yaml or use the (--token, -t) flag...For best results read the troubleshooting section of README.md https://github.com/gabrie30/ghorg to properly store your token in the osx keychain")
// ErrNoGitLabToken error message when token is not found
ErrNoGitLabToken = errors.New("Could not find a valid gitlab token. GHORG_GITLAB_TOKEN or (--token, -t) flag must be set. Create a token from gitlab then set it in your $HOME/ghorg/conf.yaml or use the (--token, -t) flag...For best results read the troubleshooting section of README.md https://github.com/gabrie30/ghorg to properly store your token in the osx keychain")
// ErrNoBitbucketUsername error message when no username found
ErrNoBitbucketUsername = errors.New("Could not find bitbucket username. GHORG_BITBUCKET_USERNAME or (--bitbucket-username) must be set to clone repos from bitbucket, see 'BitBucket Setup' in README.md")
// ErrNoBitbucketAppPassword error message when no app password found
ErrNoBitbucketAppPassword = errors.New("Could not find a valid bitbucket app password. GHORG_BITBUCKET_APP_PASSWORD or (--token, -t) must be set to clone repos from bitbucket, see 'BitBucket Setup' in README.md")
// ErrIncorrectScmType indicates an unsupported scm type being used
ErrIncorrectScmType = errors.New("GHORG_SCM_TYPE or --scm must be one of github, gitlab, or bitbucket")
// ErrIncorrectCloneType indicates an unsupported clone type being used
ErrIncorrectCloneType = errors.New("GHORG_CLONE_TYPE or --clone-type must be one of org or user")
// ErrIncorrectProtocolType indicates an unsupported protocol type being used
ErrIncorrectProtocolType = errors.New("GHORG_CLONE_PROTOCOL or --protocol must be one of https or ssh")
)
func init() {
initConfig()
}
@@ -155,7 +179,7 @@ func getOrSetBitBucketToken() {
}
// VerifyTokenSet checks to make sure env is set for the correct scm provider
func VerifyTokenSet() {
func VerifyTokenSet() error {
var tokenLength int
var token string
scmProvider := os.Getenv("GHORG_SCM_TYPE")
@@ -174,42 +198,45 @@ func VerifyTokenSet() {
tokenLength = 20
token = os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")
if os.Getenv("GHORG_BITBUCKET_USERNAME") == "" {
colorlog.PrintError("GHORG_BITBUCKET_USERNAME or --bitbucket-username must be set to clone repos from bitbucket, see BitBucket Setup in Readme")
os.Exit(1)
return ErrNoBitbucketUsername
}
}
if len(token) != tokenLength {
if scmProvider == "github" || scmProvider == "gitlab" {
colorlog.PrintError("Could not find a set token for " + scmProvider + ". You should create a personal access token from " + scmProvider + " , then set the correct in your $HOME/ghorg/conf.yaml...or read the troubleshooting section of Readme.md https://github.com/gabrie30/ghorg to correctly store your token in your osx keychain. You can also manually set your token with (--token, -t) flag")
os.Exit(1)
if scmProvider == "github" {
return ErrNoGitHubToken
}
if scmProvider == "gitlab" {
return ErrNoGitLabToken
}
if scmProvider == "bitbucket" {
colorlog.PrintError("Could not find a set user and or app password for " + scmProvider + ", please update your $HOME/ghorg/conf.yaml. You can also read the Bitbucket Setup section of Readme.md https://github.com/gabrie30/ghorg to correctly create and store these values. Or you can also manually set your token with (--token, -t) flag and username with (--bitbucket-username")
return ErrNoBitbucketAppPassword
}
}
return nil
}
// VerifyConfigsSetCorrectly makes sure flags are set to appropriate values
func VerifyConfigsSetCorrectly() {
func VerifyConfigsSetCorrectly() error {
scmType := os.Getenv("GHORG_SCM_TYPE")
cloneType := os.Getenv("GHORG_CLONE_TYPE")
protocol := os.Getenv("GHORG_CLONE_PROTOCOL")
if scmType != "github" && scmType != "gitlab" && scmType != "bitbucket" {
colorlog.PrintError("GHORG_SCM_TYPE or --scm must be one of github, gitlab, or bitbucket")
os.Exit(1)
return ErrIncorrectScmType
}
if cloneType != "user" && cloneType != "org" {
colorlog.PrintError("GHORG_CLONE_TYPE or --clone-type must be one of org or user")
os.Exit(1)
return ErrIncorrectCloneType
}
if protocol != "ssh" && protocol != "https" {
colorlog.PrintError("GHORG_CLONE_PROTOCOL or --protocol must be one of https or ssh")
os.Exit(1)
return ErrIncorrectProtocolType
}
return nil
}

128
configs/configs_test.go Normal file
View File

@@ -0,0 +1,128 @@
package configs_test
import (
"os"
"testing"
"github.com/gabrie30/ghorg/configs"
)
func TestDefaultSettings(t *testing.T) {
configs.Load()
branch := os.Getenv("GHORG_BRANCH")
protocol := os.Getenv("GHORG_CLONE_PROTOCOL")
scm := os.Getenv("GHORG_SCM_TYPE")
cloneType := os.Getenv("GHORG_CLONE_TYPE")
namespace := os.Getenv("GHORG_GITLAB_DEFAULT_NAMESPACE")
path := os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO")
if branch != "master" {
t.Errorf("Default branch should be master, got: %v", branch)
}
if protocol != "https" {
t.Errorf("Default protocol should be https, got: %v", protocol)
}
if scm != "github" {
t.Errorf("Default scm should be github, got: %v", scm)
}
if cloneType != "org" {
t.Errorf("Default clone type should be org, got: %v", cloneType)
}
if namespace != "unset" {
t.Errorf("Default gitlab namespace type should be unset, got: %v", namespace)
}
if path != configs.HomeDir()+"/Desktop/" {
t.Errorf("Default ghorg path to clone to should be $HOME/Desktop/, got: %v", path)
}
}
func TestVerifyTokenSet(t *testing.T) {
t.Run("When cloning github", func(tt *testing.T) {
os.Setenv("GHORG_SCM_TYPE", "github")
err := configs.VerifyTokenSet()
if err != configs.ErrNoGitHubToken {
tt.Errorf("Expected ErrNoGitHubTokenError, got: %v", err)
}
})
t.Run("When cloning gitlab", func(tt *testing.T) {
os.Setenv("GHORG_SCM_TYPE", "gitlab")
err := configs.VerifyTokenSet()
if err != configs.ErrNoGitLabToken {
tt.Errorf("Expected ErrNoGitHubTokenError, got: %v", err)
}
})
t.Run("When cloning bitbucket with no username", func(tt *testing.T) {
os.Setenv("GHORG_SCM_TYPE", "bitbucket")
err := configs.VerifyTokenSet()
if err != configs.ErrNoBitbucketUsername {
tt.Errorf("Expected ErrNoBitbucketUsername, got: %v", err)
}
})
t.Run("When cloning bitbucket with username but no app password", func(tt *testing.T) {
os.Setenv("GHORG_SCM_TYPE", "bitbucket")
os.Setenv("GHORG_BITBUCKET_USERNAME", "bitbucket")
err := configs.VerifyTokenSet()
if err != configs.ErrNoBitbucketAppPassword {
tt.Errorf("Expected ErrNoBitbucketAppPassword, got: %v", err)
}
})
}
func TestVerifyConfigsSetCorrectly(t *testing.T) {
t.Run("When unsupported scm", func(tt *testing.T) {
os.Setenv("GHORG_CLONE_TYPE", "org")
os.Setenv("GHORG_CLONE_PROTOCOL", "ssh")
os.Setenv("GHORG_SCM_TYPE", "githubz")
err := configs.VerifyConfigsSetCorrectly()
if err != configs.ErrIncorrectScmType {
tt.Errorf("Expected ErrIncorrectScmType, got: %v", err)
}
})
t.Run("When unsupported clone type", func(tt *testing.T) {
os.Setenv("GHORG_SCM_TYPE", "github")
os.Setenv("GHORG_CLONE_PROTOCOL", "ssh")
os.Setenv("GHORG_CLONE_TYPE", "bot")
err := configs.VerifyConfigsSetCorrectly()
if err != configs.ErrIncorrectCloneType {
tt.Errorf("Expected ErrIncorrectCloneType, got: %v", err)
}
})
t.Run("When unsupported protocol", func(tt *testing.T) {
os.Setenv("GHORG_SCM_TYPE", "github")
os.Setenv("GHORG_CLONE_TYPE", "org")
os.Setenv("GHORG_CLONE_PROTOCOL", "ftp")
err := configs.VerifyConfigsSetCorrectly()
if err != configs.ErrIncorrectProtocolType {
tt.Errorf("Expected ErrIncorrectProtocolType, got: %v", err)
}
})
}

View File

@@ -1,15 +0,0 @@
package main
import (
"os"
"testing"
"github.com/gabrie30/ghorg/configs"
)
func TestDefaultBranch(t *testing.T) {
configs.Load()
if os.Getenv("GHORG_BRANCH") != "master" {
t.Errorf("Default branch should be master")
}
}