2026-03-26 12:48:47 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"axolotl/db"
|
|
|
|
|
"axolotl/output"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var initCmd = &cobra.Command{
|
2026-03-27 02:11:46 +01:00
|
|
|
Use: "init [path]", Short: "Initialize a new database", Args: cobra.MaximumNArgs(1),
|
2026-03-26 12:48:47 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2026-03-27 02:11:46 +01:00
|
|
|
p := "."
|
2026-03-26 12:48:47 +00:00
|
|
|
if len(args) > 0 {
|
2026-03-27 02:11:46 +01:00
|
|
|
p = args[0]
|
2026-03-26 12:48:47 +00:00
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
dbPath := filepath.Join(p, ".ax.db")
|
2026-03-26 12:48:47 +00:00
|
|
|
if _, err := os.Stat(dbPath); err == nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, "database already exists:", dbPath)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
if err := db.Init(dbPath); err != nil {
|
2026-03-26 12:48:47 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "failed to initialize:", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
output.PrintAction(cmd.OutOrStdout(), "Created", dbPath, false)
|
2026-03-26 12:48:47 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 02:11:46 +01:00
|
|
|
func init() { rootCmd.AddCommand(initCmd) }
|