package app import ( "bytes" "errors" "fmt" "io" "os" "path/filepath" "strings" configkit "gitea.maximumdirect.net/eric/ConfigKit" ) type secretsBootstrap struct { selection *string selectionSet bool called bool } func (b *secretsBootstrap) load(pipelinePath string, loader func(string) (*configkit.SecretsReport, error), diagnostics io.Writer) error { if b == nil || b.called { return nil } b.called = true directory, required, err := selectedSecretsDirectory(pipelinePath, b.selection) if err != nil { return err } if !required { if _, err := os.Stat(directory); errors.Is(err, os.ErrNotExist) { return nil } else if err != nil { return fmt.Errorf("inspect conventional secrets directory %q: %w", directory, err) } } report, loadErr := loader(directory) if loadErr == nil { if report != nil { if _, err := report.WriteTo(diagnostics); err != nil { return fmt.Errorf("write secrets bootstrap report: %w", err) } } return nil } var rendered bytes.Buffer if report != nil { _, _ = report.WriteTo(&rendered) } if rendered.Len() != 0 { return fmt.Errorf("bootstrap secrets directory: %w\n%s", loadErr, strings.TrimSpace(rendered.String())) } return fmt.Errorf("bootstrap secrets directory: %w", loadErr) } func selectedSecretsDirectory(pipelinePath string, selection *string) (string, bool, error) { if selection != nil { if strings.TrimSpace(*selection) == "" { return "", true, fmt.Errorf("--secrets-dir requires a non-empty path") } path, err := filepath.Abs(*selection) if err != nil { return "", true, fmt.Errorf("resolve --secrets-dir %q: %w", *selection, err) } return filepath.Clean(path), true, nil } root, err := filepath.Abs(pipelinePath) if err != nil { return "", false, fmt.Errorf("resolve pipeline path for secrets discovery: %w", err) } return filepath.Join(filepath.Dir(root), "secrets"), false, nil }