mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-03-02 02:56:00 +00:00
feat(tests): more tests and ci action (#14)
* feat(tests): more tests and ci action * fix(ci): coverage and pr-lint * fix(ci): renovate rules, permissions, linting, actions * fix(lint): editorconfig fixes * fix(lint): kics.config * fix(lint): formatting, permissions, pre-commit config * chore(ci): set workflow to use go 1.23, go mod tidy * chore(ci): fixes and stuff * chore(ci): disable GO_GOLANGCI_LINT * chore(ci): pinning, permissions
This commit is contained in:
117
main.go
117
main.go
@@ -30,45 +30,46 @@ func init() {
|
||||
flag.StringVar(&destination, "destination", "", "Output file to write aggregated code")
|
||||
flag.StringVar(&prefix, "prefix", "", "Text to add at the beginning of the output file")
|
||||
flag.StringVar(&suffix, "suffix", "", "Text to add at the end of the output file")
|
||||
flag.StringVar(&format, "format", "json", "Output format (json, markdown, yaml)")
|
||||
flag.StringVar(&format, "format", "markdown", "Output format (json, markdown, yaml)")
|
||||
flag.IntVar(&concurrency, "concurrency", runtime.NumCPU(), "Number of concurrent workers (default: number of CPU cores)")
|
||||
}
|
||||
|
||||
func main() {
|
||||
// In production, use a background context.
|
||||
if err := run(context.Background()); err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Run executes the main logic of the CLI application using the provided context.
|
||||
func Run(ctx context.Context) error {
|
||||
func run(ctx context.Context) error {
|
||||
flag.Parse()
|
||||
|
||||
// We need at least a source directory
|
||||
if sourceDir == "" {
|
||||
return fmt.Errorf("usage: gibidify -source <source_directory> [--destination <output_file>] [--format=json|yaml|markdown] ")
|
||||
if err := validateFlags(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If destination is not specified, auto-generate it using the base name of sourceDir + "." + format
|
||||
if destination == "" {
|
||||
absRoot, err := filepath.Abs(sourceDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get absolute path for %s: %w", sourceDir, err)
|
||||
}
|
||||
baseName := filepath.Base(absRoot)
|
||||
// If sourceDir ends with a slash, baseName might be "." so handle that case as needed
|
||||
if baseName == "." || baseName == "" {
|
||||
baseName = "output"
|
||||
}
|
||||
destination = baseName + "." + format
|
||||
if err := setDestination(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config.LoadConfig()
|
||||
|
||||
logrus.Infof("Starting gibidify. Format: %s, Source: %s, Destination: %s, Workers: %d", format, sourceDir, destination, concurrency)
|
||||
logrus.Infof(
|
||||
"Starting gibidify. Format: %s, Source: %s, Destination: %s, Workers: %d",
|
||||
format,
|
||||
sourceDir,
|
||||
destination,
|
||||
concurrency,
|
||||
)
|
||||
|
||||
// Collect files
|
||||
files, err := fileproc.CollectFiles(sourceDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error collecting files: %w", err)
|
||||
}
|
||||
logrus.Infof("Found %d files to process", len(files))
|
||||
|
||||
// Open output file
|
||||
outFile, err := os.Create(destination)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create output file %s: %w", destination, err)
|
||||
@@ -79,42 +80,16 @@ func Run(ctx context.Context) error {
|
||||
}
|
||||
}(outFile)
|
||||
|
||||
// Create channels
|
||||
fileCh := make(chan string)
|
||||
writeCh := make(chan fileproc.WriteRequest)
|
||||
writerDone := make(chan struct{})
|
||||
|
||||
// Start writer goroutine
|
||||
go fileproc.StartWriter(outFile, writeCh, writerDone, format, prefix, suffix)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Start worker goroutines with context cancellation
|
||||
for i := 0; i < concurrency; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case filePath, ok := <-fileCh:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Pass sourceDir to ProcessFile so it knows the 'root'
|
||||
absRoot, err := filepath.Abs(sourceDir)
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to get absolute path for %s: %v", sourceDir, err)
|
||||
return
|
||||
}
|
||||
fileproc.ProcessFile(filePath, writeCh, absRoot)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
startWorkers(ctx, &wg, fileCh, writeCh)
|
||||
|
||||
// Feed files to worker pool while checking for cancellation
|
||||
for _, fp := range files {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -132,11 +107,49 @@ func Run(ctx context.Context) error {
|
||||
logrus.Infof("Processing completed. Output saved to %s", destination)
|
||||
return nil
|
||||
}
|
||||
func validateFlags() error {
|
||||
if sourceDir == "" {
|
||||
return fmt.Errorf("usage: gibidify -source <source_directory> [--destination <output_file>] [--format=json|yaml|markdown] ")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// In production, use a background context.
|
||||
if err := Run(context.Background()); err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
os.Exit(1)
|
||||
func setDestination() error {
|
||||
if destination == "" {
|
||||
absRoot, err := filepath.Abs(sourceDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get absolute path for %s: %w", sourceDir, err)
|
||||
}
|
||||
baseName := filepath.Base(absRoot)
|
||||
if baseName == "." || baseName == "" {
|
||||
baseName = "output"
|
||||
}
|
||||
destination = baseName + "." + format
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func startWorkers(ctx context.Context, wg *sync.WaitGroup, fileCh chan string, writeCh chan fileproc.WriteRequest) {
|
||||
for range concurrency {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case filePath, ok := <-fileCh:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
absRoot, err := filepath.Abs(sourceDir)
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to get absolute path for %s: %v", sourceDir, err)
|
||||
return
|
||||
}
|
||||
fileproc.ProcessFile(filePath, writeCh, absRoot)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user