@@ -4,11 +4,10 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
4
4
package initconfig
5
5
6
6
import (
7
- "embed"
8
7
"fmt"
9
- "html/template"
10
8
"os"
11
9
10
+ "github.com/observeinc/observe-agent/internal/config"
12
11
"github.com/observeinc/observe-agent/internal/root"
13
12
"github.com/spf13/cobra"
14
13
"github.com/spf13/viper"
@@ -25,43 +24,14 @@ var (
25
24
host_monitoring_logs_include []string
26
25
host_monitoring_metrics_host_enabled bool
27
26
host_monitoring_metrics_process_enabled bool
28
- //go:embed observe-agent.tmpl
29
- configTemplateFS embed.FS
30
27
)
31
28
32
- const configTemplate = "observe-agent.tmpl"
33
-
34
- type FlatAgentConfig struct {
35
- Token string
36
- ObserveURL string
37
- CloudResourceDetectors []string
38
- SelfMonitoring_Enabled bool
39
- HostMonitoring_Enabled bool
40
- HostMonitoring_LogsEnabled bool
41
- HostMonitoring_LogsInclude []string
42
- HostMonitoring_Metrics_HostEnabled bool
43
- HostMonitoring_Metrics_ProcessEnabled bool
44
- }
45
-
46
29
func NewConfigureCmd (v * viper.Viper ) * cobra.Command {
47
30
return & cobra.Command {
48
31
Use : "init-config" ,
49
32
Short : "Initialize agent configuration" ,
50
33
Long : `This command takes in parameters and creates an initialized observe agent configuration file. Will overwrite existing config file and should only be used to initialize.` ,
51
34
RunE : func (cmd * cobra.Command , args []string ) error {
52
- configValues := FlatAgentConfig {
53
- Token : v .GetString ("token" ),
54
- ObserveURL : v .GetString ("observe_url" ),
55
- CloudResourceDetectors : v .GetStringSlice ("cloud_resource_detectors" ),
56
- SelfMonitoring_Enabled : v .GetBool ("self_monitoring::enabled" ),
57
- HostMonitoring_Enabled : v .GetBool ("host_monitoring::enabled" ),
58
- HostMonitoring_LogsEnabled : v .GetBool ("host_monitoring::logs::enabled" ),
59
- HostMonitoring_Metrics_HostEnabled : v .GetBool ("host_monitoring::metrics::host::enabled" ),
60
- HostMonitoring_Metrics_ProcessEnabled : v .GetBool ("host_monitoring::metrics::process::enabled" ),
61
- }
62
- if configValues .HostMonitoring_LogsEnabled {
63
- configValues .HostMonitoring_LogsInclude = v .GetStringSlice ("host_monitoring::logs::include" )
64
- }
65
35
var f * os.File
66
36
if v .GetBool ("print" ) {
67
37
f = os .Stdout
@@ -80,10 +50,11 @@ func NewConfigureCmd(v *viper.Viper) *cobra.Command {
80
50
defer f .Close ()
81
51
fmt .Printf ("Writing configuration values to %s...\n \n " , outputPath )
82
52
}
83
- t := template . Must ( template . New ( configTemplate ). ParseFS ( configTemplateFS , configTemplate ) )
84
- if err := t . ExecuteTemplate ( f , configTemplate , configValues ); err != nil {
53
+ agentConfig , err := config . AgentConfigFromViper ( v )
54
+ if err != nil {
85
55
return err
86
56
}
57
+ writeConfigFile (f , agentConfig , v .GetBool ("include-defaults" ))
87
58
return nil
88
59
},
89
60
}
@@ -98,24 +69,40 @@ func init() {
98
69
99
70
func RegisterConfigFlags (cmd * cobra.Command , v * viper.Viper ) {
100
71
cmd .Flags ().StringVarP (& config_path , "config_path" , "" , "" , "Path to write config output file to" )
101
- cmd .PersistentFlags ().Bool ("print" , false , "Print the configuration to stdout instead of writing to a file" )
72
+ cmd .Flags ().Bool ("print" , false , "Print the configuration to stdout instead of writing to a file" )
73
+ v .BindPFlag ("print" , cmd .Flags ().Lookup ("print" ))
74
+ cmd .Flags ().Bool ("include-defaults" , false , "Include the names and default values for unset config options." )
75
+ v .BindPFlag ("include-defaults" , cmd .Flags ().Lookup ("include-defaults" ))
76
+
102
77
cmd .PersistentFlags ().StringVar (& token , "token" , "" , "Observe token" )
103
- cmd .PersistentFlags ().StringVar (& observe_url , "observe_url" , "" , "Observe data collection url" )
104
- cmd .PersistentFlags ().StringSliceVar (& cloud_resource_detectors , "cloud_resource_detectors" , []string {}, "The cloud environments from which to detect resources" )
105
- cmd .PersistentFlags ().BoolVar (& self_monitoring_enabled , "self_monitoring::enabled" , true , "Enable self monitoring" )
106
- cmd .PersistentFlags ().BoolVar (& host_monitoring_enabled , "host_monitoring::enabled" , true , "Enable host monitoring" )
107
- cmd .PersistentFlags ().BoolVar (& host_monitoring_logs_enabled , "host_monitoring::logs::enabled" , true , "Enable host monitoring logs" )
108
- cmd .PersistentFlags ().StringSliceVar (& host_monitoring_logs_include , "host_monitoring::logs::include" , nil , "Set host monitoring log include paths" )
109
- cmd .PersistentFlags ().BoolVar (& host_monitoring_metrics_host_enabled , "host_monitoring::metrics::host::enabled" , true , "Enable host monitoring host metrics" )
110
- cmd .PersistentFlags ().BoolVar (& host_monitoring_metrics_process_enabled , "host_monitoring::metrics::process::enabled" , false , "Enable host monitoring process metrics" )
111
- v .BindPFlag ("print" , cmd .PersistentFlags ().Lookup ("print" ))
112
78
v .BindPFlag ("token" , cmd .PersistentFlags ().Lookup ("token" ))
79
+
80
+ cmd .PersistentFlags ().StringVar (& observe_url , "observe_url" , "" , "Observe data collection url" )
113
81
v .BindPFlag ("observe_url" , cmd .PersistentFlags ().Lookup ("observe_url" ))
82
+
83
+ cmd .PersistentFlags ().StringSliceVar (& cloud_resource_detectors , "cloud_resource_detectors" , []string {}, "The cloud environments from which to detect resources" )
114
84
v .BindPFlag ("cloud_resource_detectors" , cmd .PersistentFlags ().Lookup ("cloud_resource_detectors" ))
85
+
86
+ cmd .PersistentFlags ().BoolVar (& self_monitoring_enabled , "self_monitoring::enabled" , true , "Enable self monitoring" )
115
87
v .BindPFlag ("self_monitoring::enabled" , cmd .PersistentFlags ().Lookup ("self_monitoring::enabled" ))
88
+ v .SetDefault ("self_monitoring::enabled" , true )
89
+
90
+ cmd .PersistentFlags ().BoolVar (& host_monitoring_enabled , "host_monitoring::enabled" , true , "Enable host monitoring" )
116
91
v .BindPFlag ("host_monitoring::enabled" , cmd .PersistentFlags ().Lookup ("host_monitoring::enabled" ))
92
+ v .SetDefault ("host_monitoring::enabled" , true )
93
+
94
+ cmd .PersistentFlags ().BoolVar (& host_monitoring_logs_enabled , "host_monitoring::logs::enabled" , true , "Enable host monitoring logs" )
117
95
v .BindPFlag ("host_monitoring::logs::enabled" , cmd .PersistentFlags ().Lookup ("host_monitoring::logs::enabled" ))
96
+ v .SetDefault ("host_monitoring::logs::enabled" , true )
97
+
98
+ cmd .PersistentFlags ().StringSliceVar (& host_monitoring_logs_include , "host_monitoring::logs::include" , nil , "Set host monitoring log include paths" )
118
99
v .BindPFlag ("host_monitoring::logs::include" , cmd .PersistentFlags ().Lookup ("host_monitoring::logs::include" ))
100
+
101
+ cmd .PersistentFlags ().BoolVar (& host_monitoring_metrics_host_enabled , "host_monitoring::metrics::host::enabled" , true , "Enable host monitoring host metrics" )
119
102
v .BindPFlag ("host_monitoring::metrics::host::enabled" , cmd .PersistentFlags ().Lookup ("host_monitoring::metrics::host::enabled" ))
103
+ v .SetDefault ("host_monitoring::metrics::host::enabled" , true )
104
+
105
+ cmd .PersistentFlags ().BoolVar (& host_monitoring_metrics_process_enabled , "host_monitoring::metrics::process::enabled" , false , "Enable host monitoring process metrics" )
120
106
v .BindPFlag ("host_monitoring::metrics::process::enabled" , cmd .PersistentFlags ().Lookup ("host_monitoring::metrics::process::enabled" ))
107
+ v .SetDefault ("host_monitoring::metrics::process::enabled" , false )
121
108
}
0 commit comments