[#989] adm: Read and parse chains from file

* Slightly fix the approach to read encoded chain from file
  in frostfs-adm.

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2024-02-16 12:59:48 +03:00 committed by Evgenii Stratonikov
parent 9611710e19
commit 613e11c4d2
2 changed files with 13 additions and 25 deletions

View file

@ -22,8 +22,8 @@ const (
chainIDDesc = "Rule chain ID" chainIDDesc = "Rule chain ID"
ruleFlag = "rule" ruleFlag = "rule"
ruleFlagDesc = "Rule chain in text format" ruleFlagDesc = "Rule chain in text format"
ruleJSONFlag = "rule-json" pathFlag = "path"
ruleJSONFlagDesc = "Chain rule in JSON format or path to the file" pathFlagDesc = "path to encoded chain in JSON or binary format"
targetNameFlag = "target-name" targetNameFlag = "target-name"
targetNameDesc = "Resource name in APE resource name format" targetNameDesc = "Resource name in APE resource name format"
targetTypeFlag = "target-type" targetTypeFlag = "target-type"
@ -99,10 +99,10 @@ func initAddRuleChainCmd() {
addRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc) addRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc)
_ = addRuleChainCmd.MarkFlagRequired(chainIDFlag) _ = addRuleChainCmd.MarkFlagRequired(chainIDFlag)
addRuleChainCmd.Flags().String(ruleFlag, "", ruleFlagDesc) addRuleChainCmd.Flags().StringArray(ruleFlag, []string{}, ruleFlagDesc)
addRuleChainCmd.Flags().String(ruleJSONFlag, "", ruleJSONFlagDesc) addRuleChainCmd.Flags().String(pathFlag, "", pathFlagDesc)
addRuleChainCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc) addRuleChainCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc)
addRuleChainCmd.MarkFlagsMutuallyExclusive(ruleFlag, ruleJSONFlag) addRuleChainCmd.MarkFlagsMutuallyExclusive(ruleFlag, pathFlag)
} }
func initRemoveRuleChainCmd() { func initRemoveRuleChainCmd() {

View file

@ -1,9 +1,7 @@
package ape package ape
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"strings" "strings"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
@ -63,29 +61,19 @@ func parseChainID(cmd *cobra.Command) apechain.ID {
func parseChain(cmd *cobra.Command) *apechain.Chain { func parseChain(cmd *cobra.Command) *apechain.Chain {
chain := new(apechain.Chain) chain := new(apechain.Chain)
if ruleStmt, _ := cmd.Flags().GetString(ruleFlag); ruleStmt != "" { if rules, _ := cmd.Flags().GetStringArray(ruleFlag); len(rules) > 0 {
parseErr := parseutil.ParseAPEChain(chain, []string{ruleStmt}) commonCmd.ExitOnErr(cmd, "parser error: %w", parseutil.ParseAPEChain(chain, rules))
commonCmd.ExitOnErr(cmd, "ape chain parser error: %w", parseErr) } else if encPath, _ := cmd.Flags().GetString(pathFlag); encPath != "" {
} else if ruleJSON, _ := cmd.Flags().GetString(ruleJSONFlag); ruleJSON != "" { commonCmd.ExitOnErr(cmd, "decode binary or json error: %w", parseutil.ParseAPEChainBinaryOrJSON(chain, encPath))
var rule []byte
if _, err := os.Stat(ruleJSON); err == nil {
rule, err = os.ReadFile(ruleJSON)
commonCmd.ExitOnErr(cmd, "read file error: %w", err)
} else {
rule = []byte(ruleJSON)
if !json.Valid(rule) {
commonCmd.ExitOnErr(cmd, "read raw rule error: %w",
fmt.Errorf("invalid JSON"))
}
}
err := chain.DecodeBytes(rule)
commonCmd.ExitOnErr(cmd, "chain decode error: %w", err)
} else { } else {
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("rule is not passed")) commonCmd.ExitOnErr(cmd, "parser error: %w", fmt.Errorf("rule is not passed"))
} }
chain.ID = parseChainID(cmd) chain.ID = parseChainID(cmd)
cmd.Println("Parsed chain:")
parseutil.PrintHumanReadableAPEChain(cmd, chain)
return chain return chain
} }