github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/cli/backup/folder_permissions.go (about)

     1  package backup
     2  
     3  import (
     4  	"context"
     5  	"github.com/bep/simplecobra"
     6  	"github.com/esnet/gdg/cli/support"
     7  	"github.com/esnet/gdg/internal/config"
     8  	"github.com/jedib0t/go-pretty/v6/table"
     9  	"github.com/spf13/cobra"
    10  	"log/slog"
    11  )
    12  
    13  func newFolderPermissionCommand() simplecobra.Commander {
    14  	description := "Folder Permissions"
    15  	return &support.SimpleCommand{
    16  		NameP: "permission",
    17  		Short: description,
    18  		Long:  description,
    19  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    20  			cmd.Aliases = []string{"p", "permissions"}
    21  		},
    22  		CommandsList: []simplecobra.Commander{
    23  			newFolderPermissionListCmd(),
    24  			newFolderPermissionUploadCmd(),
    25  			newFolderPermissionDownloadCmd(),
    26  		},
    27  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    28  			return cd.CobraCommand.Help()
    29  		},
    30  	}
    31  }
    32  
    33  func newFolderPermissionListCmd() simplecobra.Commander {
    34  	description := "list Folder Permissions"
    35  	return &support.SimpleCommand{
    36  		NameP: "list",
    37  		Short: description,
    38  		Long:  description,
    39  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    40  			cmd.Aliases = []string{"l"}
    41  		},
    42  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    43  			rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
    44  
    45  			slog.Info("Listing Folders for context", "context", config.Config().GetGDGConfig().GetContext())
    46  			rootCmd.TableObj.AppendHeader(table.Row{"folder ID", "folderUid", "folder Name", "UserID", "Team Name", "Role", "Permission Name"}, rowConfigAutoMerge)
    47  			folders := rootCmd.GrafanaSvc().ListFolderPermissions(getFolderFilter())
    48  
    49  			if len(folders) == 0 {
    50  				slog.Info("No folders found")
    51  				return nil
    52  			}
    53  			for key, value := range folders {
    54  				rootCmd.TableObj.AppendRow(table.Row{key.ID, key.UID, key.Title})
    55  				for _, entry := range value {
    56  					rootCmd.TableObj.AppendRow(table.Row{"", "", "    PERMISSION--->", entry.UserLogin, entry.Team, entry.Role, entry.PermissionName}, rowConfigAutoMerge)
    57  				}
    58  			}
    59  			rootCmd.Render(cd.CobraCommand, folders)
    60  			return nil
    61  		},
    62  	}
    63  }
    64  func newFolderPermissionDownloadCmd() simplecobra.Commander {
    65  	description := "download Folders Permissions"
    66  	return &support.SimpleCommand{
    67  		NameP: "download",
    68  		Short: description,
    69  		Long:  description,
    70  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    71  			cmd.Aliases = []string{"d"}
    72  		},
    73  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    74  			slog.Info("Downloading Folder Permissions for context", "context", config.Config().GetGDGConfig().GetContext())
    75  			rootCmd.TableObj.AppendHeader(table.Row{"filename"})
    76  			folders := rootCmd.GrafanaSvc().DownloadFolderPermissions(getFolderFilter())
    77  			slog.Info("Downloading folder permissions")
    78  
    79  			if len(folders) == 0 {
    80  				slog.Info("No folders found")
    81  				return nil
    82  			}
    83  			for _, folder := range folders {
    84  				rootCmd.TableObj.AppendRow(table.Row{folder})
    85  			}
    86  			rootCmd.Render(cd.CobraCommand, folders)
    87  			return nil
    88  		},
    89  	}
    90  }
    91  func newFolderPermissionUploadCmd() simplecobra.Commander {
    92  	description := "upload Folders Permissions"
    93  	return &support.SimpleCommand{
    94  		NameP: "upload",
    95  		Short: description,
    96  		Long:  description,
    97  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    98  			cmd.Aliases = []string{"u"}
    99  		},
   100  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
   101  			slog.Info("Uploading folder permissions")
   102  			rootCmd.TableObj.AppendHeader(table.Row{"file name"})
   103  			folders := rootCmd.GrafanaSvc().UploadFolderPermissions(getFolderFilter())
   104  
   105  			if len(folders) == 0 {
   106  				slog.Info("No folders found")
   107  				return nil
   108  			}
   109  			for _, folder := range folders {
   110  				rootCmd.TableObj.AppendRow(table.Row{folder})
   111  			}
   112  			rootCmd.Render(cd.CobraCommand, folders)
   113  			return nil
   114  		},
   115  	}
   116  }