Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions cli/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,95 +20,110 @@ var codesCmds = &cli.Command{

var removeCandidateCodeCmd = &cli.Command{
Name: "rm",
Usage: "remove code info",
Usage: "Remove code info",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "code",
Usage: "code id",
Usage: "Code ID (required)",
Value: "",
},
},
Action: func(cctx *cli.Context) error {
code := cctx.String("code")
if code == "" {
return fmt.Errorf("the --code flag is required")
}

ctx := ReqContext(cctx)
schedulerAPI, closer, err := GetSchedulerAPI(cctx, "")
if err != nil {
return err
return fmt.Errorf("failed to get scheduler API: %w", err)
}
defer closer()

return schedulerAPI.RemoveCandidateCode(ctx, code)
if err := schedulerAPI.RemoveCandidateCode(ctx, code); err != nil {
return fmt.Errorf("failed to remove candidate code: %w", err)
}

fmt.Println("Candidate code removed successfully")
return nil
},
}

var resetCandidateCodeCmd = &cli.Command{
Name: "reset",
Usage: "reset code info",
Usage: "Reset code info",
Flags: []cli.Flag{
nodeIDFlag,
&cli.StringFlag{
Name: "code",
Usage: "code id",
Usage: "Code ID (required)",
Value: "",
},
},
Action: func(cctx *cli.Context) error {
nodeID := cctx.String("node-id")
code := cctx.String("code")

if nodeID == "" {
return fmt.Errorf("the --node-id flag is required")
}
if code == "" {
return fmt.Errorf("the --code flag is required")
}

ctx := ReqContext(cctx)
schedulerAPI, closer, err := GetSchedulerAPI(cctx, "")
if err != nil {
return err
return fmt.Errorf("failed to get scheduler API: %w", err)
}
defer closer()

return schedulerAPI.ResetCandidateCode(ctx, nodeID, code)
if err := schedulerAPI.ResetCandidateCode(ctx, nodeID, code); err != nil {
return fmt.Errorf("failed to reset candidate code: %w", err)
}

fmt.Println("Candidate code reset successfully")
return nil
},
}

var generateCandidateCodeCmd = &cli.Command{
Name: "new",
Usage: "generate code",
Usage: "Generate codes",
Flags: []cli.Flag{
// nodeTypeFlag,
&cli.Int64Flag{
Name: "count",
Usage: "code count",
Usage: "Number of codes to generate (must be greater than 0)",
Value: 0,
},
&cli.BoolFlag{
Name: "test",
Usage: "is test",
Usage: "Generate test codes",
Value: false,
},
},
Action: func(cctx *cli.Context) error {
// nodeType := cctx.Int("node-type")
count := cctx.Int("count")
isTest := cctx.Bool("test")

if count <= 0 {
return fmt.Errorf("the --count flag must be greater than 0")
}

ctx := ReqContext(cctx)
schedulerAPI, closer, err := GetSchedulerAPI(cctx, "")
if err != nil {
return err
return fmt.Errorf("failed to get scheduler API: %w", err)
}
defer closer()

// if nodeType != int(types.NodeCandidate) && nodeType != int(types.NodeValidator) {
// return nil
// }

if count <= 0 {
return nil
}

list, err := schedulerAPI.GenerateCandidateCodes(ctx, count, types.NodeCandidate, isTest)
if err != nil {
return err
return fmt.Errorf("failed to generate candidate codes: %w", err)
}

fmt.Println("Generated codes:")
for _, code := range list {
fmt.Println(code)
}
Expand All @@ -119,33 +134,38 @@ var generateCandidateCodeCmd = &cli.Command{

var loadCandidateCodeCmd = &cli.Command{
Name: "get",
Usage: "load candidate code info",
Usage: "Load candidate code info",
Flags: []cli.Flag{
nodeIDFlag,
&cli.StringFlag{
Name: "code",
Usage: "code id",
Usage: "Code ID (optional)",
Value: "",
},
},
Action: func(cctx *cli.Context) error {
nodeID := cctx.String("node-id")
code := cctx.String("code")

if nodeID == "" {
return fmt.Errorf("the --node-id flag is required")
}

ctx := ReqContext(cctx)
schedulerAPI, closer, err := GetSchedulerAPI(cctx, "")
if err != nil {
return err
return fmt.Errorf("failed to get scheduler API: %w", err)
}
defer closer()

infos, err := schedulerAPI.GetCandidateCodeInfos(ctx, nodeID, code)
if err != nil {
return err
return fmt.Errorf("failed to load candidate code info: %w", err)
}

fmt.Println("Candidate code infos:")
for _, info := range infos {
fmt.Printf("code:%s node:%s type:%s\n", info.Code, info.NodeID, info.NodeType.String())
fmt.Printf("Code: %s, Node: %s, Type: %s\n", info.Code, info.NodeID, info.NodeType.String())
}

return nil
Expand Down