Skip to content

Commit caf1129

Browse files
committed
feat: 添加生成配置Proto文件的命令
1 parent 69c46b4 commit caf1129

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

layout/cmd/cli/config/cmd/proto.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"github.com/TBXark/sphere/contrib/json-gen-proto/gen"
6+
"github.com/TBXark/sphere/layout/internal/config"
7+
"github.com/spf13/cobra"
8+
"log"
9+
"os"
10+
)
11+
12+
// protoCmd represents the config command
13+
var protoCmd = &cobra.Command{
14+
Use: "gen",
15+
Short: "Generate config proto file",
16+
Long: `Generate proto definition file.`,
17+
Run: runConfig,
18+
}
19+
20+
func init() {
21+
rootCmd.AddCommand(protoCmd)
22+
protoCmd.Flags().StringP("output", "o", "config.proto", "output file path")
23+
protoCmd.Flags().StringP("package", "p", "config", "package name")
24+
}
25+
26+
func runProto(cmd *cobra.Command, args []string) {
27+
output := cmd.Flag("output").Value.String()
28+
pkgName := cmd.Flag("package").Value.String()
29+
conf := config.NewEmptyConfig()
30+
bytes, err := json.Marshal(conf)
31+
if err != nil {
32+
log.Fatalf("marshal error: %v", err)
33+
}
34+
protoStr, err := gen.Generate(pkgName, "Config", bytes)
35+
if err != nil {
36+
log.Fatalf("generate proto error: %v", err)
37+
}
38+
err = os.WriteFile(output, []byte(protoStr), 0644)
39+
if err != nil {
40+
log.Fatalf("write proto file error: %v", err)
41+
}
42+
}

0 commit comments

Comments
 (0)