39 lines
727 B
Go
39 lines
727 B
Go
|
|
package cmd
|
||
|
|
|
||
|
|
import (
|
||
|
|
"axolotl/db"
|
||
|
|
"axolotl/output"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
|
||
|
|
"github.com/spf13/cobra"
|
||
|
|
)
|
||
|
|
|
||
|
|
var initCmd = &cobra.Command{
|
||
|
|
Use: "init [path]",
|
||
|
|
Short: "Initialize a new database",
|
||
|
|
Args: cobra.MaximumNArgs(1),
|
||
|
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
|
path := "."
|
||
|
|
if len(args) > 0 {
|
||
|
|
path = args[0]
|
||
|
|
}
|
||
|
|
dbPath := filepath.Join(path, ".ax.db")
|
||
|
|
if _, err := os.Stat(dbPath); err == nil {
|
||
|
|
fmt.Fprintln(os.Stderr, "database already exists:", dbPath)
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
err := db.Init(dbPath)
|
||
|
|
if err != nil {
|
||
|
|
fmt.Fprintln(os.Stderr, "failed to initialize:", err)
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
output.PrintCreated(cmd.OutOrStdout(), dbPath)
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
func init() {
|
||
|
|
rootCmd.AddCommand(initCmd)
|
||
|
|
}
|