github.com/naphatkrit/deis@v1.12.3/client/cmd/perms.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/deis/deis/client/controller/client"
     7  	"github.com/deis/deis/client/controller/models/perms"
     8  	"github.com/deis/deis/client/pkg/git"
     9  )
    10  
    11  // PermsList prints which users have permissions.
    12  func PermsList(appID string, admin bool, results int) error {
    13  	c, appID, err := permsLoad(appID, admin)
    14  
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	var users []string
    20  	var count int
    21  
    22  	if admin {
    23  		if results == defaultLimit {
    24  			results = c.ResponseLimit
    25  		}
    26  		users, count, err = perms.ListAdmins(c, results)
    27  	} else {
    28  		users, err = perms.List(c, appID)
    29  	}
    30  
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	if admin {
    36  		fmt.Printf("=== Administrators%s", limitCount(len(users), count))
    37  	} else {
    38  		fmt.Printf("=== %s's Users\n", appID)
    39  	}
    40  
    41  	for _, user := range users {
    42  		fmt.Println(user)
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  // PermCreate adds a user to an app or makes them an administrator.
    49  func PermCreate(appID string, username string, admin bool) error {
    50  
    51  	c, appID, err := permsLoad(appID, admin)
    52  
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	if admin {
    58  		fmt.Printf("Adding %s to system administrators... ", username)
    59  		err = perms.NewAdmin(c, username)
    60  	} else {
    61  		fmt.Printf("Adding %s to %s collaborators... ", username, appID)
    62  		err = perms.New(c, appID, username)
    63  	}
    64  
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	fmt.Println("done")
    70  
    71  	return nil
    72  }
    73  
    74  // PermDelete removes a user from an app or revokes admin privilages.
    75  func PermDelete(appID string, username string, admin bool) error {
    76  
    77  	c, appID, err := permsLoad(appID, admin)
    78  
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	if admin {
    84  		fmt.Printf("Removing %s from system administrators... ", username)
    85  		err = perms.DeleteAdmin(c, username)
    86  	} else {
    87  		fmt.Printf("Removing %s from %s collaborators... ", username, appID)
    88  		err = perms.Delete(c, appID, username)
    89  	}
    90  
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	fmt.Println("done")
    96  
    97  	return nil
    98  }
    99  
   100  func permsLoad(appID string, admin bool) (*client.Client, string, error) {
   101  	c, err := client.New()
   102  
   103  	if err != nil {
   104  		return nil, "", err
   105  	}
   106  
   107  	if !admin && appID == "" {
   108  		appID, err = git.DetectAppName(c.ControllerURL.Host)
   109  
   110  		if err != nil {
   111  			return nil, "", err
   112  		}
   113  	}
   114  
   115  	return c, appID, err
   116  }