Skip to content

Commit

Permalink
feat: add .github/pinact.yaml as a default config path (#394)
Browse files Browse the repository at this point in the history
* feat: add .github/pinact.yaml as a default config path

* chore: add missing err check in test fixture
  • Loading branch information
kachick authored May 1, 2024
1 parent 2d5bf26 commit 7500059
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,18 @@ $ pinact run action.yaml
```

A configuration file is optional.
You can create a configuration file by `pinact init`.
You can create a configuration file `.pinact.yaml` by `pinact init`.

```console
$ pinact init
```

You can change the output path.

```console
$ pinact init '.github/pinact.yaml'
```

About the configuration, please see [Configuration](#Configuration).

## GitHub Actions
Expand All @@ -137,7 +143,7 @@ We develop GitHub Actions to pin GitHub Actions and reusable workflows by pinact

## Configuration

pinact supports a configuration file `.pinact.yaml`.
pinact supports a configuration file `.pinact.yaml` or `.github/pinact.yaml`.
You can also specify the configuration file path by the environment variable `PINACT_CONFIG` or command line option `-c`.

.pinact.yaml
Expand Down
21 changes: 17 additions & 4 deletions pkg/controller/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,29 @@ func (ctrl *Controller) searchFilesByConfig(logE *logrus.Entry, cfg *Config, pwd
return files, nil
}

func getConfigPath(fs afero.Fs) (string, error) {
for _, path := range []string{".pinact.yaml", ".github/pinact.yaml"} {
f, err := afero.Exists(fs, path)
if err != nil {
return "", fmt.Errorf("check if %s exists: %w", path, err)
}
if f {
return path, nil
}
}
return "", nil
}

func (ctrl *Controller) readConfig(configFilePath string, cfg *Config) error {
var err error
if configFilePath == "" {
f, err := afero.Exists(ctrl.fs, ".pinact.yaml")
configFilePath, err = getConfigPath(ctrl.fs)
if err != nil {
return fmt.Errorf("check if .pinact.yaml exists: %w", err)
return err
}
if !f {
if configFilePath == "" {
return nil
}
configFilePath = ".pinact.yaml"
}
f, err := ctrl.fs.Open(configFilePath)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions pkg/controller/run/main_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,51 @@ func TestController_patchLine(t *testing.T) {
})
}
}

func TestController_getConfigPath(t *testing.T) {
t.Parallel()
data := []struct {
name string
paths []string
exp string
}{
{
name: "no config",
paths: []string{},
exp: "",
},
{
name: "primary",
paths: []string{".pinact.yaml"},
exp: ".pinact.yaml",
},
{
name: "another",
paths: []string{".github/pinact.yaml"},
exp: ".github/pinact.yaml",
},
{
name: "both primary and others",
paths: []string{".pinact.yaml", ".github/pinact.yaml"},
exp: ".pinact.yaml",
},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
for _, path := range d.paths {
if err := afero.WriteFile(fs, path, []byte(""), 0o644); err != nil {
t.Fatal(err)
}
}
got, err := getConfigPath(fs)
if err != nil {
t.Fatal(err)
}
if got != d.exp {
t.Fatalf(`wanted %s, got %s`, d.exp, got)
}
})
}
}

0 comments on commit 7500059

Please sign in to comment.