Skip to content

Commit

Permalink
Merge pull request #30 from jonasbn/issue28
Browse files Browse the repository at this point in the history
Added support for global configuration file
  • Loading branch information
jonasbn committed Mar 12, 2023
2 parents ef2c51f + 649e27f commit c5df987
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
.git
main.go
_site
assets
cover.out
dictionary.dic
examples
staticcheck.conf
stevedore-config.schema.json
stevedore.code-workspace
.github
Gemfile
Gemfile.lock
.in
README.md
1 change: 1 addition & 0 deletions .github/spellchecker-wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ nofullpath
JSON
jonasbn
Unsplash
XDG
3 changes: 2 additions & 1 deletion .stevedore.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "stevedore-config.schema.json",
"debug": false
"debug": false,
"invertcolors": true
}
57 changes: 54 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ Since this is just an analysis/reporting tool it can be fed with parameters to d
- `--stdin` / `-s` reads ignore file from STDIN
- `--fullpath` / `-f` emits full path of encountered files and directories

Precedence for configuration of parameters are:

- Global configuration file
- Local configuration file
- Command line parameters

Use the global configuration file for the configuration you prefer for all you projects and invocations.

Add a local configuration file, where you want to continuously override the global configuration for that particular directory and for all your invocations.

See Configuration section for details on configuration.

### Verbosity

If the verbose flag is set the output is altered and is more explanatory:
Expand Down Expand Up @@ -83,9 +95,12 @@ These will render the same result.

## Configuration

If you find yourself constantly writing out the same command line parameters, you can create a local configuration file: `.stevedore.json`
If you find yourself constantly writing out the same command line parameters, you have several options for for using a configuration file:

1. You can configure per project/repository, by having a file named `.stevedore.json`
2. You can specify a file in `$HOME/.config/stevedore/config.json`

You can specify the setting for all command line arguments, but with a JSON key/value structure:
You can in either file specify the setting for all command line arguments, with a JSON key/value structure:

```json
{
Expand All @@ -106,6 +121,20 @@ Parameters not available for configuration:
- `--help`
- `--stdin`

Precedence for the configuration files are:

- Global configuration file
- Local configuration file
- Command line parameters

Use the global configuration file for the configuration you prefer for all you projects and invocations.

Add a local configuration file, where you want to continuously override the global configuration for that particular directory and for all your invocations.

See also Configuration for more details.

See Parameters section for details on parameters.

## Return Values

- `0` indicates a successful run
Expand All @@ -124,6 +153,21 @@ The `.stevedoreignore` file follows the general implementation pattern. and exam
.git
```

## Environment

`stevedore` support locating a configuration file in:

- `$HOME/.config/stevedore`
- Named: `config.json`

The directory can be specified using the environment variable:

`$XDG_CONFIG_HOME`, the default is: `$HOME/.config`. If the environment variable is not set, the default is evaluated.

Do note `stevedore` does not support: `$XDG_CONFIG_DIRS`.

See Configuration section for details on configuration.

## Compatibility

- [Docker ignore][DOCKERIGNORE]: `.dockerignore` (main purpose)
Expand All @@ -132,7 +176,12 @@ The `.stevedoreignore` file follows the general implementation pattern. and exam

## Incompatibility

- `stevedore` does not support following symbolic links in the traversal of directories
`stevedore` does not support:

- following symbolic links in the traversal of directories, this limitation is imposed by the limitation from the library used for the implementation: [`path/filepath` documentation for `WalkDir` function](https://pkg.go.dev/path/filepath#WalkDir)
- `$XDG_CONFIG_DIRS` which are part of the "XDG Base Directory Specification" are not supported at this time

See Configuration section for details on configuration.

## Resources and References

Expand All @@ -141,6 +190,8 @@ The `.stevedoreignore` file follows the general implementation pattern. and exam
- [Git ignore][GITIGNORE]
- [Go gitignore][GO-GITIGNORE]
- [Yak ignore][YAKIGNORE]
- [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
- [`path/filepath` documentation for `WalkDir` function](https://pkg.go.dev/path/filepath#WalkDir)
- [Background image by photographer Josh Young](https://unsplash.com/photos/Huv8EWe2Vo8)

## License and Copyright
Expand Down
54 changes: 52 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,24 @@ func realMain() int {

flag.Parse()

globalConfigDir := os.Getenv("HOME") + "/.config"
XDGConfigHome := os.Getenv("XDG_CONFIG_HOME")

if XDGConfigHome != "" {
globalConfigDir = XDGConfigHome
}

stevedoreGlobalConfigDir := globalConfigDir + "/stevedore/"

globalConfigFile := stevedoreGlobalConfigDir + "config.json"
_, err := loadGlobalConfigFile(globalConfigFile, &config)

if err != nil {
fmt.Printf("Error attempting to read global configuration file: %s, continuing...\n", err)
}

configFile := ".stevedore.json"
_, err := loadConfigFile(configFile, &config)
_, err = loadLocalConfigFile(configFile, &config)

if err != nil {
fmt.Printf("Error attempting to read configuration file: %s, continuing...\n", err)
Expand Down Expand Up @@ -318,7 +334,41 @@ func realMain() int {
return 0
}

func loadConfigFile(configFile string, config *Config) (rv bool, err error) {
func loadGlobalConfigFile(configFile string, config *Config) (rv bool, err error) {

if _, err := os.Stat(configFile); !errors.Is(err, fs.ErrNotExist) {

jsonData, err := os.ReadFile(configFile)

if err != nil {
return false, err
}

err = json.Unmarshal(jsonData, &config)
if err != nil {
return false, err
}

if config.Debug {
fmt.Println("Config file:")
fmt.Println("\tcolor: ", config.Color)
fmt.Println("\tnocolor: ", config.Nocolor)
fmt.Println("\tignorefile: ", config.Ignorefile)
fmt.Println("\tdebug: ", config.Debug)
fmt.Println("\tverbose: ", config.Verbose)
fmt.Println("\texcluded: ", config.Excluded)
fmt.Println("\tincluded: ", config.Included)
fmt.Println("\tfullpath: ", config.Fullpath)
fmt.Println("\tnofullpath: ", config.Nofullpath)
}
} else {
return false, fmt.Errorf("Config file %s not found", configFile)
}

return true, nil
}

func loadLocalConfigFile(configFile string, config *Config) (rv bool, err error) {

if _, err := os.Stat(configFile); !errors.Is(err, fs.ErrNotExist) {

Expand Down

0 comments on commit c5df987

Please sign in to comment.