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: attach to available instance #176

Open
wants to merge 2 commits into
base: fix/typos_command_examples_initapp_use_id_flag
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
88 changes: 60 additions & 28 deletions cmd/initapp/initapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,6 @@ func RunInitApp(cmd *cobra.Command, args []string) {
return
}

appName, shouldContinue, err := GetAppName(cmd, args)
if err != nil {
printer.Error(cmd, err)
return
}
//If the operation is canceled
if !shouldContinue {
return
}

appAlternateId := GetAppID(cmd, appName)
if appAlternateId == "" {
return
}

if manifest.ExistsLocal() {
response := prompt.PromptYesNo(cmd, "Config file exists. Do you want to overwrite it?", false)
if !response.Confirmed {
Expand All @@ -124,24 +109,31 @@ func RunInitApp(cmd *cobra.Command, args []string) {
return
}

newApp, err := appService.CreateApp(orgID, *p.ID, appAlternateId, appName)
if err != nil {
if !errors.Is(err, errors.ErrConflict) {
aid, _ := cmd.Flags().GetString("id")

var newApp *app.App
if aid != "" {
exists, err := appService.DoesAppExist(orgID, aid)
if err != nil {
printer.Error(cmd, err)
return
os.Exit(1)
}

existingApp, handleErr := HandleExistingApp(cmd, *appService, orgID, appAlternateId)
if handleErr != nil {
printer.Error(cmd, handleErr)
return
}
if existingApp == nil {
printer.Info("Operation cancelled.")
return
if exists {
newApp, err = HandleExistingApp(cmd, *appService, orgID, aid)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}
}
}

newApp = *existingApp
if newApp == nil {
newApp, err = CreateNewApp(cmd, *appService, orgID, args, p)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}
}

mcl := manifest.Config{
Expand Down Expand Up @@ -398,3 +390,43 @@ func HandleExistingApp(cmd *cobra.Command, appService app.AppService, orgID, app
printer.Info(fmt.Sprintf("Using existing app '%s' (%s)", existingApp.Name, existingApp.AlternateId))
return &existingApp, nil
}

func CreateNewApp(cmd *cobra.Command, appService app.AppService, orgID string, args []string, p projects.Project) (*app.App, error) {

appName, shouldContinue, err := GetAppName(cmd, args)
if err != nil {
return nil, err
}
//If the operation is canceled
if !shouldContinue {
printer.Error(cmd, err)
os.Exit(1)
}

appAlternateId := GetAppID(cmd, appName)
if appAlternateId == "" {
os.Exit(0)
}

newApp, err := appService.CreateApp(orgID, *p.ID, appAlternateId, appName)
if err != nil {
if !errors.Is(err, errors.ErrConflict) {
printer.Error(cmd, err)
os.Exit(1)
}

existingApp, handleErr := HandleExistingApp(cmd, appService, orgID, appAlternateId)
if handleErr != nil {
printer.Error(cmd, handleErr)
os.Exit(1)
}
if existingApp == nil {
printer.Info("Operation cancelled.")
os.Exit(0)
}

newApp = *existingApp
}

return &newApp, nil
}
118 changes: 76 additions & 42 deletions cmd/initproject/initproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,31 @@ func RunInitProject(cmd *cobra.Command, args []string) {

projectService := projects.NewService(orgID)

projectName, shouldContinue, err := GetProjectName(cmd, args)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}
if !shouldContinue {
os.Exit(0)
}
pid, _ := cmd.Flags().GetString("id")

projectAlternateId := GetProjectID(cmd, projectName)
if projectAlternateId == "" {
os.Exit(0)
}
var proj *projects.Project
if pid != "" {
exists, err := projectService.DoesProjectExist(pid)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}

if exists {
proj, err = HandleExistingProject(cmd, projectService, pid)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}
}
}
if proj == nil {
proj, err = CreateNewProject(cmd, args, projectService)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}
}
if manifest.ExistsLocal() {
response := prompt.PromptYesNo(cmd, "Config file exists. Do you want to overwrite it?", false)
if !response.Confirmed {
Expand All @@ -100,36 +111,10 @@ func RunInitProject(cmd *cobra.Command, args []string) {
}
}

newProject := projects.Project{
Name: projectName,
AlternateID: projectAlternateId,
IsMonorepo: IsMonorepo,
}

createdProject, err := projectService.CreateProject(newProject)
if err != nil {
if !errors.Is(err, errors.ErrConflict) {
printer.Error(cmd, err)
os.Exit(1)
}

existingProject, handleErr := HandleExistingProject(cmd, projectService, projectAlternateId)
if handleErr != nil {
printer.Error(cmd, handleErr)
os.Exit(1)
}
if existingProject == nil {
printer.Info("Operation cancelled.")
os.Exit(0)
}

createdProject = *existingProject
}

mcl := manifest.Config{
ProjectId: createdProject.ID,
ProjectAlternateId: &createdProject.AlternateID,
ProjectName: &createdProject.Name,
ProjectId: proj.ID,
ProjectAlternateId: &proj.AlternateID,
ProjectName: &proj.Name,
OrganizationId: orgID,
}
mcl.IsMonorepo = ptr.Bool(IsMonorepo)
Expand All @@ -143,7 +128,7 @@ func RunInitProject(cmd *cobra.Command, args []string) {
if err := gitutil.EnsureGitignore(manifest.ManifestSecretFile); err != nil {
printer.Error(cmd, fmt.Errorf("error adding .hxkey to .gitignore: %w. Please do this manually if you wish", err))
}
PrintInitializationSummary(createdProject.Name, createdProject.AlternateID, *createdProject.ID, orgID)
PrintInitializationSummary(proj.Name, proj.AlternateID, *proj.ID, orgID)
}

func GetProjectID(cmd *cobra.Command, projectName string) string {
Expand Down Expand Up @@ -229,6 +214,10 @@ func HandleExistingProject(cmd *cobra.Command, projectService projects.ProjectSe
return nil, err
}

if IsMonorepo && !existingProject.IsMonorepo {
return nil, errors.New("Existing project is not a monorepo project")
}

printer.Info(fmt.Sprintf("Using existing project '%s' (%s)", existingProject.Name, existingProject.AlternateID))
return &existingProject, nil
}
Expand Down Expand Up @@ -263,3 +252,48 @@ func GetProjectName(cmd *cobra.Command, args []string) (string, bool, error) {

return dirName, true, nil
}

func CreateNewProject(cmd *cobra.Command, args []string, ps projects.ProjectService) (*projects.Project, error) {

projectName, shouldContinue, err := GetProjectName(cmd, args)
if err != nil {
printer.Error(cmd, err)
os.Exit(1)
}
if !shouldContinue {
os.Exit(0)
}

projectAlternateId := GetProjectID(cmd, projectName)
if projectAlternateId == "" {
os.Exit(0)
}

newProject := projects.Project{
Name: projectName,
AlternateID: projectAlternateId,
IsMonorepo: IsMonorepo,
}

createdProject, err := ps.CreateProject(newProject)
if err != nil {
if !errors.Is(err, errors.ErrConflict) {
printer.Error(cmd, err)
os.Exit(1)
}

existingProject, handleErr := HandleExistingProject(cmd, ps, projectAlternateId)
if handleErr != nil {
printer.Error(cmd, handleErr)
os.Exit(1)
}
if existingProject == nil {
printer.Info("Operation cancelled.")
os.Exit(0)
}

createdProject = *existingProject
}

return &createdProject, nil
}
26 changes: 26 additions & 0 deletions internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AppServicer interface {
CreateApp(organizationID, projectID, alternateID, name string) (App, error)
GetApp(organizationID, appID string) (App, error)
DeleteApp(organizationID, appID string) error
DoesAppExist(organizationID, appID string) (bool, error)
}

type AppService struct {
Expand Down Expand Up @@ -167,3 +168,28 @@ func (ps *AppService) DeleteApp(organizationID, appID string) error {

return nil
}

func (ps *AppService) DoesAppExist(organizationID, appID string) (bool, error) {
url := fmt.Sprintf("%s/api/organizations/%s/apps/%s/", ps.baseUrl, organizationID, appID)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, errors.Wrap(err, "Failed to create request")
}

resp, err := ps.httpClient.Do(req)
if err != nil {
return false, errors.Wrap(err, "Request failed")
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return true, nil
}

if resp.StatusCode == http.StatusNotFound {
return false, nil
}

return false, errors.HandleHTTPError(resp)
}
26 changes: 26 additions & 0 deletions internal/projects/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ProjectServicer interface {
ListProjects() ([]Project, error)
GetProject(projectID string) (Project, error)
CreateProject(project Project) (Project, error)
DoesProjectExist(projectID string) (bool, error)
}

type ProjectService struct {
Expand Down Expand Up @@ -149,3 +150,28 @@ func (ps *ProjectService) CreateProject(project Project) (Project, error) {

return createdProject, nil
}

func (ps *ProjectService) DoesProjectExist(projectID string) (bool, error) {
url := fmt.Sprintf("%s/%s", ps.baseUrl, projectID)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, errors.Wrap(err, "Failed to create request")
}

resp, err := ps.httpClient.Do(req)
if err != nil {
return false, errors.Wrap(err, "Request failed")
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return true, nil
}

if resp.StatusCode == http.StatusNotFound {
return false, nil
}

return false, errors.HandleHTTPError(resp)
}
Loading