| ... | @@ -0,0 +1,51 @@ |
| 1 | package main |
| 2 | |
| 3 | import ( |
| 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 | |
| 15 | func 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 | } |