From 0450860eea82de09c291030df4393ac79264a9ad Mon Sep 17 00:00:00 2001 From: septs Date: Mon, 28 Aug 2023 21:05:01 +0800 Subject: [PATCH] Feature: add environs startup option support (#2909) --- main.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 3adfe3d..d94009d 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,6 @@ import ( ) var ( - flagset map[string]bool version bool testConfig bool homeDir string @@ -30,19 +29,14 @@ var ( ) 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.StringVar(&homeDir, "d", os.Getenv("CLASH_HOME_DIR"), "set configuration directory") + flag.StringVar(&configFile, "f", os.Getenv("CLASH_CONFIG_FILE"), "specify configuration file") + flag.StringVar(&externalUI, "ext-ui", os.Getenv("CLASH_OVERRIDE_EXTERNAL_UI_DIR"), "override external ui directory") + flag.StringVar(&externalController, "ext-ctl", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER"), "override external controller address") + flag.StringVar(&secret, "secret", os.Getenv("CLASH_OVERRIDE_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() { @@ -86,13 +80,13 @@ func main() { } var options []hub.Option - if flagset["ext-ui"] { + if externalUI != "" { options = append(options, hub.WithExternalUI(externalUI)) } - if flagset["ext-ctl"] { + if externalController != "" { options = append(options, hub.WithExternalController(externalController)) } - if flagset["secret"] { + if secret != "" { options = append(options, hub.WithSecret(secret)) }