Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default config file if exists #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ with the filename of the changed file. (The symbol may be changed with the
OPTIONS are given below:
--all=false:
Include normally ignored files (VCS and editor special files).
-c, --config="":
-c, --config="reflex.conf":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the output of running reflex -h, is it?

You should update the -c documentation string to mention reflex.conf, particularly because it's not a normal flag default value.

Copy link
Author

@zrhmn zrhmn Dec 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would much appreciate some advice on how best to do that.

We can edit the help string to append the phrase Default: reflex.conf at the end, so it appears like so:

  -c, --config="":
            A configuration file that describes how to run reflex
            (or '-' to read the configuration from stdin). Default: reflex.conf

But that's not how pflag prefers to indicate default arg values, instead this feels better:

  -c, --config="reflex.conf":
            A configuration file that describes how to run reflex
            (or '-' to read the configuration from stdin).

However, unless I'm missing something, the 2nd method can only be done through the value arg of pflag.StringVarP call, which I don't want to do because there's a special case, where -c is not passed, but reflex.conf doesn't exist either. It's not simply that "default value for -c is reflex.conf" but rather "default value for -c is reflex.conf if the file exists in current working dir", and I'm at loss as to how best to handle that third case.

I'm leaning towards a spin on the first method above mentioned, like so:

 -c, --config="":
            A configuration file that describes how to run reflex
            (or '-' to read the configuration from stdin).
            Default: reflex.conf – if it exists in current working directory.

A configuration file that describes how to run reflex
(or '-' to read the configuration from stdin).
-d, --decoration="plain":
Expand Down Expand Up @@ -136,6 +136,10 @@ also build SCSS and Coffeescript when those change as well. Instead of running
multiple reflex instances, which is cumbersome (and inefficient), you can give
reflex a configuration file.

If a configuration file is not specified, and a file named `reflex.conf` exists
in the current directory, reflex automatically picks that file as the default
configuration file.

The configuration file syntax is simple: each line is a command, and each
command is composed of flags and arguments -- just like calling reflex but
without the initial `reflex`. Lines that start with `#` are ignored. Commands
Expand Down
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
flag "github.com/ogier/pflag"
)

const defaultSubSymbol = "{}"
const (
defaultSubSymbol = "{}"
defaultConfigFile = "reflex.conf"
)

var (
reflexes []*Reflex
Expand Down Expand Up @@ -138,14 +141,27 @@ func main() {
}

var configs []*Config
if flagConf == "" {
if _, err := os.Stat(defaultConfigFile); err != nil {
if !os.IsNotExist(err) {
log.Fatalf("Could not read %s: %v.", defaultConfigFile, err)
}
} else {
flagConf = defaultConfigFile
}
}

if flagConf == "" {
if flagSequential {
log.Fatal("Cannot set --sequential without --config (because you cannot specify multiple commands).")
}
configs = []*Config{globalConfig}
} else {
if anyNonGlobalsRegistered() {
log.Fatal("Cannot set other flags along with --config other than --sequential, --verbose, and --decoration.")
log.Fatalf(
"Cannot set other flags other than --sequential, --verbose, and --decoration, when --config is specified or %s exists.",
defaultConfigFile,
)
}
var err error
configs, err = ReadConfigs(flagConf)
Expand Down