-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (97 loc) · 2.55 KB
/
main.go
File metadata and controls
112 lines (97 loc) · 2.55 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
v "github.com/deepsourcelabs/cli/buildinfo"
"github.com/deepsourcelabs/cli/command"
"github.com/deepsourcelabs/cli/internal/cli/style"
"github.com/deepsourcelabs/cli/internal/debug"
clierrors "github.com/deepsourcelabs/cli/internal/errors"
"github.com/deepsourcelabs/cli/internal/update"
"github.com/getsentry/sentry-go"
"github.com/pterm/pterm"
)
var (
version = "development"
Date = "YYYY-MM-DD"
SentryDSN string
buildMode string
)
func sentryEnvironment(ver string) string {
if strings.HasPrefix(ver, "v") || strings.Contains(ver, ".") {
return "production"
}
return "development"
}
func main() {
os.Exit(mainRun())
}
func mainRun() (exitCode int) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Override app identity for dev builds
if buildMode == "dev" {
v.AppName = "deepsource-dev"
v.ConfigDirName = ".deepsource-dev"
v.BaseURL = "https://cli.deepsource.one"
}
// Init sentry
err := sentry.Init(sentry.ClientOptions{
Dsn: SentryDSN,
Release: v.AppName + "-cli@" + version,
Environment: sentryEnvironment(version),
})
if err != nil {
log.Println("Could not load sentry.")
}
defer func() {
if r := recover(); r != nil {
sentry.CurrentHub().Recover(r)
sentry.Flush(2 * time.Second)
fmt.Fprintf(os.Stderr, "fatal: unexpected panic: %v\n", r)
exitCode = 2
}
}()
return run()
}
func run() int {
v.SetBuildInfo(version, Date, buildMode)
// Notify about available updates from a previous check (instant, disk-only read).
// Then kick off a background check for the next invocation.
isUpdateCmd := len(os.Args) >= 2 && os.Args[1] == "update"
if !isUpdateCmd && update.ShouldCheckForUpdate() {
state, err := update.ReadUpdateState()
if err != nil {
debug.Log("update: %v", err)
}
if state != nil {
fmt.Fprintln(os.Stderr, pterm.Yellow(fmt.Sprintf("Update available: v%s → v%s, run '%s update' to install.", version, state.Version, filepath.Base(os.Args[0]))))
}
go func() {
client := &http.Client{Timeout: 3 * time.Second}
if err := update.CheckForUpdate(client); err != nil {
debug.Log("update: %v", err)
}
}()
}
exitCode := 0
if err := command.Execute(); err != nil {
var cliErr *clierrors.CLIError
if errors.As(err, &cliErr) {
style.Errorf(os.Stderr, "%s", cliErr.Message)
} else {
style.Errorf(os.Stderr, "%v", err)
}
if !clierrors.IsUserError(err) {
sentry.CaptureException(err)
}
sentry.Flush(2 * time.Second)
exitCode = 1
}
return exitCode
}