mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-04 16:26:16 +00:00
Merge pull request #1521 from rhythmbhiwani/show-org-name-in-init
Show organization name in `infisical init` command
This commit is contained in:
@@ -145,6 +145,25 @@ func CallLogin2V2(httpClient *resty.Client, request GetLoginTwoV2Request) (GetLo
|
|||||||
return loginTwoV2Response, nil
|
return loginTwoV2Response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CallGetAllOrganizations(httpClient *resty.Client) (GetOrganizationsResponse, error) {
|
||||||
|
var orgResponse GetOrganizationsResponse
|
||||||
|
response, err := httpClient.
|
||||||
|
R().
|
||||||
|
SetResult(&orgResponse).
|
||||||
|
SetHeader("User-Agent", USER_AGENT).
|
||||||
|
Get(fmt.Sprintf("%v/v1/organization", config.INFISICAL_URL))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return GetOrganizationsResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.IsError() {
|
||||||
|
return GetOrganizationsResponse{}, fmt.Errorf("CallGetAllOrganizations: Unsuccessful response: [response=%v]", response)
|
||||||
|
}
|
||||||
|
|
||||||
|
return orgResponse, nil
|
||||||
|
}
|
||||||
|
|
||||||
func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesResponse, error) {
|
func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesResponse, error) {
|
||||||
var workSpacesResponse GetWorkSpacesResponse
|
var workSpacesResponse GetWorkSpacesResponse
|
||||||
response, err := httpClient.
|
response, err := httpClient.
|
||||||
|
|||||||
@@ -120,14 +120,21 @@ type PullSecretsByInfisicalTokenResponse struct {
|
|||||||
|
|
||||||
type GetWorkSpacesResponse struct {
|
type GetWorkSpacesResponse struct {
|
||||||
Workspaces []struct {
|
Workspaces []struct {
|
||||||
ID string `json:"_id"`
|
ID string `json:"_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Plan string `json:"plan,omitempty"`
|
Plan string `json:"plan,omitempty"`
|
||||||
V int `json:"__v"`
|
V int `json:"__v"`
|
||||||
Organization string `json:"organization,omitempty"`
|
OrganizationId string `json:"orgId"`
|
||||||
} `json:"workspaces"`
|
} `json:"workspaces"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetOrganizationsResponse struct {
|
||||||
|
Organizations []struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"organizations"`
|
||||||
|
}
|
||||||
|
|
||||||
type Secret struct {
|
type Secret struct {
|
||||||
SecretKeyCiphertext string `json:"secretKeyCiphertext,omitempty"`
|
SecretKeyCiphertext string `json:"secretKeyCiphertext,omitempty"`
|
||||||
SecretKeyIV string `json:"secretKeyIV,omitempty"`
|
SecretKeyIV string `json:"secretKeyIV,omitempty"`
|
||||||
@@ -505,5 +512,5 @@ type GetRawSecretsV3Response struct {
|
|||||||
SecretComment string `json:"secretComment"`
|
SecretComment string `json:"secretComment"`
|
||||||
} `json:"secrets"`
|
} `json:"secrets"`
|
||||||
Imports []any `json:"imports"`
|
Imports []any `json:"imports"`
|
||||||
ETag string
|
ETag string
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-15
@@ -5,7 +5,6 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/Infisical/infisical-merge/packages/api"
|
"github.com/Infisical/infisical-merge/packages/api"
|
||||||
"github.com/Infisical/infisical-merge/packages/models"
|
"github.com/Infisical/infisical-merge/packages/models"
|
||||||
@@ -52,25 +51,19 @@ var initCmd = &cobra.Command{
|
|||||||
|
|
||||||
httpClient := resty.New()
|
httpClient := resty.New()
|
||||||
httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken)
|
httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken)
|
||||||
workspaceResponse, err := api.CallGetAllWorkSpacesUserBelongsTo(httpClient)
|
|
||||||
|
organizationResponse, err := api.CallGetAllOrganizations(httpClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.HandleError(err, "Unable to pull projects that belong to you")
|
util.HandleError(err, "Unable to pull organizations that belong to you")
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaces := workspaceResponse.Workspaces
|
organizations := organizationResponse.Organizations
|
||||||
if len(workspaces) == 0 {
|
|
||||||
message := fmt.Sprintf("You don't have any projects created in Infisical. You must first create a project at %s", util.INFISICAL_TOKEN_NAME)
|
|
||||||
util.PrintErrorMessageAndExit(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
var workspaceNames []string
|
organizationNames := util.GetOrganizationsNameList(organizationResponse)
|
||||||
for _, workspace := range workspaces {
|
|
||||||
workspaceNames = append(workspaceNames, workspace.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt := promptui.Select{
|
prompt := promptui.Select{
|
||||||
Label: "Which of your Infisical projects would you like to connect this project to?",
|
Label: "Which Infisical organization would you like to select a project from?",
|
||||||
Items: workspaceNames,
|
Items: organizationNames,
|
||||||
Size: 7,
|
Size: 7,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +72,27 @@ var initCmd = &cobra.Command{
|
|||||||
util.HandleError(err)
|
util.HandleError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writeWorkspaceFile(workspaces[index])
|
selectedOrganization := organizations[index]
|
||||||
|
|
||||||
|
workspaceResponse, err := api.CallGetAllWorkSpacesUserBelongsTo(httpClient)
|
||||||
|
if err != nil {
|
||||||
|
util.HandleError(err, "Unable to pull projects that belong to you")
|
||||||
|
}
|
||||||
|
|
||||||
|
filteredWorkspaces, workspaceNames := util.GetWorkspacesInOrganization(workspaceResponse, selectedOrganization.ID)
|
||||||
|
|
||||||
|
prompt = promptui.Select{
|
||||||
|
Label: "Which of your Infisical projects would you like to connect this project to?",
|
||||||
|
Items: workspaceNames,
|
||||||
|
Size: 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
index, _, err = prompt.Run()
|
||||||
|
if err != nil {
|
||||||
|
util.HandleError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writeWorkspaceFile(filteredWorkspaces[index])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.HandleError(err)
|
util.HandleError(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,11 @@ type SingleFolder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Workspace struct {
|
type Workspace struct {
|
||||||
ID string `json:"_id"`
|
ID string `json:"_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Plan string `json:"plan,omitempty"`
|
Plan string `json:"plan,omitempty"`
|
||||||
V int `json:"__v"`
|
V int `json:"__v"`
|
||||||
Organization string `json:"organization,omitempty"`
|
OrganizationId string `json:"orgId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkspaceConfigFile struct {
|
type WorkspaceConfigFile struct {
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Infisical/infisical-merge/packages/api"
|
||||||
|
"github.com/Infisical/infisical-merge/packages/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetOrganizationsNameList(organizationResponse api.GetOrganizationsResponse) []string {
|
||||||
|
organizations := organizationResponse.Organizations
|
||||||
|
|
||||||
|
if len(organizations) == 0 {
|
||||||
|
message := fmt.Sprintf("You don't have any organization created in Infisical. You must first create a organization at %s", INFISICAL_DEFAULT_URL)
|
||||||
|
PrintErrorMessageAndExit(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
var organizationNames []string
|
||||||
|
for _, workspace := range organizations {
|
||||||
|
organizationNames = append(organizationNames, workspace.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return organizationNames
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWorkspacesInOrganization(workspaceResponse api.GetWorkSpacesResponse, orgId string) ([]models.Workspace, []string) {
|
||||||
|
workspaces := workspaceResponse.Workspaces
|
||||||
|
|
||||||
|
var filteredWorkspaces []models.Workspace
|
||||||
|
var workspaceNames []string
|
||||||
|
|
||||||
|
for _, workspace := range workspaces {
|
||||||
|
if workspace.OrganizationId == orgId {
|
||||||
|
filteredWorkspaces = append(filteredWorkspaces, workspace)
|
||||||
|
workspaceNames = append(workspaceNames, workspace.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(filteredWorkspaces) == 0 {
|
||||||
|
message := fmt.Sprintf("You don't have any projects created in Infisical organization. You must first create a project at %s", INFISICAL_DEFAULT_URL)
|
||||||
|
PrintErrorMessageAndExit(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredWorkspaces, workspaceNames
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user