Skip to content

Commit 81ad313

Browse files
authored
Merge pull request #60 from nothub/feature/optional-mods
flags for handling optional mods
2 parents 0fe0c23 + eda9fe8 commit 81ad313

File tree

5 files changed

+91
-23
lines changed

5 files changed

+91
-23
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Examples:
2525
mrpack-install adrenaserver --server-file srv.jar
2626
mrpack-install yK0ISmKn 1.0.0-1.18 --server-dir mcserver
2727
mrpack-install communitypack9000 --host api.labrinth.example.org
28-
mrpack-install --version
28+
mrpack-install example.mrpack --optional-select 'foo\.jar' \
29+
--optional-select 'bar-[\d+\.]+\.jar'
2930
3031
Available Commands:
3132
completion Generate the autocompletion script for the specified shell
@@ -36,15 +37,17 @@ Available Commands:
3637
version Print version infos
3738
3839
Flags:
39-
--dl-retries uint8 Retries when download fails (default 3)
40-
--dl-threads uint8 Concurrent download threads (default 8)
41-
-h, --help help for mrpack-install
42-
--host string Labrinth host address (default "api.modrinth.com")
43-
--proxy string Proxy url for http connections
44-
--server-dir string Server directory path (default "mc")
45-
--server-file string Server jar file name
46-
-v, --verbose Enable verbose output
47-
-V, --version Print version and exit
40+
--dl-retries uint8 Retries when download fails (default 3)
41+
--dl-threads uint8 Concurrent download threads (default 8)
42+
-h, --help help for mrpack-install
43+
--host string Labrinth host address (default "api.modrinth.com")
44+
--optional-disable-all Disable all optional mods
45+
--optional-select stringArray Select optional mods by file path (regex)
46+
--proxy string Proxy url for http connections
47+
--server-dir string Server directory path (default "mc")
48+
--server-file string Server jar file name
49+
-v, --verbose Enable verbose output
50+
-V, --version Print version and exit
4851
4952
Use "mrpack-install [command] --help" for more information about a command.
5053
@@ -109,8 +112,10 @@ Usage:
109112
mrpack-install update [<version>] [flags]
110113
111114
Flags:
112-
--backup-dir string Backup directory path
113-
-h, --help help for update
115+
--backup-dir string Backup directory path
116+
-h, --help help for update
117+
--optional-disable-all Disable all optional mods
118+
--optional-select stringArray Select optional mods by file path (regex)
114119
115120
Global Flags:
116121
--dl-retries uint8 Retries when download fails (default 3)

cmd/root.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"log"
1414
"os"
1515
"path/filepath"
16+
"regexp"
1617
"runtime"
1718
"strings"
1819
)
@@ -27,6 +28,12 @@ var (
2728
dlRetries uint8
2829
)
2930

