mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: implement update command and add error check for toml key access
This commit is contained in:
62
cmd/update.go
Normal file
62
cmd/update.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"envy/cmd/utils"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// updateCmd represents the update command
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Update the current environment data with the new one",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
envy, err := utils.GetEnvy()
|
||||
utils.StopIfErr(err)
|
||||
|
||||
project, err := utils.GetKey(envy, "envy.project")
|
||||
utils.StopIfErr(err)
|
||||
|
||||
current, err := utils.GetKey(envy, "envy.current")
|
||||
utils.StopIfErr(err)
|
||||
|
||||
projectDb, err := utils.GetProject(project)
|
||||
utils.StopIfErr(err)
|
||||
|
||||
currentDb, err := utils.GetEnvironment(projectDb.ID, current)
|
||||
utils.StopIfErr(err)
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
utils.StopIfErr(err)
|
||||
|
||||
env, err := os.ReadFile(path.Join(cwd, ".env"))
|
||||
utils.StopIfErr(err)
|
||||
|
||||
if currentDb.Data == utils.ParseEnv(string(env)) {
|
||||
utils.ErrPrint("No changes found to update.")
|
||||
return
|
||||
}
|
||||
|
||||
result := utils.DB.Model(¤tDb).Update("data", utils.ParseEnv(string(env)))
|
||||
utils.StopIfErr(result.Error)
|
||||
|
||||
fmt.Printf("Updated data for environment %s.\n", current)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// updateCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// updateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
Reference in New Issue
Block a user