Skip to content

Commit 138e47d

Browse files
use flags instead of new subcommand, rename flags to --render-otel and --render-otel-details
1 parent c71b0b2 commit 138e47d

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

internal/commands/config/config.go

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"os"
10+
"strings"
1011

1112
"github.com/go-viper/mapstructure/v2"
1213
"github.com/observeinc/observe-agent/internal/commands/start"
@@ -25,67 +26,68 @@ var configCmd = &cobra.Command{
2526
Use: "config",
2627
Short: "Prints the full configuration for this agent.",
2728
Long: `This command prints all configuration for this agent including any additional
28-
OTEL configuration.`,
29+
bundled OTel configuration.`,
2930
RunE: func(cmd *cobra.Command, args []string) error {
30-
ctx := logger.WithCtx(context.Background(), logger.GetNop())
31-
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(ctx)
32-
if err != nil {
33-
return err
34-
}
35-
if cleanup != nil {
36-
defer cleanup()
37-
}
38-
agentConfig, err := config.AgentConfigFromViper(viper.GetViper())
31+
detailedOtel, err := cmd.Flags().GetBool("render-otel-details")
3932
if err != nil {
4033
return err
4134
}
42-
agentConfigYaml, err := yaml.Marshal(agentConfig)
35+
singleOtel, err := cmd.Flags().GetBool("render-otel")
4336
if err != nil {
4437
return err
4538
}
46-
fmt.Printf("# ======== computed agent config\n")
47-
fmt.Println(string(agentConfigYaml) + "\n")
48-
agentConfigFile := viper.ConfigFileUsed()
49-
if agentConfigFile != "" {
50-
configFilePaths = append([]string{agentConfigFile}, configFilePaths...)
39+
if singleOtel && detailedOtel {
40+
return fmt.Errorf("cannot specify both --render-otel and --render-otel-details")
5141
}
52-
for _, filePath := range configFilePaths {
53-
file, err := os.ReadFile(filePath)
54-
if err != nil {
55-
fmt.Fprintf(os.Stderr, "error reading config file %s: %s", filePath, err.Error())
56-
} else {
57-
fmt.Printf("# ======== config file %s\n", filePath)
58-
fmt.Println(string(file))
59-
}
60-
}
61-
return nil
62-
},
63-
}
6442

65-
var otelConfigSubCmd = &cobra.Command{
66-
Use: "export-otel",
67-
Short: "Prints a single otel config file containing the full configuration that would run.",
68-
Long: `This command prints a single otel config file containing all bundled configuration.
69-
Features that are enabled or disabled in the observe-agent config will be reflected in the output accordingly.`,
70-
RunE: func(cmd *cobra.Command, args []string) error {
71-
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(logger.WithCtx(context.Background(), logger.GetNop()))
43+
ctx := logger.WithCtx(context.Background(), logger.GetNop())
44+
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(ctx)
7245
if cleanup != nil {
7346
defer cleanup()
7447
}
7548
if err != nil {
7649
return err
7750
}
78-
fullVersion, err := cmd.Flags().GetBool("full")
79-
if err != nil {
80-
return err
81-
}
82-
if fullVersion {
51+
if singleOtel {
52+
return printShortOtelConfig(ctx, configFilePaths)
53+
} else if detailedOtel {
8354
return printFullOtelConfig(configFilePaths)
8455
}
85-
return printShortOtelConfig(cmd.Context(), configFilePaths)
56+
return printAllConfigsIndividually(configFilePaths)
8657
},
8758
}
8859

60+
func printAllConfigsIndividually(configFilePaths []string) error {
61+
printConfig := func(comment string, data []byte) {
62+
fmt.Printf("# ======== %s\n", comment)
63+
fmt.Println(strings.Trim(string(data), "\n\t "))
64+
fmt.Println("---")
65+
}
66+
67+
agentConfig, err := config.AgentConfigFromViper(viper.GetViper())
68+
if err != nil {
69+
return err
70+
}
71+
agentConfigYaml, err := yaml.Marshal(agentConfig)
72+
if err != nil {
73+
return err
74+
}
75+
printConfig("computed agent config", agentConfigYaml)
76+
agentConfigFile := viper.ConfigFileUsed()
77+
if agentConfigFile != "" {
78+
configFilePaths = append([]string{agentConfigFile}, configFilePaths...)
79+
}
80+
for _, filePath := range configFilePaths {
81+
file, err := os.ReadFile(filePath)
82+
if err != nil {
83+
fmt.Fprintf(os.Stderr, "error reading config file %s: %s", filePath, err.Error())
84+
} else {
85+
printConfig("config file "+filePath, file)
86+
}
87+
}
88+
return nil
89+
}
90+
8991
func printShortOtelConfig(ctx context.Context, configFilePaths []string) error {
9092
settings := observecol.ConfigProviderSettings(configFilePaths)
9193
resolver, err := confmap.NewResolver(settings.ResolverSettings)
@@ -132,7 +134,7 @@ func printFullOtelConfig(configFilePaths []string) error {
132134
}
133135

134136
func init() {
135-
otelConfigSubCmd.Flags().Bool("full", false, "Print the full resolved configuration including default values instead of the pre-processed configuration.")
136-
configCmd.AddCommand(otelConfigSubCmd)
137+
configCmd.Flags().Bool("render-otel-details", false, "Print the full resolved otel configuration including default values after the otel components perform their semantic processing.")
138+
configCmd.Flags().Bool("render-otel", false, "Print a single rendered otel configuration file. This file is equivalent to the bundled configuration enabled in the observe-agent config.")
137139
root.RootCmd.AddCommand(configCmd)
138140
}

0 commit comments

Comments
 (0)