mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 03:32:33 +08:00
Feature: add more command-line options (#656)
add command-line options to override `external-controller`, `secret` and `external-ui` (#531)
This commit is contained in:
parent
51b6b8521b
commit
41a9488cfa
27
hub/hub.go
27
hub/hub.go
|
@ -3,15 +3,40 @@ package hub
|
|||
import (
|
||||
"github.com/Dreamacro/clash/hub/executor"
|
||||
"github.com/Dreamacro/clash/hub/route"
|
||||
"github.com/Dreamacro/clash/config"
|
||||
)
|
||||
|
||||
type Option func(*config.Config)
|
||||
|
||||
func WithExternalUI(externalUI string) Option {
|
||||
return func(cfg *config.Config) {
|
||||
cfg.General.ExternalUI = externalUI
|
||||
}
|
||||
}
|
||||
|
||||
func WithExternalController(externalController string) Option {
|
||||
return func(cfg *config.Config) {
|
||||
cfg.General.ExternalController = externalController
|
||||
}
|
||||
}
|
||||
|
||||
func WithSecret(secret string) Option {
|
||||
return func(cfg *config.Config) {
|
||||
cfg.General.Secret = secret
|
||||
}
|
||||
}
|
||||
|
||||
// Parse call at the beginning of clash
|
||||
func Parse() error {
|
||||
func Parse(options ...Option) error {
|
||||
cfg, err := executor.Parse()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, option := range options {
|
||||
option(cfg)
|
||||
}
|
||||
|
||||
if cfg.General.ExternalUI != "" {
|
||||
route.SetUIPath(cfg.General.ExternalUI)
|
||||
}
|
||||
|
|
33
main.go
33
main.go
|
@ -18,18 +18,30 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
version bool
|
||||
testConfig bool
|
||||
homeDir string
|
||||
configFile string
|
||||
flagset map[string]bool
|
||||
version bool
|
||||
testConfig bool
|
||||
homeDir string
|
||||
configFile string
|
||||
externalUI string
|
||||
externalController string
|
||||
secret string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&homeDir, "d", "", "set configuration directory")
|
||||
flag.StringVar(&configFile, "f", "", "specify configuration file")
|
||||
flag.StringVar(&externalUI, "ext-ui", "", "override external ui directory")
|
||||
flag.StringVar(&externalController, "ext-ctl", "", "override external controller address")
|
||||
flag.StringVar(&secret, "secret", "", "override secret for RESTful API")
|
||||
flag.BoolVar(&version, "v", false, "show current version of clash")
|
||||
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
|
||||
flag.Parse()
|
||||
|
||||
flagset = map[string]bool{}
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
flagset[f.Name] = true
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -71,7 +83,18 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
if err := hub.Parse(); err != nil {
|
||||
var options []hub.Option
|
||||
if flagset["ext-ui"] {
|
||||
options = append(options, hub.WithExternalUI(externalUI))
|
||||
}
|
||||
if flagset["ext-ctl"] {
|
||||
options = append(options, hub.WithExternalController(externalController))
|
||||
}
|
||||
if flagset["secret"] {
|
||||
options = append(options, hub.WithSecret(secret))
|
||||
}
|
||||
|
||||
if err := hub.Parse(options...); err != nil {
|
||||
log.Fatalln("Parse config error: %s", err.Error())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user