-
Notifications
You must be signed in to change notification settings - Fork 0
/
starbound-workshop-linker.go
154 lines (132 loc) · 3.76 KB
/
starbound-workshop-linker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/nmrshll/go-cp"
"github.com/urfave/cli/v2"
)
type Mod struct {
ID string
Path string
}
var App *cli.App
func main() {
App = cli.NewApp()
App.Name = "Starbound Workshop Linker"
App.Usage = ""
App.Description = ""
App.Authors = []*cli.Author{
{
Name: "Shane Clark",
Email: "[email protected]",
},
}
App.Version = "1.0.0"
App.Commands = []*cli.Command{
{
Name: "symlink",
Aliases: []string{"s"},
Usage: "Links workshop files using symlinks. For running on the same machine as you.",
Flags: []cli.Flag{
&cli.StringFlag{Name: "workshop", Aliases: []string{"w"}, Usage: "Provides the path to look for mods in. Should end in workshop/content/211820", Required: true},
&cli.StringFlag{Name: "server", Aliases: []string{"s"}, Usage: "Provides the path to place mods in. Should end in /mods", Required: true},
},
Action: func(ctx *cli.Context) error {
paks, err := getPaks(ctx.String("workshop"))
if err != nil {
return err
}
paths, err := linkPaks(ctx.String("server"), paks)
if err != nil {
return err
}
fmt.Printf("Successfully linked %d mods.\n", len(paths))
return nil
},
},
{
Name: "copy",
Aliases: []string{"c"},
Usage: "Copies workshop files. For running on a separate dedicated machine.",
Flags: []cli.Flag{
&cli.StringFlag{Name: "workshop", Aliases: []string{"w"}, Usage: "Provides the path to look for mods in. Should end in workshop/content/211820", Required: true},
&cli.StringFlag{Name: "server", Aliases: []string{"s"}, Usage: "Provides the path to place mods in. Should end in /mods", Required: true},
},
Action: func(ctx *cli.Context) error {
paks, err := getPaks(ctx.String("workshop"))
if err != nil {
return err
}
paths, err := copyPaks(ctx.String("server"), paks)
if err != nil {
return err
}
fmt.Printf("Successfully copied %d mods.\n", len(paths))
return nil
},
},
{
Name: "unlink",
Aliases: []string{"u"},
Usage: "Removed all currently linked mods.",
Flags: []cli.Flag{
&cli.StringFlag{Name: "server", Aliases: []string{"s"}, Usage: "Provides the path to remove mods from. Should end in /mods", Required: true},
},
Action: func(ctx *cli.Context) error {
paks, err := getPaks(ctx.String("server"))
if err != nil {
return err
}
for _, pak := range paks {
if err := os.Remove(pak.Path); err != nil {
return err
}
}
fmt.Printf("Successfully removed %d mods.\n", len(paks))
return nil
},
},
}
if err := App.Run(os.Args); err != nil {
log.Fatalf("Failed to start application: %v", err)
}
}
func getPaks(dir string) ([]Mod, error) {
var returnString []Mod
if err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() || filepath.Ext(path) != ".pak" {
return nil
}
returnString = append(returnString, Mod{ID: filepath.Base(filepath.Dir(path)), Path: path})
return nil
}); err != nil {
return nil, err
}
return returnString, nil
}
func linkPaks(dir string, paks []Mod) ([]string, error) {
var returnString []string
for _, pak := range paks {
newPath := fmt.Sprintf("%s%c%s.pak", dir, os.PathSeparator, pak.ID)
if err := os.Symlink(pak.Path, newPath); err != nil {
return nil, err
}
returnString = append(returnString, newPath)
}
return returnString, nil
}
func copyPaks(dir string, paks []Mod) ([]string, error) {
var returnString []string
for _, pak := range paks {
newPath := fmt.Sprintf("%s%c%s.pak", dir, os.PathSeparator, pak.ID)
err := cp.CopyFile(pak.Path, newPath)
if err != nil {
return nil, err
}
returnString = append(returnString, newPath)
}
return returnString, nil
}