Files
envy/cmd/update.go

53 lines
1.1 KiB
Go

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(&currentDb).Update("data", utils.ParseEnv(string(env)))
utils.StopIfErr(result.Error)
fmt.Printf("Updated data for environment %s.\n", current)
},
}
func init() {
rootCmd.AddCommand(updateCmd)
}