Skip to content

joshua-temple/goconf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub go.mod Go version GitHub Workflow Status GitHub issues GitHub stars

goconf

goconf is a Go tool that provides a CLI and library for generating configuration constants from YAML files and updating Go code with new constant values. Use it as a standalone CLI or import its functions directly into your tooling.

Features

  • Configuration Generation: Generate Go constants from YAML configuration files.
  • Constant Updates: Replace old constant usages with new ones by comparing constant values.
  • Backup & Dry-run: Optionally backup files before modifying and run in dry-run mode.
  • Concurrent File Processing: Speeds up updates on large codebases.
  • Cobra-based CLI: Easy-to-use command line interface.

Installation

  1. Clone the repository:
go install github.com/joshua-temple/goconf/cmd/goconf@latest
  1. Build the CLI:
go build -o goconf ./cmd/goconf

Usage

As a CLI

Generate Configuration Constants

Generate constants from YAML configuration files.
You can specify the target path for your YAML files, output file path, package name, and whether to create backups.

./goconf generate -t ./configs -o ./generated -p config

Update Constant Usages

Replace old constant names with new ones based on matching values from two Go files.
Options include dry-run and backup.
Provide the new and old constant files, and the directories containing files to update.

./goconf update -n new.go -o old.go --dirs ./pkg,./internal -d -b
  • -d/--dry-run: Display changes without writing.
  • -b/--backup: Create a backup file (with .bak extension) before modifying.

As a Library

Import the core functions into your project without using the CLI initialization:

package main

import "github.com/joshua-temple/goconf/pkg/goconf"

var (
    targetPath  = "./configs"
    outPath     = "./generated"
    packageName = "config"
    backup      = true

    oldFile     = "old.go"
    newFile     = "new.go"
    dryRun      = false
    directories = []string{"./pkg", "./internal"}
)

func main() {
	// ...
	
	// Generate configuration constants:
	err := goconf.GenerateConstants(targetPath, outPath, packageName, backup)
	if err != nil {
		// handle error
	}

	// Update constant usages:
	err = goconf.UpdateConstants(oldFile, newFile, dryRun, backup, directories)
	if err != nil {
		// handle error
	}

}

Testing

Run unit tests with:

go test ./...

Contributing

Contributions are welcome! Please submit issues and pull requests on GitHub. Make sure your changes are covered by tests and follow the existing style.

License

MIT License

Donations

Static Badge

Static Badge

Static Badge

Static Badge

Static Badge

Static Badge

Static Badge

Static Badge

About

Configuration utility for mapping yaml configs to go constants

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages