z_mirror/fmm

git clone https://git.raiguard.me/z_mirror/fmm

Caleb HeuerImprove organization269870f

predep-parsing
2.9 KiB167 linesraw
1package main
2
3import (
4	"fmt"
5	"path"
6	"strings"
7)
8
9func disable(args []string) {
10	if len(args) == 0 {
11		disableAll()
12		return
13	}
14
15	dir, err := newDir(modsDir)
16	if err != nil {
17		abort(err)
18	}
19	defer dir.save()
20
21	var mods []Dependency
22	for _, input := range args {
23		mods = append(mods, Dependency{Ident: newModIdent(input), Req: VersionAny})
24	}
25	for _, mod := range mods {
26		file, entry, err := dir.Find(mod)
27		if err != nil {
28			errorln(err)
29			continue
30		}
31		if !entry.Enabled {
32			continue
33		}
34		entry.Enabled = false
35		fmt.Println("Disabled", file.Ident.Name)
36	}
37}
38
39func disableAll() {
40	list, err := newModList(path.Join(modsDir, "mod-list.json"))
41	if err != nil {
42		abort(err)
43	}
44	defer list.save()
45
46	for i := range list.Mods {
47		mod := &list.Mods[i]
48		if mod.Name != "base" {
49			mod.Enabled = false
50		}
51	}
52
53	fmt.Println("Disabled all mods")
54}
55
56func enable(args []string) {
57	if len(args) == 0 {
58		abort("no mods were provided")
59	}
60
61	dir, err := newDir(modsDir)
62	if err != nil {
63		abort(err)
64	}
65	defer dir.save()
66
67	var mods []Dependency
68	for _, input := range args {
69		mods = append(mods, Dependency{Ident: newModIdent(input), Req: VersionEq})
70	}
71
72	i := 0
73	for {
74		if i > len(mods)-1 {
75			break
76		}
77		mod := mods[i]
78		i++
79		file, entry, err := dir.Find(mod)
80		if err != nil {
81			errorln(err)
82			continue
83		}
84		if entry.Enabled {
85			if mod.Ident.Version == nil || mod.Ident.Version.cmp(entry.Version) == VersionEq {
86				continue
87			}
88		}
89		entry.Enabled = true
90		entry.Version = mod.Ident.Version
91		fmt.Println("Enabled", file.Ident.toString())
92
93		deps, err := file.Dependencies()
94		if err != nil {
95			errorln(err)
96		}
97		if deps == nil {
98			continue
99		}
100
101		for _, dep := range *deps {
102			if dep.Ident.Name != "base" && dep.Kind == DependencyRequired {
103				mods = append(mods, dep)
104			}
105		}
106	}
107}
108
109func install(args []string) {
110	if len(args) == 0 {
111		abort("no mods were provided")
112	}
113
114	if downloadUsername == "" {
115		abort("Username not specified")
116	}
117	if downloadToken == "" {
118		abort("Token not specified")
119	}
120
121	dir, err := newDir(modsDir)
122	if err != nil {
123		abort(err)
124	}
125
126	var mods []Dependency
127	for _, input := range args {
128		mods = append(mods, Dependency{Ident: newModIdent(input), Req: VersionEq})
129	}
130
131	for _, mod := range mods {
132		// TODO: Do we want to do this?
133		if file, _, _ := dir.Find(mod); file != nil {
134			fmt.Println(file.Ident.toString(), "is already in the mods directory")
135			continue
136		}
137
138		err := portalDownloadMod(mod, dir)
139		if err != nil {
140			errorln(err)
141		}
142	}
143}
144
145func sync(files []string) {
146	for _, file := range files {
147		if strings.HasSuffix(file, ".log") {
148			if err := parseLogFile(file); err != nil {
149				errorln(err)
150			}
151		}
152	}
153}
154
155func upload(files []string) {
156	if apiKey == "" {
157		abort("API key not specified.")
158	}
159	if len(files) == 0 {
160		abort("no files were provided")
161	}
162	for _, file := range files {
163		if err := portalUploadMod(file); err != nil {
164			abort("Upload failed:", err)
165		}
166	}
167}