This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
525 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"glab/commands" | ||
"glab/internal/utils" | ||
"io" | ||
"net" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"glab/commands" | ||
"glab/internal/config" | ||
) | ||
|
||
// Version is set at build | ||
var version string | ||
|
||
// build is set at build | ||
var build string | ||
// usage mode is set at build to either "dev" or "prod" depending how binary is created | ||
var usageMode string | ||
var debug bool | ||
|
||
func main() { | ||
commands.Version = version | ||
commands.Build = build | ||
if err := commands.Execute(); err != nil { | ||
fmt.Println(err) | ||
|
||
initConfig() | ||
if usageMode == "dev" { | ||
debug = true | ||
} | ||
if cmd, err := commands.Execute(); err != nil { | ||
printError(os.Stderr, err, cmd, debug) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func initConfig() { | ||
config.SetGlobalPathDir() | ||
config.UseGlobalConfig = true | ||
|
||
if config.GetEnv("GITLAB_URI") == "NOTFOUND" || config.GetEnv("GITLAB_URI") == "OK" { | ||
config.SetEnv("GITLAB_URI", "https://gitlab.com") | ||
} | ||
if config.GetEnv("GIT_REMOTE_URL_VAR") == "NOTFOUND" || config.GetEnv("GIT_REMOTE_URL_VAR") == "OK" { | ||
config.SetEnv("GIT_REMOTE_URL_VAR", "origin") | ||
} | ||
|
||
config.UseGlobalConfig = false | ||
} | ||
|
||
func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) { | ||
if err == utils.SilentError { | ||
return | ||
} | ||
|
||
var dnsError *net.DNSError | ||
if errors.As(err, &dnsError) { | ||
_, _ = fmt.Fprintf(out, "error connecting to %s\n", dnsError.Name) | ||
if debug { | ||
_, _ = fmt.Fprintln(out, dnsError) | ||
} | ||
_, _ = fmt.Fprintln(out, "check your internet connection or status.gitlab.com or 'Run sudo gitlab-ctl status' on your server if self-hosted") | ||
return | ||
} | ||
|
||
re := regexp.MustCompile(`(?s){(.*)}`) | ||
m := re.FindAllStringSubmatch(err.Error(), -1) | ||
if len(m) != 0 { | ||
if len(m[0]) >= 1 { | ||
_, _ = fmt.Fprintln(out, m[0][1]) | ||
} | ||
} else { | ||
_, _ = fmt.Fprintln(out, err) | ||
} | ||
|
||
var flagError *utils.FlagError | ||
if errors.As(err, &flagError) || strings.HasPrefix(err.Error(), "unknown command ") { | ||
if !strings.HasSuffix(err.Error(), "\n") { | ||
_, _ = fmt.Fprintln(out) | ||
} | ||
_, _ = fmt.Fprintln(out, cmd.UsageString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"glab/internal/config" | ||
"glab/internal/utils" | ||
"math/rand" | ||
"net" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// Test started when the test binary is started | ||
// and calls the main function | ||
func TestGlab(t *testing.T) { | ||
rand.Seed(time.Now().UnixNano()) | ||
flag.Set("test", "../coverage-" + strconv.Itoa(int(rand.Uint64())) + ".out") | ||
main() | ||
} | ||
|
||
func Test_printError(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
|
||
type args struct { | ||
err error | ||
cmd *cobra.Command | ||
debug bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantOut string | ||
}{ | ||
{ | ||
name: "generic error", | ||
args: args{ | ||
err: errors.New("the app exploded"), | ||
cmd: nil, | ||
debug: false, | ||
}, | ||
wantOut: "the app exploded\n", | ||
}, | ||
{ | ||
name: "DNS error", | ||
args: args{ | ||
err: fmt.Errorf("DNS oopsie: %w", &net.DNSError{ | ||
Name: config.GetEnv("GITLAB_URI")+"/api/v4", | ||
}), | ||
cmd: nil, | ||
debug: false, | ||
}, | ||
wantOut: `error connecting to `+config.GetEnv("GITLAB_URI")+`/api/v4 | ||
check your internet connection or status.gitlab.com or 'Run sudo gitlab-ctl status' on your server if self-hosted | ||
`, | ||
}, | ||
{ | ||
name: "Cobra flag error", | ||
args: args{ | ||
err: &utils.FlagError{Err: errors.New("unknown flag --foo")}, | ||
cmd: cmd, | ||
debug: false, | ||
}, | ||
wantOut: "unknown flag --foo\n\nUsage:\n\n", | ||
}, | ||
{ | ||
name: "unknown Cobra command error", | ||
args: args{ | ||
err: errors.New("unknown command foo"), | ||
cmd: cmd, | ||
debug: false, | ||
}, | ||
wantOut: "unknown command foo\n\nUsage:\n\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out := &bytes.Buffer{} | ||
printError(out, tt.args.err, tt.args.cmd, tt.args.debug) | ||
if gotOut := out.String(); gotOut != tt.wantOut { | ||
t.Errorf("printError() = %q, want %q", gotOut, tt.wantOut) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.