81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"os/signal"
|
||
|
|
"syscall"
|
||
|
|
|
||
|
|
"git.apinb.com/ops/license/internal/app"
|
||
|
|
"git.apinb.com/ops/license/internal/config"
|
||
|
|
"git.apinb.com/ops/license/internal/signing"
|
||
|
|
)
|
||
|
|
|
||
|
|
const usage = `用法:
|
||
|
|
ops-licence serve --config <yaml-file>
|
||
|
|
ops-licence keys issue --root-private <file> --private-out <file> --certificate-out <file>`
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
os.Exit(run(os.Args[1:]))
|
||
|
|
}
|
||
|
|
|
||
|
|
func run(arguments []string) int {
|
||
|
|
if len(arguments) == 3 && arguments[0] == "serve" && arguments[1] == "--config" && arguments[2] != "" {
|
||
|
|
cfg, err := config.LoadFile(arguments[2])
|
||
|
|
if err != nil {
|
||
|
|
fmt.Fprintf(os.Stderr, "加载配置失败: %v\n", err)
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||
|
|
defer stop()
|
||
|
|
if err := app.Run(ctx, cfg); err != nil {
|
||
|
|
fmt.Fprintf(os.Stderr, "服务运行失败: %v\n", err)
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(arguments) != 8 || arguments[0] != "keys" || arguments[1] != "issue" {
|
||
|
|
fmt.Fprintln(os.Stderr, usage)
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
|
||
|
|
values := make(map[string]string, 3)
|
||
|
|
for index := 2; index < len(arguments); index += 2 {
|
||
|
|
name := arguments[index]
|
||
|
|
switch name {
|
||
|
|
case "--root-private", "--private-out", "--certificate-out":
|
||
|
|
default:
|
||
|
|
fmt.Fprintf(os.Stderr, "未知参数: %s\n%s\n", name, usage)
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
if _, exists := values[name]; exists {
|
||
|
|
fmt.Fprintf(os.Stderr, "参数不能重复: %s\n%s\n", name, usage)
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
value := arguments[index+1]
|
||
|
|
if value == "" {
|
||
|
|
fmt.Fprintf(os.Stderr, "参数值不能为空: %s\n%s\n", name, usage)
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
values[name] = value
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, name := range []string{"--root-private", "--private-out", "--certificate-out"} {
|
||
|
|
if _, exists := values[name]; !exists {
|
||
|
|
fmt.Fprintf(os.Stderr, "缺少参数: %s\n%s\n", name, usage)
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if err := signing.IssueKey(
|
||
|
|
values["--root-private"],
|
||
|
|
values["--private-out"],
|
||
|
|
values["--certificate-out"],
|
||
|
|
); err != nil {
|
||
|
|
fmt.Fprintf(os.Stderr, "签发密钥失败: %v\n", err)
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|