From e5a002e4b41cba7d4a7cbc637151232926448055 Mon Sep 17 00:00:00 2001 From: Suraj B M Date: Fri, 26 Sep 2025 10:48:35 +0530 Subject: [PATCH] feat: implement fetch command --- cmd/fetch.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 cmd/fetch.go diff --git a/cmd/fetch.go b/cmd/fetch.go new file mode 100644 index 0000000..ac4fed9 --- /dev/null +++ b/cmd/fetch.go @@ -0,0 +1,60 @@ +/* +Copyright © 2025 NAME HERE +*/ +package cmd + +import ( + "envy/cmd/utils" + "fmt" + "os" + "path" + + "github.com/spf13/cobra" +) + +// fetchCmd represents the fetch command +var fetchCmd = &cobra.Command{ + Use: "fetch", + Short: "Automatically get the pinned environment for this project", + Run: func(cmd *cobra.Command, args []string) { + envy, err := utils.GetEnvy() + utils.StopIfErr(err) + + project, err := utils.GetKey(envy, "envy.project") + utils.StopIfErr(err) + + pinned, err := utils.GetKey(envy, "envy.env") + utils.StopIfErr(err) + + dbProject, err := utils.GetProject(project) + utils.StopIfErr(err) + + dbEnv, err := utils.GetEnvironment(dbProject.ID, pinned) + utils.StopIfErr(err) + + cwd, err := os.Getwd() + utils.StopIfErr(err) + + os.WriteFile(path.Join(cwd, ".env"), []byte(dbEnv.Data), 0755) + + envy.Set("envy.current", pinned) + err = utils.WriteEnvy(envy) + utils.StopIfErr(err) + + fmt.Printf("Fetched environment %s for project %s.\n", pinned, project) + }, +} + +func init() { + rootCmd.AddCommand(fetchCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // fetchCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // fetchCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +}