|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/seashell/drago/drago/structs" |
| 9 | + cli "github.com/seashell/drago/pkg/cli" |
| 10 | + "github.com/spf13/pflag" |
| 11 | +) |
| 12 | + |
| 13 | +// ACLPolicyApplyCommand : |
| 14 | +type ACLPolicyApplyCommand struct { |
| 15 | + UI cli.UI |
| 16 | + Command |
| 17 | + |
| 18 | + // Parsed flags |
| 19 | + description string |
| 20 | +} |
| 21 | + |
| 22 | +func (c *ACLPolicyApplyCommand) FlagSet() *pflag.FlagSet { |
| 23 | + |
| 24 | + flags := c.Command.FlagSet(c.Name()) |
| 25 | + flags.Usage = func() { c.UI.Output("\n" + c.Help() + "\n") } |
| 26 | + |
| 27 | + return flags |
| 28 | +} |
| 29 | + |
| 30 | +// Name : |
| 31 | +func (c *ACLPolicyApplyCommand) Name() string { |
| 32 | + return "acl policy apply" |
| 33 | +} |
| 34 | + |
| 35 | +// Synopsis : |
| 36 | +func (c *ACLPolicyApplyCommand) Synopsis() string { |
| 37 | + return "Apply ACL policy" |
| 38 | +} |
| 39 | + |
| 40 | +// Run : |
| 41 | +func (c *ACLPolicyApplyCommand) Run(ctx context.Context, args []string) int { |
| 42 | + |
| 43 | + flags := c.FlagSet() |
| 44 | + |
| 45 | + if err := flags.Parse(args); err != nil { |
| 46 | + return 1 |
| 47 | + } |
| 48 | + |
| 49 | + args = flags.Args() |
| 50 | + if len(args) != 1 { |
| 51 | + c.UI.Error("This command takes one argument: <name>") |
| 52 | + c.UI.Error(`For additional help, try 'drago acl policy apply --help'`) |
| 53 | + return 1 |
| 54 | + } |
| 55 | + |
| 56 | + name := args[0] |
| 57 | + |
| 58 | + // Get the HTTP client |
| 59 | + api, err := c.Command.APIClient() |
| 60 | + if err != nil { |
| 61 | + c.UI.Error(fmt.Sprintf("Error setting up API client: %s", err)) |
| 62 | + return 1 |
| 63 | + } |
| 64 | + |
| 65 | + p := &structs.ACLPolicy{ |
| 66 | + Name: name, |
| 67 | + Description: c.description, |
| 68 | + } |
| 69 | + if _, err := api.ACLPolicies().Upsert(p); err != nil { |
| 70 | + c.UI.Error(fmt.Sprintf("Error applying ACL policy: %s", err)) |
| 71 | + return 1 |
| 72 | + } |
| 73 | + |
| 74 | + return 0 |
| 75 | +} |
| 76 | + |
| 77 | +// Help : |
| 78 | +func (c *ACLPolicyApplyCommand) Help() string { |
| 79 | + h := ` |
| 80 | +Usage: drago acl policy apply <name> [options] |
| 81 | +
|
| 82 | + Create or update an ACL policy. |
| 83 | +
|
| 84 | +General Options: |
| 85 | +` + GlobalOptions() + ` |
| 86 | +
|
| 87 | +ACL Policy Apply Options: |
| 88 | +
|
| 89 | + --description=<description> |
| 90 | + Sets the description for the ACL policy. |
| 91 | +
|
| 92 | +` |
| 93 | + return strings.TrimSpace(h) |
| 94 | +} |
0 commit comments