Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(strm-1433): implement invite users command #98

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/spf13/cobra v1.3.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.10.0
github.com/strmprivacy/api-definitions-go/v2 v2.43.1
github.com/strmprivacy/api-definitions-go/v2 v2.45.1-0.20220721115252-0709c7abd785
bobvandenhoogen marked this conversation as resolved.
Show resolved Hide resolved
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.46.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/strmprivacy/api-definitions-go/v2 v2.43.1 h1:saiY1lAElmviKOv8e0IjQGpoitypMFHqglWjp1Za3DU=
github.com/strmprivacy/api-definitions-go/v2 v2.43.1/go.mod h1:3uFjMuBEQSzrRQzaKEgIrLbBWRdya9DTYCQZqyS7nEw=
github.com/strmprivacy/api-definitions-go/v2 v2.45.1-0.20220721115252-0709c7abd785 h1:K6COipiaaTlNzvnIMLWT8dmAoUDXL4ZoRYw9zlwq1Kg=
github.com/strmprivacy/api-definitions-go/v2 v2.45.1-0.20220721115252-0709c7abd785/go.mod h1:3uFjMuBEQSzrRQzaKEgIrLbBWRdya9DTYCQZqyS7nEw=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
Expand Down
3 changes: 3 additions & 0 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strmprivacy/strm/pkg/entity/kafka_exporter"
"strmprivacy/strm/pkg/entity/kafka_user"
"strmprivacy/strm/pkg/entity/key_stream"
"strmprivacy/strm/pkg/entity/organization"
"strmprivacy/strm/pkg/entity/project"
"strmprivacy/strm/pkg/entity/schema"
"strmprivacy/strm/pkg/entity/schema_code"
Expand Down Expand Up @@ -47,6 +48,7 @@ func SetupVerbs(rootCmd *cobra.Command) {
rootCmd.AddCommand(cmd.ContextCommand)
rootCmd.AddCommand(cmd.ActivateCmd)
rootCmd.AddCommand(cmd.ArchiveCmd)
rootCmd.AddCommand(cmd.InviteCmd)
}

func SetupServiceClients(accessToken *string) {
Expand All @@ -67,6 +69,7 @@ func SetupServiceClients(accessToken *string) {
installation.SetupClient(clientConnection, ctx)
account.SetupClient(clientConnection, ctx)
project.SetupClient(clientConnection, ctx)
organization.SetupClient(clientConnection, ctx)
}

func ConfigPath() string {
Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/invite.go
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())
}
1 change: 1 addition & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const CreateCommandName = "create"
const DeleteCommandName = "delete"
const ActivateCommandName = "activate"
const ArchiveCommandName = "archive"
const InviteCommandName = "invite"

const RecursiveFlagName = "recursive"
const RecursiveFlagUsage = "Retrieve entities and their dependents"
Expand Down
33 changes: 33 additions & 0 deletions pkg/entity/organization/cmd.go
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
}
64 changes: 64 additions & 0 deletions pkg/entity/organization/organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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,
}
_, err := client.InviteUsers(apiContext, req)
common.CliExit(err)

fmt.Println(fmt.Sprintf("Invited %d users to your organization", len(invites)))
}

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)
}