Skip to content

Commit

Permalink
Hlubek bugfix config init (markbates#48)
Browse files Browse the repository at this point in the history
* Fix init without explicit config flag (markbates#36)

- Config file defaults to refresh.yml
- Check if file already exists before overwriting it
- Refactor error checkin in Cobra commands

* 1.17 update

* Update tests.yml

Co-authored-by: Christopher Hlubek <[email protected]>
  • Loading branch information
markbates and hlubek authored Sep 22, 2021
1 parent 764db58 commit fa07c64
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 130 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ jobs:
name: Release
runs-on: ubuntu-latest
steps:
-
name: Set up Go 1.13
- name: Set up Go 1.17
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.17
id: go
-
name: Checkout Code
- name: Checkout Code
uses: actions/checkout@master
-
name: Run GoReleaser
- name: Run GoReleaser
env:
GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
uses: goreleaser/goreleaser-action@v1
Expand Down
25 changes: 13 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
name: Tests
on: [push]
jobs:

tests-on:
name: ${{matrix.go-version}} ${{matrix.os}}
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.12.x, 1.13.x]
go-version: [1.17.x]
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Checkout Code
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: Test
run: |
go mod tidy -v
go test -race ./...
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17.x
- name: Checkout Code
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: Test
run: |
go mod tidy -v
go test -race ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ generated/
bin/*
gin-bin
.idea/
.vscode
18 changes: 16 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"os"

"github.com/markbates/refresh/refresh"
Expand All @@ -14,7 +15,10 @@ func init() {
var initCmd = &cobra.Command{
Use: "init",
Short: "generates a default configuration file for you.",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// Do not report errors as wrong usage
cmd.SilenceUsage = true

c := refresh.Configuration{
AppRoot: ".",
IgnoredFolders: []string{"vendor", "log", "logs", "tmp", "node_modules", "bin", "templates"},
Expand All @@ -27,6 +31,16 @@ var initCmd = &cobra.Command{
CommandEnv: []string{},
EnableColors: true,
}
c.Dump(cfgFile)

if cfgFile == "" {
cfgFile = "refresh.yml"
}

_, err := os.Stat(cfgFile)
if !os.IsNotExist(err) {
return fmt.Errorf("config file %q already exists, skipping init", cfgFile)
}

return c.Dump(cfgFile)
},
}
5 changes: 2 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ var RootCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Refresh (%s)\n\n", Version)
},
Run: func(cmd *cobra.Command, args []string) {
Run(cfgFile)
RunE: func(cmd *cobra.Command, args []string) error {
return Run(cfgFile)
},
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
Expand Down
14 changes: 5 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ var runCmd = &cobra.Command{
},
}

func Run(cfgFile string) {
func Run(cfgFile string) error {
ctx := context.Background()
RunWithContext(cfgFile, ctx)
return RunWithContext(cfgFile, ctx)
}

func RunWithContext(cfgFile string, ctx context.Context) {
func RunWithContext(cfgFile string, ctx context.Context) error {
c := &refresh.Configuration{}

if err := loadConfig(c, cfgFile); err != nil {
if err != ErrConfigNotExist {
log.Fatalln(err)
os.Exit(-1)
return err
}

log.Println("No configuration loaded, proceeding with defaults")
Expand All @@ -52,10 +51,7 @@ func RunWithContext(cfgFile string, ctx context.Context) {
}

r := refresh.NewWithContext(c, ctx)
if err := r.Start(); err != nil {
log.Fatalln(err)
os.Exit(-1)
}
return r.Start()
}

func loadConfig(c *refresh.Configuration, path string) error {
Expand Down
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module github.com/markbates/refresh

go 1.13
go 1.17

require (
github.com/fatih/color v1.9.0
github.com/fsnotify/fsnotify v1.4.7
github.com/fatih/color v1.13.0
github.com/fsnotify/fsnotify v1.5.1
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.6
gopkg.in/yaml.v2 v2.2.8
github.com/spf13/cobra v1.2.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
624 changes: 532 additions & 92 deletions go.sum

Large diffs are not rendered by default.

0 comments on commit fa07c64

Please sign in to comment.