From 6e5c6da1ac714313476af8f868f40e5b91d3f0fc Mon Sep 17 00:00:00 2001 From: Meghan Date: Mon, 9 Nov 2020 14:52:16 -0800 Subject: [PATCH] add add command --- cmd_add.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 cmd_add.go diff --git a/cmd_add.go b/cmd_add.go new file mode 100644 index 0000000000000000000000000000000000000000..844a3449f62bc9c56b66e044b4f363e675ce9dce --- /dev/null +++ b/cmd_add.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + + "github.com/nektro/go-util/arrays/stringsu" + "github.com/nektro/go-util/util" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +func init() { + rootCmd.AddCommand(&cobra.Command{ + Use: "add", + Short: "Add a new dependency to your project", + Long: `zigmod add `, + Run: func(cmd *cobra.Command, args []string) { + assert(len(args) >= 1, "missing package parameter") + assert(len(args) >= 2, "missing package parameter") + + dept := args[0] + path := args[1] + + assert(stringsu.Contains(AllDepTypes, dept), "provided parameter is not a valid dependency type") + + f, err := os.Open("./zig.mod") + util.DieOnError(err) + bys, _ := ioutil.ReadAll(f) + + m := new(ModFile) + yaml.Unmarshal(bys, &m) + + for _, item := range m.Deps { + if item.Type == dept && item.Path == path { + assert(false, "dependency already added, skipping!") + } + } + m.Deps = append(m.Deps, Dep{dept, path}) + + f.Close() + f, err = os.Create("./zig.mod") + util.DieOnError(err) + d, _ := yaml.Marshal(m) + fmt.Fprint(f, string(d)) + log.Println("Successfully added", path) + }, + }) +} -- 2.54.0