From 0de1566eaff66bbd4574ae9806968dde6c676a5f Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 18 Jan 2022 16:28:20 +0200 Subject: [PATCH] add net stat and limit cli --- cli/net.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/cli/net.go b/cli/net.go index fdd0a13d656..104b4b40ebd 100644 --- a/cli/net.go +++ b/cli/net.go @@ -36,6 +36,8 @@ var NetCmd = &cli.Command{ NetReachability, NetBandwidthCmd, NetBlockCmd, + NetStatCmd, + NetLimitCmd, }, } @@ -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 + }, +}