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

Fix configuration file extension from conf to toml #90

Merged
merged 5 commits into from
Aug 2, 2020
Merged
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@ cd /path/to/your_project
The simplest usage is run

```bash
# firstly find `.air.conf` in current directory, if not found, use defaults
air -c .air.conf
# firstly find `.air.toml` in current directory, if not found, use defaults
air -c .air.toml
```

While I prefer the second way

```bash
# 1. create a new file
touch .air.conf
touch .air.toml

# 2. paste `air.conf.example` into this file, and **modify it** to satisfy your needs.
# 2. paste `air_example.toml` into this file, and **modify it** to satisfy your needs.

# 3. run air with your config. If file name is `.air.conf`, just run `air`.
# 3. run air with your config. If file name is `.air.toml`, just run `air`.
air
```

See the complete [air_example.conf](air_example.conf)
See the complete [air_example.toml](air_example.toml)

### Debug

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions runner/_testdata/both/.air.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = "invalid"
1 change: 1 addition & 0 deletions runner/_testdata/both/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = "both_root"
1 change: 1 addition & 0 deletions runner/_testdata/conf/.air.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = "conf_root"
1 change: 1 addition & 0 deletions runner/_testdata/toml/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = "toml_root"
67 changes: 47 additions & 20 deletions runner/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -13,6 +14,7 @@ import (
)

const (
dftTOML = ".air.toml"
dftConf = ".air.conf"
airWd = "air_wd"
)
Expand Down Expand Up @@ -59,16 +61,10 @@ type cfgMisc struct {

func initConfig(path string) (cfg *config, err error) {
if path == "" {
// when path is blank, first find `.air.conf` in `air_wd` and current working directory, if not found, use defaults
if wd := os.Getenv(airWd); wd != "" {
path = filepath.Join(wd, dftConf)
} else {
path, err = dftConfPath()
if err != nil {
return nil, err
}
cfg, err = defaultPathConfig()
if err != nil {
return nil, err
}
cfg, _ = readConfigOrDefault(path)
} else {
cfg, err = readConfigOrDefault(path)
if err != nil {
Expand All @@ -83,6 +79,34 @@ func initConfig(path string) (cfg *config, err error) {
return cfg, err
}

func defaultPathConfig() (*config, error) {
// when path is blank, first find `.air.toml`, `.air.conf` in `air_wd` and current working directory, if not found, use defaults
for _, name := range []string{dftTOML, dftConf} {
var path string
if wd := os.Getenv(airWd); wd != "" {
xiantang marked this conversation as resolved.
Show resolved Hide resolved
path = filepath.Join(wd, name)
} else {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
path = filepath.Join(wd, name)
}

cfg, err := readConfig(path)
if err == nil {
if name == dftConf {
fmt.Println("`.air.conf` will be deprecated soon, recommend using `.air.toml`.")
}

return cfg, nil
}
}

dftCfg := defaultConfig()
return &dftCfg, nil
}

func defaultConfig() config {
build := cfgBuild{
Cmd: "go build -o ./tmp/main .",
Expand Down Expand Up @@ -119,16 +143,27 @@ func defaultConfig() config {
}
}

func readConfigOrDefault(path string) (*config, error) {
dftCfg := defaultConfig()
func readConfig(path string) (*config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return &dftCfg, err
return nil, err
}

cfg := new(config)
if err = toml.Unmarshal(data, cfg); err != nil {
return nil, err
}

return cfg, nil
}

func readConfigOrDefault(path string) (*config, error) {
dftCfg := defaultConfig()
cfg, err := readConfig(path)
if err != nil {
return &dftCfg, err
}

return cfg, nil
}

Expand Down Expand Up @@ -202,14 +237,6 @@ func (c *config) colorInfo() map[string]string {
}
}

func dftConfPath() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Join(wd, dftConf), nil
}

func (c *config) buildLogPath() string {
return filepath.Join(c.tmpPath(), c.Build.Log)
}
Expand Down
40 changes: 40 additions & 0 deletions runner/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -63,3 +64,42 @@ func TestBinCmdPath(t *testing.T) {
}
}
}

func TestDefaultPathConfig(t *testing.T) {
tests := []struct {
name string
path string
root string
}{{
name: "Invalid Path",
path: "invalid/path",
root: ".",
}, {
name: "TOML",
path: "_testdata/toml",
root: "toml_root",
}, {
name: "Conf",
path: "_testdata/conf",
root: "conf_root",
}, {
name: "Both",
path: "_testdata/both",
root: "both_root",
}}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
os.Setenv(airWd, tt.path)
c, err := defaultPathConfig()
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}

if got, want := c.Root, tt.root; got != want {
t.Fatalf("Root is %s, but want %s.", got, want)
}
})
}
}