forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
108 lines (92 loc) · 4.09 KB
/
common.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
107
108
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
package cmd
import (
"flag"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
// import logp flags
_ "github.com/elastic/elastic-agent-libs/logp/configure"
"github.com/elastic/elastic-agent/internal/pkg/basecmd"
"github.com/elastic/elastic-agent/internal/pkg/cli"
"github.com/elastic/elastic-agent/internal/pkg/release"
"github.com/elastic/elastic-agent/version"
)
func troubleshootMessage() string {
v := strings.Split(release.Version(), ".")
version := strings.Join(v[:2], ".")
return fmt.Sprintf("For help, please see our troubleshooting guide at https://www.elastic.co/guide/en/fleet/%s/fleet-troubleshooting.html", version)
}
// NewCommand returns the default command for the agent.
func NewCommand() *cobra.Command {
return NewCommandWithArgs(os.Args, cli.NewIOStreams())
}
// NewCommandWithArgs returns a new agent with the flags and the subcommand.
func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "elastic-agent [subcommand]",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Name() == "container" {
// need to initialize container and try to chown agent-related paths
// before tryContainerLoadPaths as this will try to read/write from
// the agent state dir which might not have proper permissions when
// running inside a container
initContainer(streams)
}
return tryContainerLoadPaths()
},
}
// Init version information contained in package version file
if isOtel := len(args) > 1 && args[1] == "otel"; !isOtel {
err := version.InitVersionError()
if err != nil {
cmd.PrintErrf("Error initializing version information: %v\n", err)
}
}
// path flags
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home.unversioned"))
// hidden used internally by container subcommand
cmd.PersistentFlags().MarkHidden("path.home.unversioned") //nolint:errcheck // it's hidden
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.config"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("c"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("config"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.logs"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.downloads"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.socket"))
// logging flags
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("v"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("e"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("d"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("environment"))
// sub-commands
run := newRunCommandWithArgs(args, streams)
cmd.AddCommand(basecmd.NewDefaultCommandsWithArgs(args, streams)...)
cmd.AddCommand(run)
cmd.AddCommand(newInstallCommandWithArgs(args, streams))
cmd.AddCommand(newUninstallCommandWithArgs(args, streams))
cmd.AddCommand(newUpgradeCommandWithArgs(args, streams))
cmd.AddCommand(newEnrollCommandWithArgs(args, streams))
cmd.AddCommand(newInspectCommandWithArgs(args, streams))
cmd.AddCommand(newPrivilegedCommandWithArgs(args, streams))
cmd.AddCommand(newUnprivilegedCommandWithArgs(args, streams))
cmd.AddCommand(newWatchCommandWithArgs(args, streams))
cmd.AddCommand(newContainerCommand(args, streams))
cmd.AddCommand(newStatusCommand(args, streams))
cmd.AddCommand(newDiagnosticsCommand(args, streams))
cmd.AddCommand(newComponentCommandWithArgs(args, streams))
cmd.AddCommand(newLogsCommandWithArgs(args, streams))
cmd.AddCommand(newOtelCommandWithArgs(args, streams))
cmd.AddCommand(newApplyFlavorCommandWithArgs(args, streams))
// windows special hidden sub-command (only added on Windows)
reexec := newReExecWindowsCommand(args, streams)
if reexec != nil {
cmd.AddCommand(reexec)
}
cmd.Run = run.Run
cmd.RunE = run.RunE
return cmd
}