func inspectConfig()

in internal/pkg/agent/cmd/inspect.go [133:232]


func inspectConfig(ctx context.Context, cfgPath string, opts inspectConfigOpts, streams *cli.IOStreams) error {
	l, err := newErrorLogger()
	if err != nil {
		return fmt.Errorf("error creating logger: %w", err)
	}

	isAdmin, err := utils.HasRoot()
	if err != nil {
		return fmt.Errorf("error checking for root/Administrator privileges: %w", err)
	}
	if !opts.variables && !opts.includeMonitoring {
		fullCfg, err := operations.LoadFullAgentConfig(ctx, l, cfgPath, true, !isAdmin)
		if err != nil {
			return fmt.Errorf("error loading agent config: %w", err)
		}
		err = printConfig(fullCfg, streams)
		if err != nil {
			return fmt.Errorf("error printing config: %w", err)
		}
		return nil
	}

	cfg, lvl, err := getConfigWithVariables(ctx, l, cfgPath, opts.variablesWait, !isAdmin)
	if err != nil {
		return fmt.Errorf("error fetching config with variables: %w", err)
	}

	agentInfo, err := info.NewAgentInfoWithLog(ctx, "error", false)
	if err != nil {
		return fmt.Errorf("could not load agent info: %w", err)
	}

	if opts.includeMonitoring {
		// Load the requirements before trying to load the configuration. These should always load
		// even if the configuration is wrong.
		platform, err := component.LoadPlatformDetail()
		if err != nil {
			return fmt.Errorf("failed to gather system information: %w", err)
		}
		specs, err := component.LoadRuntimeSpecs(paths.Components(), platform)
		if err != nil {
			return fmt.Errorf("failed to detect inputs and outputs: %w", err)
		}

		monitorFn, err := getMonitoringFn(ctx, cfg)
		if err != nil {
			return fmt.Errorf("failed to get monitoring: %w", err)
		}
		components, err := specs.PolicyToComponents(cfg, lvl, agentInfo)
		if err != nil {
			return fmt.Errorf("failed to get binary mappings: %w", err)
		}

		// service units like endpoint are special; they require a PID to monitor.
		// however, `inspect` doesn't talk to the coordinator backend, which means it can't know their actual PID from this point in the code
		// instead, we look for service units and create a fake PID, so we print the monitoring config anyway.
		serviceUnitExists := false
		fakeServicePids := map[string]uint64{}

		for _, component := range components {
			if spec := component.InputSpec; spec != nil {
				if spec.Spec.Service != nil {
					serviceUnitExists = true
					fakeServicePids[component.ID] = 1234
				}
			}
		}
		monitorCfg, err := monitorFn(cfg, components, fakeServicePids)
		if err != nil {
			return fmt.Errorf("failed to get monitoring config: %w", err)
		}

		if monitorCfg != nil {

			// see above comment; because we don't know endpoint's actual PID, we need to make a fake one. Warn the user.
			if serviceUnitExists {
				keys := make([]string, 0, len(fakeServicePids))
				for k := range fakeServicePids {
					keys = append(keys, k)
				}
				fmt.Fprintf(streams.Err, "WARNING: the inspect command can't accurately produce monitoring configs for service units: %v. Use the diagnostics command to get the real config used for monitoring these components\n", keys)
			}

			rawCfg := config.MustNewConfigFrom(cfg)

			if err := rawCfg.Merge(monitorCfg); err != nil {
				return fmt.Errorf("failed to merge monitoring config: %w", err)
			}

			cfg, err = rawCfg.ToMapStr()
			if err != nil {
				return fmt.Errorf("failed to convert monitoring config: %w", err)
			}

		}

	}

	return printMapStringConfig(cfg, streams)
}