github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/parser/perms.go (about)

     1  package parser
     2  
     3  import (
     4  	docopt "github.com/docopt/docopt-go"
     5  	"github.com/drycc/workflow-cli/cmd"
     6  )
     7  
     8  // Perms routes perms commands to their specific function.
     9  func Perms(argv []string, cmdr cmd.Commander) error {
    10  	usage := `
    11  Valid commands for perms:
    12  
    13  perms:list            list permissions granted on an app
    14  perms:create          create a new permission for a user
    15  perms:delete          delete a permission for a user
    16  
    17  Use 'drycc help perms:[command]' to learn more.
    18  `
    19  
    20  	switch argv[0] {
    21  	case "perms:list":
    22  		return permsList(argv, cmdr)
    23  	case "perms:create":
    24  		return permCreate(argv, cmdr)
    25  	case "perms:delete":
    26  		return permDelete(argv, cmdr)
    27  	default:
    28  		if printHelp(argv, usage) {
    29  			return nil
    30  		}
    31  
    32  		if argv[0] == "perms" {
    33  			argv[0] = "perms:list"
    34  			return permsList(argv, cmdr)
    35  		}
    36  
    37  		PrintUsage(cmdr)
    38  		return nil
    39  	}
    40  }
    41  
    42  func permsList(argv []string, cmdr cmd.Commander) error {
    43  	usage := `
    44  Lists all users with permission to use an app, or lists all users with system
    45  administrator privileges.
    46  
    47  Usage: drycc perms:list [-a --app=<app>|--admin|--admin --limit=<num>]
    48  
    49  Options:
    50    -a --app=<app>
    51      lists all users with permission to <app>. <app> is the uniquely identifiable name
    52      for the application.
    53    --admin
    54      lists all users with system administrator privileges.
    55    -l --limit=<num>
    56      the maximum number of results to display, defaults to config setting`
    57  
    58  	args, err := docopt.ParseArgs(usage, argv, "")
    59  
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	app := safeGetString(args, "--app")
    65  	admin := args["--admin"].(bool)
    66  
    67  	results, err := responseLimit(safeGetString(args, "--limit"))
    68  
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	return cmdr.PermsList(app, admin, results)
    74  }
    75  
    76  func permCreate(argv []string, cmdr cmd.Commander) error {
    77  	usage := `
    78  Gives another user permission to use an app, or gives another user
    79  system administrator privileges.
    80  
    81  Usage: drycc perms:create <username> [-a --app=<app>|--admin]
    82  
    83  Arguments:
    84    <username>
    85      the name of the new user.
    86  
    87  Options:
    88    -a --app=<app>
    89      grants <username> permission to use <app>. <app> is the uniquely identifiable name
    90      for the application.
    91    --admin
    92      grants <username> system administrator privileges.
    93  `
    94  
    95  	args, err := docopt.ParseArgs(usage, argv, "")
    96  
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	app := safeGetString(args, "--app")
   102  	username := args["<username>"].(string)
   103  	admin := args["--admin"].(bool)
   104  
   105  	return cmdr.PermCreate(app, username, admin)
   106  }
   107  
   108  func permDelete(argv []string, cmdr cmd.Commander) error {
   109  	usage := `
   110  Revokes another user's permission to use an app, or revokes another user's system
   111  administrator privileges.
   112  
   113  Usage: drycc perms:delete <username> [-a --app=<app>|--admin]
   114  
   115  Arguments:
   116    <username>
   117      the name of the user.
   118  
   119  Options:
   120    -a --app=<app>
   121      revokes <username> permission to use <app>. <app> is the uniquely identifiable name
   122      for the application.
   123    --admin
   124      revokes <username> system administrator privileges.`
   125  
   126  	args, err := docopt.ParseArgs(usage, argv, "")
   127  
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	app := safeGetString(args, "--app")
   133  	username := args["<username>"].(string)
   134  	admin := args["--admin"].(bool)
   135  
   136  	return cmdr.PermDelete(app, username, admin)
   137  }