|
| 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