-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
command.go
86 lines (72 loc) · 2.63 KB
/
command.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package configcheck implements 'agent configcheck'.
package configcheck
import (
"bytes"
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/spf13/cobra"
"go.uber.org/fx"
"github.com/DataDog/datadog-agent/cmd/agent/command"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
"github.com/DataDog/datadog-agent/comp/core/config"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
"github.com/DataDog/datadog-agent/comp/core/secrets"
apiutil "github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/flare"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
// cliParams are the command-line arguments for this subcommand
type cliParams struct {
*command.GlobalParams
verbose bool
}
// Commands returns a slice of subcommands for the 'agent' command.
func Commands(globalParams *command.GlobalParams) []*cobra.Command {
cliParams := &cliParams{
GlobalParams: globalParams,
}
configCheckCommand := &cobra.Command{
Use: "configcheck",
Aliases: []string{"checkconfig"},
Short: "Print all configurations loaded & resolved of a running agent",
Long: ``,
RunE: func(_ *cobra.Command, _ []string) error {
return fxutil.OneShot(run,
fx.Supply(cliParams),
fx.Supply(core.BundleParams{
ConfigParams: config.NewAgentParams(globalParams.ConfFilePath, config.WithExtraConfFiles(cliParams.ExtraConfFilePath), config.WithFleetPoliciesDirPath(cliParams.FleetPoliciesDirPath)),
SecretParams: secrets.NewEnabledParams(),
LogParams: log.ForOneShot("CORE", "off", true)}),
core.Bundle(),
)
},
}
configCheckCommand.Flags().BoolVarP(&cliParams.verbose, "verbose", "v", false, "print additional debug info")
return []*cobra.Command{configCheckCommand}
}
func run(config config.Component, cliParams *cliParams, _ log.Component) error {
endpoint, err := apiutil.NewIPCEndpoint(config, "/agent/config-check")
if err != nil {
return err
}
res, err := endpoint.DoGet()
if err != nil {
return fmt.Errorf("the agent ran into an error while checking config: %v", err)
}
cr := integration.ConfigCheckResponse{}
err = json.Unmarshal(res, &cr)
if err != nil {
return fmt.Errorf("unable to parse configcheck: %v", err)
}
var b bytes.Buffer
color.Output = &b
flare.PrintConfigCheck(color.Output, cr, cliParams.verbose)
fmt.Println(b.String())
return nil
}