Documentation
¶
Overview ¶
Package config handles configuration of the app. The configuration can simply be a default, or a config file can be provided. The config data is used for low-level settings of the app and can be used elsewhere in this app.
The config file is in YAML format for easy readability. However, the file does not need to end in the yaml extension. YAML is used since it allows for adding comments to the config file (JSON does not). Having comments is really nice for providing some additional info in the config file.
This package must not import any other packages from within this app to prevent import loops (besides minor utility packages).
---
Create a default config file in the current working directory using the -init flag.
A config is handled one of three ways:
- If a file exists at the path, it is attempted to be parsed as a valid config file.
- If a file does not exists at the path, the built-in defaults are used after a warning is shown about the missing file.
- If the path is blank, the default config is used.
---
When adding a new field to the config file:
- Add the field to the File type below.
- Determine any default value(s) for the field and set it in newDefaultConfig().
- Set validation in validate().
- Document the field as needed (README, other documentation).
Try to keep the organization/order of the config fields the same between the config file templates, the type defined below, validation, and diagnostics.
Index ¶
- Constants
- func CreateDefaultConfig() (err error)
- func Read(path string, print bool) (err error)
- type File
- func (conf *File) IsDirectoryToIgnore(path string) bool
- func (conf *File) IsExtensionToWatch(extension string) bool
- func (conf *File) IsRebuildExtension(extension string) bool
- func (conf *File) IsTempDir(path string) (yes bool, err error)
- func (conf *File) OverrideTags(t string)
- func (conf *File) OverrideVerbose(v bool)
- func (conf *File) UsingDefaults() bool
Constants ¶
const DefaultConfigFileName = "fresher.conf"
DefaultConfigFileName is the typical name of the config file.
Variables ¶
This section is empty.
Functions ¶
func CreateDefaultConfig ¶
func CreateDefaultConfig() (err error)
CreateDefaultConfig creates a config file in the current directory with the default configuration. This is mostly used to create a template config file to modify. Used/called by the -init flag.
func Read ¶
Read handles reading and parsing the config file at the provided path and saving it to the package's parsedConfig variable for future use. The parsed data is sanitized and validated. The print argument is used to print the config as it was read/parsed and as it was understood after sanitizing, validating, and handling default values.
If a config file is not found at the given path, a warning is shown and the built-in default config is used instead. Use -init to create a default config file.
Types ¶
type File ¶
type File struct {
//WorkingDir is the path to the root of your repository. All files in or within
//subdirectories of this directory, recursively, will be watched for changes.
//
//Typically this is set to "." since your terminal, and thus where fresher is
//being run from, is already set to the root directory of your repo.
WorkingDir string `yaml:"WorkingDir"`
//EntryPoint is the path to your "package main" file, the root file of your app
//that you want to build.
//
//If you have a "main.go" file in your local directory from which you are running
//fresher, you can leave this as ".". However, if your "package main" is in any
//other path, specifically a subdirectory of your repo, you must provide the
//relative path to it here (i.e: "cmd/app/app.go").
EntryPoint string `yaml:"EntryPoint"`
//TempDir is the directory off of WorkingDir where fresher will store the built
//binary, that will be run, and error logs.
TempDir string `yaml:"TempDir"`
//ExtensionsToWatch is the list of file extensions to watch for changes, typically
//.go and .html (if building a web app).
ExtensionsToWatch []string `yaml:"ExtensionsToWatch"`
//NoRebuildExtensions is the list of extensions that the binary will be restarted
//on when file changes occur, but the binary won't be rebuilt. Any extension
//listed here should, obviously, be listed in ExtensionsToWatch as well.
//
//For example, if an .html file is changed, the binary would need to be restarted
//since HTML files are typically stored in memory (using html/templates) when the
//binary is first started.
NoRebuildExtensions []string `yaml:"NoRebuildExtensions"`
//DirectoriesToIgnore is the list of directories that won't be watched for file
//change events. Typically directories such as .git, node_modules, etc.
DirectoriesToIgnore []string `yaml:"DirectoriesToIgnore"`
//BuildDelayMilliseconds is the delay between a file change event occuring and
//`go build` being run. This delay is helpful to prevent unnecessary buildng when
//multiple file change events occur in quick succession.
//
//This was inherited from the "github.com/gravityblast/fresh" and may not be
//needed any longer since running `go build`s will be cancelled if a new file
//change event occurs.
BuildDelayMilliseconds int64 `yaml:"BuildDelayMilliseconds"`
//BuildName is the name of the binary output by `go build` and saved to TempDir.
BuildName string `yaml:"BuildName"`
//BuildLogFilename is the name of file saved in TempDir where build errors will
//be logged to. This file will contain output from `go build` and is useful for
//analyzing errors rather then looking at output in terminal.
BuildLogFilename string `yaml:"BuildLogFilename"`
//GoTags is anything provided to `go run` or `go build` -tags flag.
//
//Any tags provided in the config, from file or defaults, are overridden by
//anything provided to the -tags flag provided to fresher. This was done to
//alleviate the need to always edit a config file for handling -tags changes.
GoTags string `yaml:"GoTags"`
//GoLdflags is anything provided to `go build` -ldflags flag.
//See https://pkg.go.dev/cmd/link for possible options.
GoLdflags string `yaml:"GoLdflags"`
//GoTrimpath determines if the -trimpath flag should be passed to `go build`.
//Typically this isn't needed since the built binary won't be distributed since
//fresher is designed for development use only.
//See https://pkg.go.dev/cmd/go#:~:text=but%20still%20recognized.)%0A%2D-,trimpath,-remove%20all%20file.
GoTrimpath bool `yaml:"GoTrimpath"`
//Flags is the list of flags you provide to your binary or `go run`, such as
//`go run main.go -flag1="test"`.
Flags string `yaml:"Flags"`
//Verbose causes fresher to output more logging. Use for diagnostics when
//determining which files/directories/extensions are being watched and when file
//change events are occuring.
Verbose bool `yaml:"Verbose"`
// contains filtered or unexported fields
}
File defines the list of configuration fields. The value for each field will be set by a default or read from a config file. The config file is typically stored in the same directory as the executable.
Don't use uints! If user provided a negative number an ugly error message is kicked out. We would rather check for the negative number here and provide a nicer error message.
Struct tags are needed for working with yaml.v3 package, otherwise the package expects fields to start with lower case characters. However, if we lower cased all the struct field names, then we wouldn't be able to access those fields in other packages.
If adding or updating a field here, make sure to document it in README.md!
func Data ¶
func Data() *File
Data returns the package level saved config. This is used in other packages to access the parsed config file.
func (*File) IsDirectoryToIgnore ¶
IsDirectoryToIgnore returns true if the given path is in the DirectoriesToIgnore.
func (*File) IsExtensionToWatch ¶
IsExtensionToWatch returns true if the given path contains an extension we should watch for changes.
func (*File) IsRebuildExtension ¶
IsRebuildExtension returns true if the given extension is not in the NoRebuildExtensions list.
func (*File) IsTempDir ¶
IsTempDir returns true if the given path represents the same directory as TempDir. We use absolute paths here since we want to be certain if the path given matches the same underlying directory as given in TempDir.
func (*File) OverrideTags ¶
OverrideTags sets the Tags field to t. This is used when the -tags flag was provided and overrides the value stored in parsedConfig's Tags field. This is useful for changing tags without having to edit the config file (if it exists) each time.
func (*File) OverrideVerbose ¶
OverrideVerbose sets the Verbose field to v. This is used when the -verbose flag was provided and overrides the value stored in teh parsedConfig's Verbose field. This is useful for when (1) you aren't using a config file (i.e.: the default running method of fresher), or (2) you have a config file and just want some extra logging on a case-by-case basis.
func (*File) UsingDefaults ¶
UsingDefaults returns true is usingbuildInDefaults is set to true.