Skip to content

Commit b3c1fa5

Browse files
committed
cli interface refactor
1 parent dea1eef commit b3c1fa5

35 files changed

+1259
-603
lines changed

README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Performance focused BitTorrent tracker supporting HTTP, UDP, IPv4 and IPv6.
55
- [Trakx](#trakx)
66
- [❤️‍🔥 Instances](#️-instances)
77
- [🚀 Install](#-install)
8+
- [🧰 CLI](#-cli)
89
- [🔧 Configuration](#-configuration)
910
- [Configuration file](#configuration-file)
1011
- [Database backups](#database-backups)
@@ -13,7 +14,6 @@ Performance focused BitTorrent tracker supporting HTTP, UDP, IPv4 and IPv6.
1314
- [Netdata setup](#netdata-setup)
1415
- [Build Customization](#build-customization)
1516
- [**Performance**](#performance)
16-
- [**App engines**](#app-engines)
1717
- [📈 Performance](#-performance)
1818

1919
## ❤️‍🔥 Instances
@@ -35,22 +35,37 @@ Go 1.21+ required.
3535
git clone https://github.com/crimist/trakx && cd trakx
3636

3737
# install to go bin
38-
go install .
38+
go install ./cli
3939
trakx status # generates configuration
4040

4141
# or build
42-
go build .
42+
go build -o trakx ./cli
4343
./trakx status # generates configuration
4444
```
4545

4646
See [configuration](#configuration) and [netdata setup](#netdata-setup).
4747

48+
## 🧰 CLI
49+
50+
Common commands:
51+
52+
```sh
53+
trakx run # foreground
54+
trakx start # background
55+
trakx stop # stop daemon
56+
trakx restart # restart daemon
57+
trakx status # status check
58+
trakx logs -f # follow latest log file
59+
trakx pid show # inspect pid file
60+
trakx config path # show config path
61+
```
62+
4863
## 🔧 Configuration
4964

5065
### Configuration file
5166

5267
The configuration file can be found at `~/.config/trakx/trakx.yaml`.
53-
You'll have to run the trakx controller at least once to generate this file.
68+
You'll have to run trakx at least once (for example `trakx status`) to generate this file.
5469

5570
Config settings can be overwritten with environment variables:
5671

@@ -73,18 +88,19 @@ Trakx attempts to load the config file from the following directories in order:
7388
### Database backups
7489

7590
Trakx persists the in-memory database to a binary snapshot at `db.backup.path`.
76-
`backup-export` requests a fresh snapshot from the running daemon when available,
91+
`backup export` requests a fresh snapshot from the running daemon when available,
7792
and otherwise streams the on-disk backup file.
93+
`backup import` validates snapshots before replacing the on-disk backup.
7894
You can stream the snapshot bytes to external storage using:
7995

8096
```sh
81-
trakx backup-export > trakx.snapshot
82-
cat trakx.snapshot | trakx backup-import
97+
trakx backup export > trakx.snapshot
98+
cat trakx.snapshot | trakx backup import
8399
```
84100

85101
### Default configuration & webserver files
86102

87-
You can modify the default configuration and files served by the webserver in the `tracker/config/embeded/` folder.
103+
You can modify the default configuration and files served by the webserver in the `config/embedded/` folder.
88104

89105
**NOTE:** Trakx webserver will only serve files at their full path. `dmca` will 404, `dmca.html` will 200.
90106

@@ -114,12 +130,6 @@ Trakx takes advantage of Go's build tags to target different use cases.
114130

115131
The `fast` tag will build Trakx without IP, seed, and leech metrics which will reduce cpu and memory usage.
116132

117-
#### **App engines**
118-
119-
The `heroku` tag will build trakx for app engines. This means the executable will immediately run the tracker rather than provide the daemon controller behavior included in the regular CLI build.
120-
121-
Feel free to customize the tags to suit whichever app engine you prefer in [engine_entry.go](./engine_entry.go).
122-
123133
## 📈 Performance
124134

125135
The following metrics were collected on Heroku free tier running an HTTP tracker with the `fast` tag disabled.

cli/cmd_backup.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
8+
"github.com/crimist/trakx/cmd"
9+
)
10+
11+
func newBackupRootCommand() *Command {
12+
cmd := &Command{
13+
Name: "backup",
14+
Short: "Manage DB backups",
15+
Usage: "trakx backup <subcommand>",
16+
}
17+
cmd.Add(exportSubcommand())
18+
cmd.Add(importSubcommand())
19+
return cmd
20+
}
21+
22+
func exportSubcommand() *Command {
23+
flags, help := newFlagSet("backup export")
24+
outPath := flags.String("out", "", "Write backup to file (default: stdout)")
25+
26+
cmd := &Command{
27+
Name: "export",
28+
Short: "Stream DB backup",
29+
Usage: "trakx backup export [--out <file>]",
30+
Flags: flags,
31+
HelpFlag: help,
32+
Run: func(ctx *Context, args []string) error {
33+
conf, err := ctx.LoadConfig()
34+
if err != nil {
35+
return err
36+
}
37+
38+
var out io.Writer = ctx.Stdout
39+
var file *os.File
40+
if *outPath != "" {
41+
file, err = os.Create(*outPath)
42+
if err != nil {
43+
return err
44+
}
45+
defer file.Close()
46+
out = file
47+
}
48+
49+
if err := cmd.ExportBackup(conf, out); err != nil {
50+
return err
51+
}
52+
if *outPath != "" {
53+
fmt.Fprintln(ctx.Stdout, "backup exported")
54+
}
55+
return nil
56+
},
57+
}
58+
return cmd
59+
}
60+
61+
func importSubcommand() *Command {
62+
flags, help := newFlagSet("backup import")
63+
inPath := flags.String("in", "", "Read backup from file (default: stdin)")
64+
65+
cmd := &Command{
66+
Name: "import",
67+
Short: "Load DB backup",
68+
Usage: "trakx backup import [--in <file>]",
69+
Flags: flags,
70+
HelpFlag: help,
71+
Run: func(ctx *Context, args []string) error {
72+
conf, err := ctx.LoadConfig()
73+
if err != nil {
74+
return err
75+
}
76+
77+
var in io.Reader = os.Stdin
78+
var file *os.File
79+
if *inPath != "" {
80+
file, err = os.Open(*inPath)
81+
if err != nil {
82+
return err
83+
}
84+
defer file.Close()
85+
in = file
86+
}
87+
88+
if err := cmd.ImportBackup(conf, in); err != nil {
89+
return err
90+
}
91+
if *inPath != "" {
92+
fmt.Fprintln(ctx.Stdout, "backup imported")
93+
}
94+
return nil
95+
},
96+
}
97+
return cmd
98+
}

cli/cmd_config.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/crimist/trakx/config"
7+
)
8+
9+
func newConfigCommand() *Command {
10+
cmd := &Command{
11+
Name: "config",
12+
Short: "Manage configuration",
13+
Usage: "trakx config <subcommand>",
14+
}
15+
cmd.Add(newConfigDumpCommand())
16+
cmd.Add(newConfigPathCommand())
17+
cmd.Add(newConfigValidateCommand())
18+
return cmd
19+
}
20+
21+
func newConfigDumpCommand() *Command {
22+
flags, help := newFlagSet("config dump")
23+
cmd := &Command{
24+
Name: "dump",
25+
Short: "Print default config",
26+
Usage: "trakx config dump",
27+
Flags: flags,
28+
HelpFlag: help,
29+
Run: func(ctx *Context, args []string) error {
30+
return config.DumpDefaultConfig()
31+
},
32+
}
33+
return cmd
34+
}
35+
36+
func newConfigPathCommand() *Command {
37+
flags, help := newFlagSet("config path")
38+
cmd := &Command{
39+
Name: "path",
40+
Short: "Print resolved config path",
41+
Usage: "trakx config path",
42+
Flags: flags,
43+
HelpFlag: help,
44+
Run: func(ctx *Context, args []string) error {
45+
path, err := ctx.ResolvedConfigPath()
46+
if err != nil {
47+
return err
48+
}
49+
fmt.Fprintln(ctx.Stdout, path)
50+
return nil
51+
},
52+
}
53+
return cmd
54+
}
55+
56+
func newConfigValidateCommand() *Command {
57+
flags, help := newFlagSet("config validate")
58+
cmd := &Command{
59+
Name: "validate",
60+
Short: "Validate config",
61+
Usage: "trakx config validate",
62+
Flags: flags,
63+
HelpFlag: help,
64+
Run: func(ctx *Context, args []string) error {
65+
if _, err := ctx.LoadConfig(); err != nil {
66+
return err
67+
}
68+
fmt.Fprintln(ctx.Stdout, "ok")
69+
return nil
70+
},
71+
}
72+
return cmd
73+
}

cli/cmd_help.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func newHelpCommand(root *Command) *Command {
6+
flags, help := newFlagSet("help")
7+
cmd := &Command{
8+
Name: "help",
9+
Short: "Help about any command",
10+
Usage: "trakx help [command]",
11+
Flags: flags,
12+
HelpFlag: help,
13+
Run: func(ctx *Context, args []string) error {
14+
if len(args) == 0 {
15+
root.PrintHelp(ctx)
16+
return nil
17+
}
18+
19+
if target, ok := root.FindPath(args); ok {
20+
target.PrintHelp(ctx)
21+
return nil
22+
}
23+
24+
return fmt.Errorf("unknown command: %s", args[0])
25+
},
26+
}
27+
return cmd
28+
}

0 commit comments

Comments
 (0)