mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 00:21:30 +00:00
Improve cmd
This commit is contained in:
parent
529cfe2d9a
commit
c8399a297e
23
Makefile
23
Makefile
|
@ -1,9 +1,10 @@
|
||||||
NAME=sing-box
|
NAME = sing-box
|
||||||
COMMIT=$(shell git rev-parse --short HEAD)
|
COMMIT = $(shell git rev-parse --short HEAD)
|
||||||
PARAMS=-trimpath -tags '$(TAGS)' -ldflags \
|
TAGS ?= with_quic,with_clash_api
|
||||||
|
PARAMS = -trimpath -tags '$(TAGS)' -ldflags \
|
||||||
'-X "github.com/sagernet/sing-box/constant.Commit=$(COMMIT)" \
|
'-X "github.com/sagernet/sing-box/constant.Commit=$(COMMIT)" \
|
||||||
-w -s -buildid='
|
-w -s -buildid='
|
||||||
MAIN=./cmd/sing-box
|
MAIN = ./cmd/sing-box
|
||||||
|
|
||||||
.PHONY: test release
|
.PHONY: test release
|
||||||
|
|
||||||
|
@ -24,9 +25,9 @@ fmt_install:
|
||||||
go install -v github.com/daixiang0/gci@v0.4.0
|
go install -v github.com/daixiang0/gci@v0.4.0
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
gofumpt -l -w .
|
@gofumpt -l -w .
|
||||||
gofmt -s -w .
|
@gofmt -s -w .
|
||||||
gci write -s "standard,prefix(github.com/sagernet/),default" .
|
@gci write -s "standard,prefix(github.com/sagernet/),default" .
|
||||||
|
|
||||||
lint_install:
|
lint_install:
|
||||||
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
@ -38,10 +39,10 @@ lint:
|
||||||
GOOS=freebsd golangci-lint run ./...
|
GOOS=freebsd golangci-lint run ./...
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v . && \
|
@go test -v . && \
|
||||||
pushd test && \
|
@pushd test && \
|
||||||
go test -v . && \
|
@go test -v . && \
|
||||||
popd
|
@popd
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf bin dist
|
rm -rf bin dist
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/sagernet/sing-box/common/json"
|
"github.com/sagernet/sing-box/common/json"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -15,24 +16,31 @@ import (
|
||||||
var commandCheck = &cobra.Command{
|
var commandCheck = &cobra.Command{
|
||||||
Use: "check",
|
Use: "check",
|
||||||
Short: "Check configuration",
|
Short: "Check configuration",
|
||||||
Run: checkConfiguration,
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
Args: cobra.NoArgs,
|
err := check()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Args: cobra.NoArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkConfiguration(cmd *cobra.Command, args []string) {
|
func init() {
|
||||||
|
mainCommand.AddCommand(commandCheck)
|
||||||
|
}
|
||||||
|
|
||||||
|
func check() error {
|
||||||
configContent, err := os.ReadFile(configPath)
|
configContent, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("read config: ", err)
|
return E.Cause(err, "read config")
|
||||||
}
|
}
|
||||||
var options option.Options
|
var options option.Options
|
||||||
err = json.Unmarshal(configContent, &options)
|
err = json.Unmarshal(configContent, &options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("decode config: ", err)
|
return E.Cause(err, "decode config")
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
_, err = box.New(ctx, options)
|
_, err = box.New(ctx, options)
|
||||||
if err != nil {
|
|
||||||
log.Fatal("create service: ", err)
|
|
||||||
}
|
|
||||||
cancel()
|
cancel()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/sagernet/sing-box/common/json"
|
"github.com/sagernet/sing-box/common/json"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -17,47 +18,54 @@ var commandFormatFlagWrite bool
|
||||||
var commandFormat = &cobra.Command{
|
var commandFormat = &cobra.Command{
|
||||||
Use: "format",
|
Use: "format",
|
||||||
Short: "Format configuration",
|
Short: "Format configuration",
|
||||||
Run: formatConfiguration,
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
Args: cobra.NoArgs,
|
err := format()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Args: cobra.NoArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
commandFormat.Flags().BoolVarP(&commandFormatFlagWrite, "write", "w", false, "write result to (source) file instead of stdout")
|
commandFormat.Flags().BoolVarP(&commandFormatFlagWrite, "write", "w", false, "write result to (source) file instead of stdout")
|
||||||
|
mainCommand.AddCommand(commandFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatConfiguration(cmd *cobra.Command, args []string) {
|
func format() error {
|
||||||
configContent, err := os.ReadFile(configPath)
|
configContent, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("read config: ", err)
|
return E.Cause(err, "read config")
|
||||||
}
|
}
|
||||||
var options option.Options
|
var options option.Options
|
||||||
err = json.Unmarshal(configContent, &options)
|
err = json.Unmarshal(configContent, &options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("decode config: ", err)
|
return E.Cause(err, "decode config")
|
||||||
}
|
}
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
encoder := json.NewEncoder(buffer)
|
encoder := json.NewEncoder(buffer)
|
||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
err = encoder.Encode(options)
|
err = encoder.Encode(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("encode config: ", err)
|
return E.Cause(err, "encode config")
|
||||||
}
|
}
|
||||||
if !commandFormatFlagWrite {
|
if !commandFormatFlagWrite {
|
||||||
os.Stdout.WriteString(buffer.String() + "\n")
|
os.Stdout.WriteString(buffer.String() + "\n")
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
if bytes.Equal(configContent, buffer.Bytes()) {
|
if bytes.Equal(configContent, buffer.Bytes()) {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
output, err := os.Create(configPath)
|
output, err := os.Create(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("open output: ", err)
|
return E.Cause(err, "open output")
|
||||||
}
|
}
|
||||||
_, err = output.Write(buffer.Bytes())
|
_, err = output.Write(buffer.Bytes())
|
||||||
output.Close()
|
output.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("write output: ", err)
|
return E.Cause(err, "write output")
|
||||||
}
|
}
|
||||||
outputPath, _ := filepath.Abs(configPath)
|
outputPath, _ := filepath.Abs(configPath)
|
||||||
os.Stderr.WriteString(outputPath + "\n")
|
os.Stderr.WriteString(outputPath + "\n")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,17 +20,19 @@ import (
|
||||||
var commandRun = &cobra.Command{
|
var commandRun = &cobra.Command{
|
||||||
Use: "run",
|
Use: "run",
|
||||||
Short: "Run service",
|
Short: "Run service",
|
||||||
Run: run,
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
err := run()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(cmd *cobra.Command, args []string) {
|
func init() {
|
||||||
err := run0()
|
mainCommand.AddCommand(commandRun)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func run0() error {
|
func run() error {
|
||||||
configContent, err := os.ReadFile(configPath)
|
configContent, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "read config")
|
return E.Cause(err, "read config")
|
||||||
|
|
|
@ -21,6 +21,7 @@ var nameOnly bool
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only")
|
commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only")
|
||||||
|
mainCommand.AddCommand(commandVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printVersion(cmd *cobra.Command, args []string) {
|
func printVersion(cmd *cobra.Command, args []string) {
|
||||||
|
|
|
@ -23,11 +23,6 @@ func init() {
|
||||||
mainCommand.PersistentFlags().StringVarP(&configPath, "config", "c", "config.json", "set configuration file path")
|
mainCommand.PersistentFlags().StringVarP(&configPath, "config", "c", "config.json", "set configuration file path")
|
||||||
mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
|
mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
|
||||||
mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
|
mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
|
||||||
|
|
||||||
mainCommand.AddCommand(commandRun)
|
|
||||||
mainCommand.AddCommand(commandCheck)
|
|
||||||
mainCommand.AddCommand(commandFormat)
|
|
||||||
mainCommand.AddCommand(commandVersion)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in a new issue