authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-09 14:52:16 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-09 14:52:16 -08:00
log6e5c6da1ac714313476af8f868f40e5b91d3f0fc
tree1e874a14c2484713a100d95b9aacd24414750c88
parentff13dbee28d2033a96806ccce731ac1a76efb90c

add add command


1 files changed, 51 insertions(+), 0 deletions(-)

cmd_add.go created+51
......@@ -0,0 +1,51 @@
1package main
2
3import (
4 "fmt"
5 "io/ioutil"
6 "log"
7 "os"
8
9 "github.com/nektro/go-util/arrays/stringsu"
10 "github.com/nektro/go-util/util"
11 "github.com/spf13/cobra"
12 "gopkg.in/yaml.v2"
13)
14
15func init() {
16 rootCmd.AddCommand(&cobra.Command{
17 Use: "add",
18 Short: "Add a new dependency to your project",
19 Long: `zigmod add <type> <path>`,
20 Run: func(cmd *cobra.Command, args []string) {
21 assert(len(args) >= 1, "missing package <type> parameter")
22 assert(len(args) >= 2, "missing package <path> parameter")
23
24 dept := args[0]
25 path := args[1]
26
27 assert(stringsu.Contains(AllDepTypes, dept), "provided <type> parameter is not a valid dependency type")
28
29 f, err := os.Open("./zig.mod")
30 util.DieOnError(err)
31 bys, _ := ioutil.ReadAll(f)
32
33 m := new(ModFile)
34 yaml.Unmarshal(bys, &m)
35
36 for _, item := range m.Deps {
37 if item.Type == dept && item.Path == path {
38 assert(false, "dependency already added, skipping!")
39 }
40 }
41 m.Deps = append(m.Deps, Dep{dept, path})
42
43 f.Close()
44 f, err = os.Create("./zig.mod")
45 util.DieOnError(err)
46 d, _ := yaml.Marshal(m)
47 fmt.Fprint(f, string(d))
48 log.Println("Successfully added", path)
49 },
50 })
51}