roles.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package user
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "github.com/gravitl/netmaker/cli/cmd/commons"
  7. "github.com/gravitl/netmaker/cli/functions"
  8. "github.com/guumaster/tablewriter"
  9. "github.com/spf13/cobra"
  10. )
  11. var userRoleCmd = &cobra.Command{
  12. Use: "role",
  13. Args: cobra.NoArgs,
  14. Short: "Manage User Roles",
  15. Long: `Manage User Roles`,
  16. }
  17. // List Roles
  18. var (
  19. platformRoles bool
  20. )
  21. var userRoleListCmd = &cobra.Command{
  22. Use: "list",
  23. Args: cobra.NoArgs,
  24. Short: "List all user roles",
  25. Long: `List all user roles`,
  26. Run: func(cmd *cobra.Command, args []string) {
  27. data := functions.ListUserRoles()
  28. switch commons.OutputFormat {
  29. case commons.JsonOutput:
  30. functions.PrettyPrint(data)
  31. default:
  32. table := tablewriter.NewWriter(os.Stdout)
  33. h := []string{"ID", "Default", "Dashboard Access", "Full Access"}
  34. if !platformRoles {
  35. h = append(h, "Network")
  36. }
  37. table.SetHeader(h)
  38. for _, d := range data {
  39. e := []string{d.ID.String(), strconv.FormatBool(d.Default), strconv.FormatBool(d.DenyDashboardAccess), strconv.FormatBool(d.FullAccess)}
  40. table.Append(e)
  41. }
  42. table.Render()
  43. }
  44. },
  45. }
  46. var userRoleCreateCmd = &cobra.Command{
  47. Use: "create",
  48. Args: cobra.NoArgs,
  49. Short: "create user role",
  50. Long: `create user role`,
  51. Run: func(cmd *cobra.Command, args []string) {
  52. fmt.Println("CLI doesn't support creation of roles currently. Visit the dashboard to create one or refer to our api documentation https://docs.v2.netmaker.io/reference")
  53. },
  54. }
  55. var userRoleDeleteCmd = &cobra.Command{
  56. Use: "delete [roleID]",
  57. Args: cobra.ExactArgs(1),
  58. Short: "delete user role",
  59. Long: `delete user role`,
  60. Run: func(cmd *cobra.Command, args []string) {
  61. resp := functions.DeleteUserRole(args[0])
  62. if resp != nil {
  63. fmt.Println(resp.Message)
  64. }
  65. },
  66. }
  67. var userRoleGetCmd = &cobra.Command{
  68. Use: "get [roleID]",
  69. Args: cobra.ExactArgs(1),
  70. Short: "get user role",
  71. Long: `get user role`,
  72. Run: func(cmd *cobra.Command, args []string) {
  73. d := functions.GetUserRole(args[0])
  74. switch commons.OutputFormat {
  75. case commons.JsonOutput:
  76. functions.PrettyPrint(d)
  77. default:
  78. table := tablewriter.NewWriter(os.Stdout)
  79. h := []string{"ID", "Default Role", "Dashboard Access", "Full Access"}
  80. table.SetHeader(h)
  81. e := []string{d.ID.String(), strconv.FormatBool(d.Default), strconv.FormatBool(!d.DenyDashboardAccess), strconv.FormatBool(d.FullAccess)}
  82. table.Append(e)
  83. table.Render()
  84. }
  85. },
  86. }
  87. func init() {
  88. rootCmd.AddCommand(userRoleCmd)
  89. // list roles cmd
  90. userRoleListCmd.Flags().BoolVar(&platformRoles, "platform-roles", true,
  91. "set to false to list network roles. By default it will only list platform roles")
  92. userRoleCmd.AddCommand(userRoleListCmd)
  93. // create roles cmd
  94. userRoleCmd.AddCommand(userRoleCreateCmd)
  95. // delete role cmd
  96. userRoleCmd.AddCommand(userRoleDeleteCmd)
  97. // Get Role
  98. userRoleCmd.AddCommand(userRoleGetCmd)
  99. }