31+
var (
32+
// local options
33+
optionalSelected []string
34+
optionalDisableAll bool
35+
)
36+
3037
func init() {
3138
var printVersion bool
3239
RootCmd.Flags().BoolVarP(&printVersion, "version", "V", false, "Print version and exit")
@@ -46,6 +53,9 @@ func init() {
4653
RootCmd.PersistentFlags().Uint8Var(&dlThreads, "dl-threads", 8, "Concurrent download threads")
4754
RootCmd.PersistentFlags().Uint8Var(&dlRetries, "dl-retries", 3, "Retries when download fails")
4855

56+
RootCmd.Flags().StringArrayVar(&optionalSelected, "optional-select", nil, "Select optional mods by file path (regex)")
57+
RootCmd.Flags().BoolVar(&optionalDisableAll, "optional-disable-all", false, "Disable all optional mods")
58+
4959
cobra.OnInitialize(func() {
5060
if printVersion {
5161
buildinfo.Print()
@@ -101,7 +111,8 @@ var RootCmd = &cobra.Command{
101111
mrpack-install adrenaserver --server-file srv.jar
102112
mrpack-install yK0ISmKn 1.0.0-1.18 --server-dir mcserver
103113
mrpack-install communitypack9000 --host api.labrinth.example.org
104-
mrpack-install --version`,
114+
mrpack-install example.mrpack --optional-select 'foo\.jar' \
115+
--optional-select 'bar-[\d+\.]+\.jar'`,
105116
Args: cobra.RangeArgs(1, 2),
106117
Run: func(cmd *cobra.Command, args []string) {
107118
input := args[0]
@@ -144,7 +155,30 @@ var RootCmd = &cobra.Command{
144155
}
145156

146157
// downloads
147-
downloads := index.ServerDownloads()
158+
downloads := index.ServerDownloads(func(f mrpack.File) bool {
159+
switch f.Env.Server {
160+
case modrinth.RequiredEnvSupport:
161+
return true
162+
case modrinth.OptionalEnvSupport:
163+
if optionalDisableAll {
164+
return false
165+
}
166+
if len(optionalSelected) < 1 {
167+
return true
168+
}
169+
for _, p := range optionalSelected {
170+
if regexp.MustCompile(p).Match([]byte(filepath.Base(f.Path))) {
171+
return true
172+
}
173+
}
174+
return false
175+
case modrinth.UnsupportedEnvSupport:
176+
return false
177+
default:
178+
log.Fatalf("Unexpected environment configuration %q for mod %q\n", f.Env.Server, f.Path)
179+
return false
180+
}
181+
})
148182
log.Printf("Downloading %v dependencies...\n", len(downloads))
149183
downloader := download.Downloader{
150184
Downloads: downloads,

cmd/update.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func init() {
1818
// TODO flags: --start-server
1919
updateCmd.Flags().StringVar(&backupDir, "backup-dir", "", "Backup directory path")
2020

21+
updateCmd.Flags().StringArrayVar(&optionalSelected, "optional-select", nil, "Select optional mods by file path (regex)")
22+
updateCmd.Flags().BoolVar(&optionalDisableAll, "optional-disable-all", false, "Disable all optional mods")
23+
2124
RootCmd.AddCommand(updateCmd)
2225
}
2326

@@ -69,6 +72,6 @@ var updateCmd = &cobra.Command{
6972
log.Fatalln(err)
7073
}
7174

72-
update.Cmd(serverDir, dlThreads, dlRetries, index, zipPath, state)
75+
update.Cmd(serverDir, dlThreads, dlRetries, index, zipPath, state, optionalSelected, optionalDisableAll)
7376
},
7477
}

modrinth/mrpack/index.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,17 @@ func ReadIndex(zipFile string) (*Index, error) {
111111
return &index, nil
112112
}
113113

114-
func (index *Index) ServerDownloads() []*download.Download {
114+
func (index *Index) ServerDownloads(filter func(f File) bool) []*download.Download {
115115
var downloads []*download.Download
116116
for _, file := range index.Files {
117117
if file.Env.Server == modrinth.UnsupportedEnvSupport {
118118
continue
119119
}
120120

121+
if !filter(file) {
122+
continue
123+
}
124+
121125
if len(file.Downloads) < 1 {
122126
log.Printf("No downloads for file: %s\n", file.Path)
123127
continue

update/command.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/nothub/hashutils/chksum"
66
"github.com/nothub/hashutils/encoding"
77
"github.com/nothub/mrpack-install/files"
8+
modrinth "github.com/nothub/mrpack-install/modrinth/api"
89
"github.com/nothub/mrpack-install/modrinth/mrpack"
910
"github.com/nothub/mrpack-install/update/backup"
1011
"github.com/nothub/mrpack-install/update/packstate"
@@ -13,10 +14,11 @@ import (
1314
"os"
1415
"path/filepath"
1516
"reflect"
17+
"regexp"
1618
"slices"
1719
)
1820

19-
func Cmd(serverDir string, dlThreads uint8, dlRetries uint8, index *mrpack.Index, zipPath string, oldState *packstate.Schema) {
21+
func Cmd(serverDir string, dlThreads uint8, dlRetries uint8, index *mrpack.Index, zipPath string, oldState *packstate.Schema, optionalSelected []string, optionalDisableAll bool) {
2022
log.Printf("Updating %q in %q with %q\n", index.Name, serverDir, zipPath)
2123
err := os.Chdir(serverDir)
2224
if err != nil {
@@ -81,13 +83,33 @@ func Cmd(serverDir string, dlThreads uint8, dlRetries uint8, index *mrpack.Index
8183
}
8284

8385
// downloads
84-
var downloads []*download.Download
85-
for _, dl := range index.ServerDownloads() {
86-
if !slices.Contains(ignores, dl.Path) {
87-
downloads = append(downloads, dl)
86+
downloads := index.ServerDownloads(func(f mrpack.File) bool {
87+
if slices.Contains(ignores, f.Path) {
88+
return false
8889
}
89-
}
90-
90+
switch f.Env.Server {
91+
case modrinth.RequiredEnvSupport:
92+
return true
93+
case modrinth.OptionalEnvSupport:
94+
if optionalDisableAll {
95+
return false
96+
}
97+
if len(optionalSelected) < 1 {
98+
return true
99+
}
100+
for _, p := range optionalSelected {
101+
if regexp.MustCompile(p).Match([]byte(filepath.Base(f.Path))) {
102+
return true
103+
}
104+
}
105+
return false
106+
case modrinth.UnsupportedEnvSupport:
107+
return false
108+
default:
109+
log.Fatalf("Unexpected environment configuration %q for mod %q\n", f.Env.Server, f.Path)
110+
return false
111+
}
112+
})
91113
log.Printf("Downloading %v dependencies...\n", len(downloads))
92114
downloader := download.Downloader{
93115
Downloads: downloads,

0 commit comments

Comments
 (0)