Files
envy/cmd/init.go

57 lines
1.3 KiB
Go

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) {
_, err := os.Stat(utils.EnvyPath)
if os.IsExist(err) {
fmt.Println(utils.ErrStyle.Render("Config directory already exists."))
return
}
err = os.Mkdir(utils.EnvyPath, 0755)
if err != nil {
fmt.Println(utils.ErrStyle.Render("Failed to create config directory:", err.Error()))
}
configFile, err := os.Create(path.Join(utils.EnvyPath, "config.toml"))
if err != nil {
fmt.Println(utils.ErrStyle.Render("Failed to create config file:", err.Error()))
}
defer configFile.Close()
utils.InitDb()
utils.MigrateDb()
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")
}