Skip to content

Commit

Permalink
chore: update cli
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Nov 4, 2023
1 parent edbb135 commit 0b93d1b
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ builds:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X main.builtBy=go-zoox
main: .
main: ./cmd/clash
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VERSION=$(shell git describe --tags || echo "unknown version")
BUILDTIME=$(shell date -u)
GOBUILD=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/doreamon-design/clash/constant.Version=$(VERSION)" \
-X "github.com/doreamon-design/clash/constant.BuildTime=$(BUILDTIME)" \
-w -s -buildid='
-w -s -buildid=' ./cmd/clash

PLATFORM_LIST = \
darwin-amd64 \
Expand Down
139 changes: 139 additions & 0 deletions cmd/clash/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/doreamon-design/clash"
"github.com/doreamon-design/clash/config"
"github.com/doreamon-design/clash/hub"
"github.com/doreamon-design/clash/hub/executor"
"github.com/doreamon-design/clash/log"
"github.com/go-zoox/cli"
"go.uber.org/automaxprocs/maxprocs"

C "github.com/doreamon-design/clash/constant"
)

func main() {
app := cli.NewSingleProgram(&cli.SingleProgramConfig{
Name: "clash",
Usage: "A rule-based tunnel in Go",
Version: clash.Version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "home-dir",
Usage: "set configuration directory",
EnvVars: []string{"CLASH_HOME_DIR"},
Aliases: []string{"d"},
},
&cli.StringFlag{
Name: "config",
Usage: "specify configuration file",
EnvVars: []string{"CLASH_CONFIG_FILE"},
Aliases: []string{"f"},
},
&cli.StringFlag{
Name: "ext-ui",
Usage: "override external ui directory",
EnvVars: []string{"CLASH_OVERRIDE_EXTERNAL_UI_DIR"},
},
&cli.StringFlag{
Name: "ext-ctl",
Usage: "override external controller address",
EnvVars: []string{"CLASH_OVERRIDE_EXTERNAL_CONTROLLER"},
},
&cli.StringFlag{
Name: "secret",
Usage: "override secret for RESTful API",
EnvVars: []string{"CLASH_OVERRIDE_SECRET"},
},
&cli.BoolFlag{
Name: "test",
Usage: "test configuration and exit",
Aliases: []string{"t"},
},
},
})

app.Command(func(ctx *cli.Context) error {
homeDir := ctx.String("home-dir")
configFile := ctx.String("config")
externalUI := ctx.String("ext-ui")
externalController := ctx.String("ext-ctl")
secret := ctx.String("secret")
testConfig := ctx.Bool("test")

maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))

if homeDir != "" {
if !filepath.IsAbs(homeDir) {
currentDir, _ := os.Getwd()
homeDir = filepath.Join(currentDir, homeDir)
}
C.SetHomeDir(homeDir)
}

if configFile != "" {
if !filepath.IsAbs(configFile) {
currentDir, _ := os.Getwd()
configFile = filepath.Join(currentDir, configFile)
}
C.SetConfig(configFile)
} else {
configFile := filepath.Join(C.Path.HomeDir(), C.Path.Config())
C.SetConfig(configFile)
}

if err := config.Init(C.Path.HomeDir()); err != nil {
log.Fatalln("Initial configuration directory error: %s", err.Error())
}

if testConfig {
if _, err := executor.Parse(); err != nil {
log.Errorln(err.Error())
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
os.Exit(1)
}
fmt.Printf("configuration file %s test is successful\n", C.Path.Config())
return nil
}

var options []hub.Option
if externalUI != "" {
options = append(options, hub.WithExternalUI(externalUI))
}
if externalController != "" {
options = append(options, hub.WithExternalController(externalController))
}
if secret != "" {
options = append(options, hub.WithSecret(secret))
}

if err := hub.Parse(options...); err != nil {
log.Fatalln("Parse config error: %s", err.Error())
}

termSign := make(chan os.Signal, 1)
hupSign := make(chan os.Signal, 1)
signal.Notify(termSign, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(hupSign, syscall.SIGHUP)
for {
select {
case <-termSign:
return nil
case <-hupSign:
if cfg, err := executor.ParseWithPath(C.Path.Config()); err == nil {
executor.ApplyConfig(cfg, true)
} else {
log.Errorln("Parse config error: %s", err.Error())
}
}
}
})

app.Run()
}
29 changes: 28 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/cors v1.2.1
github.com/go-chi/render v1.0.3
github.com/go-zoox/cli v1.3.6
github.com/gofrs/uuid/v5 v5.0.0
github.com/gorilla/websocket v1.5.0
github.com/insomniacslk/dhcp v0.0.0-20230816195147-b3ca2534940d
Expand All @@ -30,18 +31,44 @@ require (

require (
github.com/ajg/form v1.5.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-zoox/chalk v1.0.1 // indirect
github.com/go-zoox/config v1.2.10 // indirect
github.com/go-zoox/core-utils v1.2.9 // indirect
github.com/go-zoox/datetime v1.1.0 // indirect
github.com/go-zoox/dotenv v1.2.3 // indirect
github.com/go-zoox/encoding v1.2.1 // indirect
github.com/go-zoox/fs v1.3.13 // indirect
github.com/go-zoox/ini v1.0.4 // indirect
github.com/go-zoox/logger v1.4.4 // indirect
github.com/go-zoox/tag v1.2.2 // indirect
github.com/go-zoox/uuid v0.0.1 // indirect
github.com/goccy/go-yaml v1.9.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/oschwald/maxminddb-golang v1.11.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pierrec/lz4/v4 v4.1.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sevlyar/go-daemon v0.1.6 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect
github.com/urfave/cli/v2 v2.24.4 // indirect
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
)
Loading

0 comments on commit 0b93d1b

Please sign in to comment.