/* Copyright © 2026 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package cmd import ( "context" "errors" "codeberg.org/scip/esctl/pkg/cfg" "codeberg.org/scip/esctl/pkg/es" "github.com/urfave/cli/v3" ) func Roles(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "role", Usage: "manage roles", Commands: []*cli.Command{ RoleList(conf), RoleShow(conf), RoleDiff(conf), }, } } func RoleList(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "list", Aliases: []string{"ls"}, Usage: "list roles", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "orphaned", Usage: "include only orphaned roles", Destination: &conf.Failed, Aliases: []string{"o"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { return es.RoleList(conf) }, } } func RoleShow(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "show", Aliases: []string{"sh"}, Usage: "show details about a role", UsageText: "show [options] ", ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(conf, cmd, Crole) }, Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no role specified") } return es.RoleShow(conf, cmd.Args().Get(0)) }, } } func RoleDiff(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "diff", Usage: "show differences between roles and CSV baseline", UsageText: `diff [options] [] CSV format: index_name;role;index_privilege;cluster_privilege;ad_group;space;retention;kibana_privilege;field_privilege`, MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{{ Flags: [][]cli.Flag{ { &cli.BoolFlag{ Name: "not-deployed", Usage: "include only not deployed but defined roles", Destination: &conf.NotDeployed, Aliases: []string{"n"}, }, }, { &cli.BoolFlag{ Name: "undefined", Usage: "include only deployed but undefined roles", Destination: &conf.Undefined, Aliases: []string{"u"}, }, }, { &cli.BoolFlag{ Name: "diff", Usage: "include only differing roles", Destination: &conf.Diff, Aliases: []string{"D"}, }, }, }}, }, Flags: []cli.Flag{ &cli.StringFlag{ Name: "separator", Usage: "CSV field separator", Destination: &conf.Separator, Aliases: []string{"s"}, Value: ",", }, }, Action: func(ctx context.Context, cmd *cli.Command) error { csvfile := cmd.Args().Get(0) if csvfile == "" { return errors.New("no CSV file specified") } return es.RoleDiff(conf, cmd.Args().Get(0), cmd.Args().Get(1)) }, } }