Skip to content
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ go install github.com/release-tools/since
- [version](#project-version)
- [release](#project-release)

**Config** - Scaffold a config file for the tool.
- [init](#init)

---

### `changelog generate`
Expand Down Expand Up @@ -202,10 +205,35 @@ Global Flags:
-t, --tag string Include commits after this tag
```

---

### `init`

Creates a new `since.yaml` config file, pre-populated with commented
examples for branch requirements, pre/post hook scripts, and commit
exclusions. If the file already exists, it is overwritten.

```
Usage:
since init [flags]

Flags:
-h, --help help for init
-o, --output string Directory to write the config file to (default: current directory)

Global Flags:
-l, --log-level string Log level (debug, info, warn, error, fatal, panic) (default "debug")
-q, --quiet Disable logging (useful for scripting)
```

---

#### The `since.yaml` file

You can also use a `since.yaml` file to configure the tool. This file should be placed in the root of your project repository.

The quickest way to start is `since init`, which drops a fully commented example config into your project. Uncomment the bits you need.

```yaml
# require us to be on the main branch
requireBranch: main
Expand Down
4 changes: 2 additions & 2 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ type SinceConfig struct {
Ignore []string `yaml:"ignore"`
}

const defaultConfigFile = "since.yaml"
const DefaultConfigFile = "since.yaml"

// LoadConfig loads the YAML config file from the given directory.
func LoadConfig(dir string) (SinceConfig, error) {
return loadConfig(path.Join(dir, defaultConfigFile))
return loadConfig(path.Join(dir, DefaultConfigFile))
}

// loadConfig loads the YAML config file from the given path.
Expand Down
2 changes: 1 addition & 1 deletion cfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_loadConfig(t *testing.T) {
func TestLoadConfig(t *testing.T) {
dir := t.TempDir()
content := "requireBranch: main\nbefore:\n - command: echo\n args:\n - hello world\n"
if err := os.WriteFile(path.Join(dir, defaultConfigFile), []byte(content), 0644); err != nil {
if err := os.WriteFile(path.Join(dir, DefaultConfigFile), []byte(content), 0644); err != nil {
t.Fatal(err)
}

Expand Down
81 changes: 81 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright © 2023 Pete Cornish <outofcoffee@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
_ "embed"
"fmt"
"os"
"path/filepath"

"github.com/release-tools/since/cfg"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

//go:embed templates/since.yaml
var defaultConfig string

var initSubCmd struct {
outputFile string
}

// initSubCmd represents the init subcommand
var initSubCmdCmd = &cobra.Command{
Use: "init",
Short: "Create a new since.yaml config file",
Long: `Creates a new since.yaml config file with example configuration,
including branch requirements, pre/post hook scripts, and commit exclusions.
If the config file already exists, it will be overwritten.`,
Args: cobra.NoArgs,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runInit()
},
}

func runInit() error {
configDir := initSubCmd.outputFile
if configDir == "" {
var err error
configDir, err = os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
}
configPath := filepath.Join(configDir, cfg.DefaultConfigFile)

if _, err := os.Stat(configPath); err == nil {
logrus.Warnf("config file '%s' already exists, overwriting", configPath)
} else if !os.IsNotExist(err) {
return fmt.Errorf("failed to check config file: %w", err)
}

if err := os.WriteFile(configPath, []byte(defaultConfig), 0644); err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}

logrus.Infof("created config file '%s'", configPath)
return nil
}

func init() {
rootCmd.AddCommand(initSubCmdCmd)

initSubCmdCmd.Flags().StringVarP(&initSubCmd.outputFile, "output", "o", "", "Directory to write the config file to (default: current directory)")
}
Loading
Loading