Skip to content

Commit

Permalink
feat: invite user to organization
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Jan 10, 2025
1 parent d44c4b3 commit eebce21
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 21 deletions.
10 changes: 9 additions & 1 deletion cmd/world/forge/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ func sendRequest(ctx context.Context, method, url string, body interface{}) ([]b
if resp.StatusCode == http.StatusUnauthorized {
return nil, eris.New("Unauthorized. Please login again using 'world forge login' command")
}
return nil, eris.Errorf("Unexpected status code: %d", resp.StatusCode)

// parse response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, eris.Wrap(err, "Failed to read response body")
}
// get message from response body
message := gjson.GetBytes(body, "message").String()
return nil, eris.New(message)
}

// Read response body
Expand Down
43 changes: 40 additions & 3 deletions cmd/world/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
// For local development
worldForgeBaseURLLocal = "http://localhost:8081"
worldForgeBaseURLLocal = "http://localhost:8001"

// For production
worldForgeBaseURLProd = "https://forge.world.dev"
Expand All @@ -36,10 +36,38 @@ var BaseCmd = &cobra.Command{
Use: "forge",
Short: "Forge is a tool for managing World Forge projects",
RunE: func(cmd *cobra.Command, _ []string) error {
err := cmd.Help()
if !checkLogin() {
return nil
}

// Get user info
globalConfig, err := globalconfig.GetGlobalConfig()
if err != nil {
return eris.Wrap(err, "Failed to get user")
}

fmt.Println("✨ World Forge Status ✨")
fmt.Println("=====================")
fmt.Println("\n👤 User Information")
fmt.Println("------------------")
fmt.Printf("ID: %s\n", globalConfig.Credential.ID)
fmt.Printf("Name: %s\n", globalConfig.Credential.Name)

// Show organization list
err = showOrganizationList(cmd.Context())
if err != nil {
return eris.Wrap(err, "Failed to show organization list")
}

// Show project list
err = showProjectList(cmd.Context())
if err != nil {
return eris.Wrap(err, "Failed to show help")
return eris.Wrap(err, "Failed to show project list")
}

// add separator
fmt.Println("\n================================================")

return nil
},
}
Expand Down Expand Up @@ -95,6 +123,14 @@ var (
return nil
},
}

inviteUserToOrganizationCmd = &cobra.Command{
Use: "invite",
Short: "Invite a user to an organization",
RunE: func(cmd *cobra.Command, _ []string) error {
return inviteUserToOrganization(cmd.Context())
},
}
)

// Project commands
Expand Down Expand Up @@ -207,6 +243,7 @@ func init() {
// Add organization commands
organizationCmd.AddCommand(createOrganizationCmd)
organizationCmd.AddCommand(switchOrganizationCmd)
organizationCmd.AddCommand(inviteUserToOrganizationCmd)
BaseCmd.AddCommand(organizationCmd)

// Add project commands
Expand Down
59 changes: 50 additions & 9 deletions cmd/world/forge/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,28 @@ type createOrgRequest struct {
}

func showOrganizationList(ctx context.Context) error {
selectedOrg, err := getSelectedOrganization(ctx)
organization, err := getSelectedOrganization(ctx)
if err != nil {
return eris.Wrap(err, "Failed to get organization")
}

orgList, err := getListOfOrganizations(ctx)
organizations, err := getListOfOrganizations(ctx)
if err != nil {
return eris.Wrap(err, "Failed to get organization list")
}

fmt.Println("Your organizations:")
fmt.Println("------------------")
for _, org := range orgList {
if org.ID == selectedOrg.ID {
fmt.Printf("* %s (%s) [SELECTED]\n", org.Name, org.Slug)
} else {
fmt.Printf(" %s (%s)\n", org.Name, org.Slug)
fmt.Println("\n🏢 Organization Information")
fmt.Println("-------------------------")
if organization.Name == "" {
fmt.Println("No organization selected")
} else {
fmt.Println("\nAvailable Organizations:")
for _, org := range organizations {
if org.ID == organization.ID {
fmt.Printf("* %s (%s) [SELECTED]\n", org.Name, org.Slug)
} else {
fmt.Printf(" %s (%s)\n", org.Name, org.Slug)
}
}
}
return nil
Expand Down Expand Up @@ -213,3 +218,39 @@ func createOrganization(ctx context.Context) (organization, error) {

return *org, nil
}

func inviteUserToOrganization(ctx context.Context) error {
// Input user id
fmt.Print("Enter user ID: ")
userID, err := getInput()
if err != nil {
return eris.Wrap(err, "Failed to read user ID")
}

if userID == "" {
return eris.New("User ID cannot be empty")
}

payload := map[string]string{
"invited_user_id": userID,
}

org, err := getSelectedOrganization(ctx)
if err != nil {
return eris.Wrap(err, "Failed to get organization")
}

if org.ID == "" {
printNoSelectedOrganization()
return nil
}

// Send request
_, err = sendRequest(ctx, http.MethodPost, fmt.Sprintf("%s/%s/invite", organizationURL, org.ID), payload)
if err != nil {
return eris.Wrap(err, "Failed to invite user to organization")
}

fmt.Println("User invited to organization")
return nil
}
21 changes: 13 additions & 8 deletions cmd/world/forge/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ func showProjectList(ctx context.Context) error {
return nil
}

selectedProject, err := getSelectedProject(ctx)
project, err := getSelectedProject(ctx)
if err != nil {
return eris.Wrap(err, "Failed to get selected project")
}

fmt.Println("Your projects:")
fmt.Println("--------------")
for _, project := range projects {
if project.ID == selectedProject.ID {
fmt.Printf("* %s (%s) [SELECTED]\n", project.Name, project.Slug)
} else {
fmt.Printf(" %s (%s)\n", project.Name, project.Slug)
fmt.Println("\n📁 Project Information")
fmt.Println("--------------------")
if project.Name == "" {
fmt.Println("No project selected")
} else {
fmt.Println("\nAvailable Projects:")
for _, prj := range projects {
if prj.ID == project.ID {
fmt.Printf("* %s (%s) [SELECTED]\n", prj.Name, prj.Slug)
} else {
fmt.Printf(" %s (%s)\n", prj.Name, prj.Slug)
}
}
}

Expand Down

0 comments on commit eebce21

Please sign in to comment.