mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-03-10 00:59:16 +00:00
feat: update go to 1.25, add permissions and envs (#49)
* chore(ci): update go to 1.25, add permissions and envs * fix(ci): update pr-lint.yml * chore: update go, fix linting * fix: tests and linting * fix(lint): lint fixes, renovate should now pass * fix: updates, security upgrades * chore: workflow updates, lint * fix: more lint, checkmake, and other fixes * fix: more lint, convert scripts to POSIX compliant * fix: simplify codeql workflow * tests: increase test coverage, fix found issues * fix(lint): editorconfig checking, add to linters * fix(lint): shellcheck, add to linters * fix(lint): apply cr comment suggestions * fix(ci): remove step-security/harden-runner * fix(lint): remove duplication, apply cr fixes * fix(ci): tests in CI/CD pipeline * chore(lint): deduplication of strings * fix(lint): apply cr comment suggestions * fix(ci): actionlint * fix(lint): apply cr comment suggestions * chore: lint, add deps management
This commit is contained in:
@@ -5,30 +5,100 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ivuorinen/gibidify/utils"
|
||||
"github.com/ivuorinen/gibidify/gibidiutils"
|
||||
)
|
||||
|
||||
// StartWriter writes the output in the specified format with memory optimization.
|
||||
func StartWriter(outFile *os.File, writeCh <-chan WriteRequest, done chan<- struct{}, format, prefix, suffix string) {
|
||||
switch format {
|
||||
case "markdown":
|
||||
startMarkdownWriter(outFile, writeCh, done, prefix, suffix)
|
||||
case "json":
|
||||
startJSONWriter(outFile, writeCh, done, prefix, suffix)
|
||||
case "yaml":
|
||||
startYAMLWriter(outFile, writeCh, done, prefix, suffix)
|
||||
// WriterConfig holds configuration for the writer.
|
||||
type WriterConfig struct {
|
||||
Format string
|
||||
Prefix string
|
||||
Suffix string
|
||||
}
|
||||
|
||||
// Validate checks if the WriterConfig is valid.
|
||||
func (c WriterConfig) Validate() error {
|
||||
if c.Format == "" {
|
||||
return gibidiutils.NewStructuredError(
|
||||
gibidiutils.ErrorTypeValidation,
|
||||
gibidiutils.CodeValidationFormat,
|
||||
"format cannot be empty",
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
switch c.Format {
|
||||
case "markdown", "json", "yaml":
|
||||
return nil
|
||||
default:
|
||||
context := map[string]interface{}{
|
||||
"format": format,
|
||||
context := map[string]any{
|
||||
"format": c.Format,
|
||||
}
|
||||
err := utils.NewStructuredError(
|
||||
utils.ErrorTypeValidation,
|
||||
utils.CodeValidationFormat,
|
||||
fmt.Sprintf("unsupported format: %s", format),
|
||||
return gibidiutils.NewStructuredError(
|
||||
gibidiutils.ErrorTypeValidation,
|
||||
gibidiutils.CodeValidationFormat,
|
||||
fmt.Sprintf("unsupported format: %s", c.Format),
|
||||
"",
|
||||
context,
|
||||
)
|
||||
utils.LogError("Failed to encode output", err)
|
||||
}
|
||||
}
|
||||
|
||||
// StartWriter writes the output in the specified format with memory optimization.
|
||||
func StartWriter(outFile *os.File, writeCh <-chan WriteRequest, done chan<- struct{}, config WriterConfig) {
|
||||
// Validate config
|
||||
if err := config.Validate(); err != nil {
|
||||
gibidiutils.LogError("Invalid writer configuration", err)
|
||||
close(done)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate outFile is not nil
|
||||
if outFile == nil {
|
||||
err := gibidiutils.NewStructuredError(
|
||||
gibidiutils.ErrorTypeIO,
|
||||
gibidiutils.CodeIOFileWrite,
|
||||
"output file is nil",
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
gibidiutils.LogError("Failed to write output", err)
|
||||
close(done)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate outFile is accessible
|
||||
if _, err := outFile.Stat(); err != nil {
|
||||
structErr := gibidiutils.WrapError(
|
||||
err,
|
||||
gibidiutils.ErrorTypeIO,
|
||||
gibidiutils.CodeIOFileWrite,
|
||||
"failed to stat output file",
|
||||
)
|
||||
gibidiutils.LogError("Failed to validate output file", structErr)
|
||||
close(done)
|
||||
return
|
||||
}
|
||||
|
||||
switch config.Format {
|
||||
case "markdown":
|
||||
startMarkdownWriter(outFile, writeCh, done, config.Prefix, config.Suffix)
|
||||
case "json":
|
||||
startJSONWriter(outFile, writeCh, done, config.Prefix, config.Suffix)
|
||||
case "yaml":
|
||||
startYAMLWriter(outFile, writeCh, done, config.Prefix, config.Suffix)
|
||||
default:
|
||||
context := map[string]interface{}{
|
||||
"format": config.Format,
|
||||
}
|
||||
err := gibidiutils.NewStructuredError(
|
||||
gibidiutils.ErrorTypeValidation,
|
||||
gibidiutils.CodeValidationFormat,
|
||||
fmt.Sprintf("unsupported format: %s", config.Format),
|
||||
"",
|
||||
context,
|
||||
)
|
||||
gibidiutils.LogError("Failed to encode output", err)
|
||||
close(done)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user