init
This commit is contained in:
38
cmd/init.go
Normal file
38
cmd/init.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user