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 init without explicit config flag #41

Open
wants to merge 3 commits into
base: master
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
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