Skip to content

Commit

Permalink
add net stat and limit cli
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 20, 2022
1 parent 39bf59d commit 0de1566
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cli/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var NetCmd = &cli.Command{
NetReachability,
NetBandwidthCmd,
NetBlockCmd,
NetStatCmd,
NetLimitCmd,
},
}

Expand Down Expand Up @@ -606,3 +608,86 @@ var NetBlockListCmd = &cli.Command{
return nil
},
}

var NetStatCmd = &cli.Command{
Name: "stat",
Usage: "report resource stat for a scope",
ArgsUsage: "scope",
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)

args := cctx.Args().Slice()
if len(args) != 1 {
return xerrors.Errorf("must specify exactly one scope")
}
scope := args[0]

result, err := api.NetStat(ctx, scope)
if err != nil {
return err
}

enc := json.NewEncoder(os.Stdout)
enc.Encode(result)

return nil
},
}

var NetLimitCmd = &cli.Command{
Name: "limit",
Usage: "get or set resource limit for a scope",
ArgsUsage: "scope [limit]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "set",
Usage: "set the limit for a scope",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
args := cctx.Args().Slice()

if cctx.Bool("set") {
if len(args) != 2 {
return xerrors.Errorf("must specify exactly a scope and a limit")
}
scope := args[0]
limitStr := args[1]

var limit atypes.NetLimit
err := json.Unmarshal([]byte(limitStr), &limit)
if err != nil {
return xerrors.Errorf("error decoding limit: %w", err)
}

return api.NetSetLimit(ctx, scope, limit)

} else {
if len(args) != 1 {
return xerrors.Errorf("must specify exactly one scope")
}
scope := args[0]

result, err := api.NetLimit(ctx, scope)
if err != nil {
return err
}

enc := json.NewEncoder(os.Stdout)
enc.Encode(result)
}

return nil
},
}

0 comments on commit 0de1566

Please sign in to comment.