github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/users/contract.go (about)

     1  package users
     2  
     3  import (
     4  	"github.com/Sirupsen/logrus"
     5  	"github.com/daticahealth/cli/commands/invites"
     6  	"github.com/daticahealth/cli/config"
     7  	"github.com/daticahealth/cli/lib/auth"
     8  	"github.com/daticahealth/cli/lib/prompts"
     9  	"github.com/daticahealth/cli/models"
    10  	"github.com/jault3/mow.cli"
    11  )
    12  
    13  // Cmd is the contract between the user and the CLI. This specifies the command
    14  // name, arguments, and required/optional arguments and flags for the command.
    15  var Cmd = models.Command{
    16  	Name:      "users",
    17  	ShortHelp: "Manage users who have access to the given organization",
    18  	LongHelp: "The <code>users</code> command allows you to manage who has access to your environment through the organization that owns the environment. " +
    19  		"The users command can not be run directly but has three subcommands.",
    20  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    21  		return func(cmd *cli.Cmd) {
    22  			cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings))
    23  			cmd.CommandLong(RmSubCmd.Name, RmSubCmd.ShortHelp, RmSubCmd.LongHelp, RmSubCmd.CmdFunc(settings))
    24  		}
    25  	},
    26  }
    27  
    28  var ListSubCmd = models.Command{
    29  	Name:      "list",
    30  	ShortHelp: "List all users who have access to the given organization",
    31  	LongHelp: "<code>users list</code> shows every user that belongs to your environment's organization. " +
    32  		"Users who belong to your environment's organization may access to your environment's services and data depending on their role in the organization. " +
    33  		"Here is a sample command\n\n" +
    34  		"<pre>\ndatica -E \"<your_env_name>\" users list\n</pre>",
    35  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    36  		return func(subCmd *cli.Cmd) {
    37  			subCmd.Action = func() {
    38  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    39  					logrus.Fatal(err.Error())
    40  				}
    41  				if err := config.CheckRequiredAssociation(settings); err != nil {
    42  					logrus.Fatal(err.Error())
    43  				}
    44  				err := CmdList(settings.UsersID, New(settings), invites.New(settings))
    45  				if err != nil {
    46  					logrus.Fatal(err.Error())
    47  				}
    48  			}
    49  		}
    50  	},
    51  }
    52  
    53  var RmSubCmd = models.Command{
    54  	Name:      "rm",
    55  	ShortHelp: "Revoke access to the given organization for the given user",
    56  	LongHelp: "<code>users rm</code> revokes a users access to your environment's organization. " +
    57  		"Revoking a user's access to your environment's organization will revoke their access to your organization's environments. " +
    58  		"Here is a sample command\n\n" +
    59  		"<pre>\ndatica -E \"<your_env_name>\" users rm user@example.com\n</pre>",
    60  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    61  		return func(subCmd *cli.Cmd) {
    62  			email := subCmd.StringArg("EMAIL", "", "The email address of the user to revoke access from for the given organization")
    63  			subCmd.Action = func() {
    64  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    65  					logrus.Fatal(err.Error())
    66  				}
    67  				if err := config.CheckRequiredAssociation(settings); err != nil {
    68  					logrus.Fatal(err.Error())
    69  				}
    70  				err := CmdRm(*email, New(settings))
    71  				if err != nil {
    72  					logrus.Fatal(err.Error())
    73  				}
    74  			}
    75  			subCmd.Spec = "EMAIL"
    76  		}
    77  	},
    78  }
    79  
    80  // IUsers
    81  type IUsers interface {
    82  	List() (*[]models.OrgUser, error)
    83  	Rm(usersID string) error
    84  }
    85  
    86  // SUsers is a concrete implementation of IUsers
    87  type SUsers struct {
    88  	Settings *models.Settings
    89  }
    90  
    91  // New generates a new instance of IUsers
    92  func New(settings *models.Settings) IUsers {
    93  	return &SUsers{
    94  		Settings: settings,
    95  	}
    96  }