diff --git a/cmd/world/forge/common.go b/cmd/world/forge/common.go index adf3390..8f3d56e 100644 --- a/cmd/world/forge/common.go +++ b/cmd/world/forge/common.go @@ -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 diff --git a/cmd/world/forge/forge.go b/cmd/world/forge/forge.go index 067c96a..5b4421f 100644 --- a/cmd/world/forge/forge.go +++ b/cmd/world/forge/forge.go @@ -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 }, } @@ -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 @@ -218,6 +254,7 @@ func init() { // Add organization commands organizationCmd.AddCommand(createOrganizationCmd) organizationCmd.AddCommand(switchOrganizationCmd) + organizationCmd.AddCommand(inviteUserToOrganizationCmd) BaseCmd.AddCommand(organizationCmd) // Add project commands diff --git a/cmd/world/forge/organization.go b/cmd/world/forge/organization.go index 44417bd..36d5b6b 100644 --- a/cmd/world/forge/organization.go +++ b/cmd/world/forge/organization.go @@ -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 @@ -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 +} diff --git a/cmd/world/forge/project.go b/cmd/world/forge/project.go index 6c9e146..5996f1a 100644 --- a/cmd/world/forge/project.go +++ b/cmd/world/forge/project.go @@ -40,18 +40,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) + } } }