-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
main.go
106 lines (99 loc) · 3.65 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cli
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/telepresenceio/telepresence/v2/pkg/client"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cmd"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/connect"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/output"
"github.com/telepresenceio/telepresence/v2/pkg/client/logging"
"github.com/telepresenceio/telepresence/v2/pkg/client/rootd"
"github.com/telepresenceio/telepresence/v2/pkg/client/userd"
userDaemon "github.com/telepresenceio/telepresence/v2/pkg/client/userd/daemon"
"github.com/telepresenceio/telepresence/v2/pkg/client/userd/trafficmgr"
"github.com/telepresenceio/telepresence/v2/pkg/errcat"
"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
"github.com/telepresenceio/telepresence/v2/pkg/proc"
)
func InitContext(ctx context.Context) context.Context {
env, err := client.LoadEnv()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load environment: %v", err)
os.Exit(1)
}
ctx = client.WithEnv(ctx, env)
switch client.ProcessName() {
case userd.ProcessName:
if proc.RunningInContainer() {
client.DisplayName = "OSS Daemon in container"
} else {
client.DisplayName = "OSS User Daemon"
}
ctx = userd.WithNewServiceFunc(ctx, userDaemon.NewService)
ctx = userd.WithNewSessionFunc(ctx, trafficmgr.NewSession)
case rootd.ProcessName:
client.DisplayName = "OSS Root Daemon"
ctx = rootd.WithNewServiceFunc(ctx, rootd.NewService)
ctx = rootd.WithNewSessionFunc(ctx, rootd.NewSession)
default:
client.DisplayName = "OSS Client"
ctx = connect.WithCommandInitializer(ctx, connect.CommandInitializer)
ctx = cmd.WithSubCommands(ctx)
}
return ctx
}
func Main(ctx context.Context) {
if dir := os.Getenv("DEV_TELEPRESENCE_CONFIG_DIR"); dir != "" {
ctx = filelocation.WithAppUserConfigDir(ctx, dir)
}
if dir := os.Getenv("DEV_TELEPRESENCE_LOG_DIR"); dir != "" {
ctx = filelocation.WithAppUserLogDir(ctx, dir)
}
if client.IsDaemon() {
// Avoid the initialization of all subcommands except for [connector|daemon]-foreground and
// avoids checks for legacy commands.
if cmd, _, err := output.Execute(cmd.TelepresenceDaemon(ctx)); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "%s: error: %v\n", cmd.CommandPath(), err)
os.Exit(1)
}
} else {
if cmd, fmtOutput, err := output.Execute(cmd.Telepresence(ctx)); err != nil {
if fmtOutput {
os.Exit(1)
}
fmt.Fprintf(cmd.ErrOrStderr(), "%s: error: %v\n", cmd.CommandPath(), err)
if errcat.GetCategory(err) > errcat.NoDaemonLogs {
if summarizeLogs(ctx, cmd) {
// If the user gets here, it might be an actual bug that they found, so
// point them to the `gather-logs` command in case they want to open an
// issue.
fmt.Fprintln(cmd.ErrOrStderr(), "If you think you have encountered a bug"+
", please run `telepresence gather-logs` and attach the "+
"telepresence_logs.zip to your github issue or create a new one: "+
"https://github.com/telepresenceio/telepresence/issues/new?template=Bug_report.md .")
}
}
os.Exit(1)
}
}
}
// summarizeLogs outputs the logs from the root and user daemons. It returns true
// if output were produced, false otherwise (might happen if no logs exist yet).
func summarizeLogs(ctx context.Context, cmd *cobra.Command) bool {
w := cmd.ErrOrStderr()
first := true
for _, proc := range []string{rootd.ProcessName, userd.ProcessName} {
if summary, err := logging.SummarizeLog(ctx, proc); err != nil {
fmt.Fprintf(w, "failed to scan %s logs: %v\n", proc, err)
} else if summary != "" {
if first {
fmt.Fprintln(w)
first = false
}
fmt.Fprintln(w, summary)
}
}
return !first
}