1
0
mirror of https://github.com/minio/mc.git synced 2026-01-04 02:44:40 +03:00

Add option to mc admin policy info to save policy JSON to file (#3914)

This commit is contained in:
Aditya Manthramurthy
2022-01-28 16:21:28 -08:00
committed by GitHub
parent 9914c8187e
commit 6d12396b47

View File

@@ -18,6 +18,8 @@
package cmd
import (
"os"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/madmin-go"
@@ -25,18 +27,25 @@ import (
"github.com/minio/pkg/console"
)
var policyInfoFlags = []cli.Flag{
cli.StringFlag{
Name: "policy-file, f",
Usage: "additionally (over-)write policy JSON to given file",
},
}
var adminPolicyInfoCmd = cli.Command{
Name: "info",
Usage: "show info on a policy",
Action: mainAdminPolicyInfo,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: globalFlags,
Flags: append(policyInfoFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET POLICYNAME
{{.HelpName}} TARGET POLICYNAME [OPTIONS...]
POLICYNAME:
Name of the policy on the MinIO server.
@@ -47,6 +56,9 @@ FLAGS:
EXAMPLES:
1. Show information on a given policy.
{{.Prompt}} {{.HelpName}} myminio writeonly
2. Show information on a given policy and write the policy JSON content to /tmp/policy.json.
{{.Prompt}} {{.HelpName}} myminio writeonly --policy-file /tmp/policy.json
`,
}
@@ -93,6 +105,18 @@ func mainAdminPolicyInfo(ctx *cli.Context) error {
pinfo, e := getPolicyInfo(client, policyName)
fatalIf(probe.NewError(e).Trace(args...), "Unable to fetch policy")
policyFile := ctx.String("policy-file")
if policyFile != "" {
f, err := os.Create(policyFile)
if err != nil {
fatalIf(probe.NewError(err).Trace(args...), "Could not open given policy file")
}
_, err = f.Write(pinfo.Policy)
if err != nil {
fatalIf(probe.NewError(err).Trace(args...), "Could not write to given policy file")
}
}
printMsg(userPolicyMessage{
op: "info",
Policy: policyName,