Run executes the command.
(ctx context.Context, args []string)
| 16 | |
| 17 | // Run executes the command. |
| 18 | func (c *ResetCommand) Run(ctx context.Context, args []string) (err error) { |
| 19 | fs := flag.NewFlagSet("litestream-reset", flag.ContinueOnError) |
| 20 | configPath, noExpandEnv := registerConfigFlag(fs) |
| 21 | dryRun := fs.Bool("dry-run", false, "print local state that would be removed without deleting") |
| 22 | fs.Usage = c.Usage |
| 23 | if err := fs.Parse(args); err != nil { |
| 24 | return err |
| 25 | } |
| 26 | |
| 27 | // Validate arguments - need exactly one database path |
| 28 | if fs.NArg() == 0 { |
| 29 | return &usageError{ |
| 30 | message: "database path required", |
| 31 | hint: "litestream reset /path/to/db", |
| 32 | } |
| 33 | } else if fs.NArg() > 1 { |
| 34 | return fmt.Errorf("too many arguments") |
| 35 | } |
| 36 | |
| 37 | dbPath := fs.Arg(0) |
| 38 | |
| 39 | // Make absolute if needed |
| 40 | if !filepath.IsAbs(dbPath) { |
| 41 | if dbPath, err = filepath.Abs(dbPath); err != nil { |
| 42 | return err |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Load configuration to find the database (if config exists) |
| 47 | var dbConfig *DBConfig |
| 48 | if *configPath != "" { |
| 49 | config, configErr := ReadConfigFile(*configPath, !*noExpandEnv) |
| 50 | if configErr != nil { |
| 51 | return fmt.Errorf("cannot read config: %w", configErr) |
| 52 | } |
| 53 | |
| 54 | // Find database config |
| 55 | for _, dbc := range config.DBs { |
| 56 | expandedPath := dbc.Path |
| 57 | if !filepath.IsAbs(expandedPath) { |
| 58 | expandedPath, _ = filepath.Abs(expandedPath) |
| 59 | } |
| 60 | if expandedPath == dbPath { |
| 61 | dbConfig = dbc |
| 62 | break |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // If no config found, check if database file exists |
| 68 | if _, err := os.Stat(dbPath); os.IsNotExist(err) { |
| 69 | return fmt.Errorf("database does not exist: %s", dbPath) |
| 70 | } else if err != nil { |
| 71 | return fmt.Errorf("cannot access database: %w", err) |
| 72 | } |
| 73 | |
| 74 | // Create DB instance |
| 75 | var db *litestream.DB |