diff --git a/cmd/orchid/main.go b/cmd/orchid/main.go index 79757a0..56b3fef 100644 --- a/cmd/orchid/main.go +++ b/cmd/orchid/main.go @@ -27,12 +27,14 @@ func main() { logger.Logger.Info("Starting...") if configPath == "" { - logger.Logger.Fatal("Config flag is missing") + logger.Logger.Error("Config flag is missing") + trySetup(configPath) + return } wd, err := getWD(configPath) if err != nil { - logger.Logger.Fatal("Failed to find config directory: ", "err", err) + logger.Logger.Fatal("Failed to find config directory", "err", err) } // try to open the config file @@ -41,19 +43,11 @@ func main() { case err == nil: break case os.IsNotExist(err): - // handle potential errors during setup - err = trySetup(wd) - switch { - case errors.Is(err, errExitSetup): - // exit setup without questions - return - case err == nil: - return - default: - logger.Logger.Fatal("Failed to run setup", "err", err) - } + logger.Logger.Warn("Failed to open config file", "err", err) + trySetup(wd) + return default: - logger.Logger.Fatal("Open config file: ", "err", err) + logger.Logger.Fatal("Open config file", "err", err) } // config file opened with no errors @@ -63,7 +57,7 @@ func main() { var config startUpConfig err = yaml.NewDecoder(openConf).Decode(&config) if err != nil { - logger.Logger.Fatal("Invalid config file: ", "err", err) + logger.Logger.Fatal("Invalid config file", "err", err) } runDaemon(wd, config) @@ -115,3 +109,17 @@ func getWD(configPath string) (string, error) { } return filepath.Dir(wdAbs), nil } + +func trySetup(wd string) { + // handle potential errors during setup + err := runSetup(wd) + switch { + case errors.Is(err, errExitSetup): + // exit setup without questions + return + case err == nil: + return + default: + logger.Logger.Fatal("Failed to run setup", "err", err) + } +} diff --git a/cmd/orchid/setup.go b/cmd/orchid/setup.go index e63f981..018b540 100644 --- a/cmd/orchid/setup.go +++ b/cmd/orchid/setup.go @@ -23,7 +23,7 @@ import ( var errExitSetup = errors.New("exit setup") -func trySetup(wd string) error { +func runSetup(wd string) error { // ask about running the setup steps createFile := false err := survey.AskOne(&survey.Confirm{Message: fmt.Sprintf("Create Orchid config files in this directory: '%s'?", wd)}, &createFile) diff --git a/database/tx.go b/database/tx.go index 2b1c0c7..24c2cab 100644 --- a/database/tx.go +++ b/database/tx.go @@ -3,12 +3,15 @@ package database import ( "context" "database/sql" + "errors" ) +var errMissingSqlDB = errors.New("cannot open transaction without sql.DB") + func (q *Queries) UseTx(ctx context.Context, cb func(tx *Queries) error) error { sqlDB, ok := q.db.(*sql.DB) if !ok { - panic("cannot open transaction without sql.DB") + return errMissingSqlDB } tx, err := sqlDB.BeginTx(ctx, nil) if err != nil {