feat: add init command and setup db

This commit is contained in:
2025-09-22 21:13:17 +05:30
parent 1847b1795d
commit 13702d117a
7 changed files with 157 additions and 17 deletions

71
cmd/init.go Normal file
View File

@@ -0,0 +1,71 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"envy/cmd/utils"
"fmt"
"os"
"path"
"github.com/spf13/cobra"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize envy on your system",
Run: func(cmd *cobra.Command, args []string) {
config, err := os.UserConfigDir()
if err != nil {
fmt.Println(utils.ErrStyle.Render("Config directory not found for your OS. Please run `envy init <path>`."))
return
}
envyPath := path.Join(config, "envy")
_, err = os.Stat(envyPath)
if os.IsExist(err) {
fmt.Println(utils.ErrStyle.Render("Config directory already exists."))
return
}
err = os.Mkdir(envyPath, 0755)
if err != nil {
fmt.Println(utils.ErrStyle.Render("Failed to create config directory:", err.Error()))
}
configFile, err := os.Create(path.Join(envyPath, "config.toml"))
if err != nil {
fmt.Println(utils.ErrStyle.Render("Failed to create config file:", err.Error()))
}
defer configFile.Close()
_, err = os.Create(path.Join(envyPath, "envy.db"))
if err != nil {
fmt.Println(utils.ErrStyle.Render("Failed to create database file:", err.Error()))
}
utils.Migrate()
configFile.Write([]byte("[config]\nmode = \"local\"\n"))
fmt.Println("envy initialized on your system.")
},
}
func init() {
rootCmd.AddCommand(initCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}