-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strm-1433): implement invite users command (#98)
* feat(strm-1433): implement invite users command * feat(strm-1433): handle issues from response * fix: update api defs version Co-authored-by: Bob van den Hoogen <bob@strmprivacy.io>
- Loading branch information
Showing
8 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"strmprivacy/strm/pkg/common" | ||
"strmprivacy/strm/pkg/entity/organization" | ||
) | ||
|
||
var InviteCmd = &cobra.Command{ | ||
Use: common.InviteCommandName, | ||
DisableAutoGenTag: true, | ||
Short: "Invite users to your organization", | ||
} | ||
|
||
func init() { | ||
InviteCmd.AddCommand(organization.InviteUsersCmd()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package organization | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
userEmailsFileFlag = "user-emails-file" | ||
) | ||
|
||
var inviteLongDoc = `Invite one or more users to your organization, by email. | ||
Either provide the emails comma-separated on the command line, or pass a file | ||
with the -f flag containing one email address per line. | ||
### Usage` | ||
|
||
func InviteUsersCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "users [first-email,second-email,...]", | ||
Short: "Invite users to your organization by email", | ||
Long: inviteLongDoc, | ||
DisableAutoGenTag: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
inviteUsers(args, cmd) | ||
}, | ||
Args: cobra.MaximumNArgs(1), | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringP(userEmailsFileFlag, "f", "", "file with users to invite, one email per line") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package organization | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"github.com/strmprivacy/api-definitions-go/v2/api/organizations/v1" | ||
"google.golang.org/grpc" | ||
"io/ioutil" | ||
"strings" | ||
"strmprivacy/strm/pkg/common" | ||
"strmprivacy/strm/pkg/util" | ||
) | ||
|
||
var client organizations.OrganizationsServiceClient | ||
var apiContext context.Context | ||
|
||
func SetupClient(clientConnection *grpc.ClientConn, ctx context.Context) { | ||
apiContext = ctx | ||
client = organizations.NewOrganizationsServiceClient(clientConnection) | ||
} | ||
|
||
func inviteUsers(args []string, cmd *cobra.Command) { | ||
if apiContext == nil { | ||
common.CliExit(errors.New(fmt.Sprint("No login information found. Use: `dstrm auth login` first."))) | ||
} | ||
emails := getEmails(args, cmd) | ||
var invites []*organizations.UserInvite | ||
for _, email := range emails { | ||
invites = append(invites, &organizations.UserInvite{Email: email}) | ||
} | ||
req := &organizations.InviteUsersRequest{ | ||
UserInvites: invites, | ||
} | ||
response, err := client.InviteUsers(apiContext, req) | ||
common.CliExit(err) | ||
handleInviteResponse(response) | ||
} | ||
|
||
func handleInviteResponse(response *organizations.InviteUsersResponse) { | ||
fmt.Println(fmt.Sprintf("Invited %d users to your organization.", response.InviteCount)) | ||
if len(response.Issues) > 0 { | ||
fmt.Println(fmt.Sprintf("There were %d invites with issues:\n", len(response.Issues))) | ||
inviteIssuesPrinter{}.Print(response.Issues) | ||
} | ||
} | ||
|
||
func getEmails(args []string, cmd *cobra.Command) []string { | ||
emailsFile := util.GetStringAndErr(cmd.Flags(), userEmailsFileFlag) | ||
var emails []string | ||
|
||
if len(args) == 0 && emailsFile == "" { | ||
common.CliExit(errors.New(fmt.Sprint("Either provide comma-separated emails on the command line, or a file containing emails."))) | ||
} else if len(args) > 0 { | ||
emails = strings.Split(args[0], ",") | ||
} else { | ||
emails = read(emailsFile) | ||
} | ||
return emails | ||
} | ||
|
||
func read(emailsFile string) []string { | ||
buf, err := ioutil.ReadFile(emailsFile) | ||
common.CliExit(err) | ||
splitFn := func(c rune) bool { | ||
return c == '\n' | ||
} | ||
// FieldsFunc is used instead of split to filter out any (trailing) empty lines | ||
return strings.FieldsFunc(string(buf), splitFn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package organization | ||
|
||
import ( | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/strmprivacy/api-definitions-go/v2/api/organizations/v1" | ||
"strmprivacy/strm/pkg/util" | ||
) | ||
|
||
type inviteIssuesPrinter struct{} | ||
|
||
func (p inviteIssuesPrinter) Print(data interface{}) { | ||
issues, _ := (data).([]*organizations.InviteUsersResponse_UserInviteIssue) | ||
rows := make([]table.Row, 0, len(issues)) | ||
|
||
for _, issue := range issues { | ||
row := table.Row{ | ||
issue.Invite.Email, | ||
issue.Message, | ||
} | ||
rows = append(rows, row) | ||
} | ||
|
||
header := table.Row{ | ||
"Email", | ||
"Issue", | ||
} | ||
|
||
util.RenderTable(header, rows) | ||
} |