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
/
main.go
86 lines (74 loc) · 1.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"errors"
"fmt"
"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
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())
}
}