From b1fd05b42c23e3eea2d68c270690314052ba7891 Mon Sep 17 00:00:00 2001 From: boruiqiao Date: Tue, 31 Oct 2023 13:58:19 +0800 Subject: [PATCH] feat: create project with context set --- internal/cmd/project/create/create.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/cmd/project/create/create.go b/internal/cmd/project/create/create.go index 15b4537..25f78ac 100644 --- a/internal/cmd/project/create/create.go +++ b/internal/cmd/project/create/create.go @@ -3,6 +3,7 @@ package create import ( "context" "fmt" + "github.com/zeabur/cli/pkg/zcontext" "github.com/briandowns/spinner" "github.com/spf13/cobra" @@ -103,7 +104,11 @@ func createProject(f *cmdutil.Factory, projectRegion string, projectName *string } f.Log.Infof("Project %s created", project.Name) - + err = setProject(f, project.ID, project.Name) + if err != nil { + f.Log.Error(err) + return err + } return nil } @@ -114,3 +119,18 @@ func paramCheck(opts *Options) error { return nil } + +func setProject(f *cmdutil.Factory, id, name string) error { + if id == "" && name == "" { + return fmt.Errorf("context auto-switching failed, check if the project was created successfully") + } + f.Config.GetContext().SetProject(zcontext.NewBasicInfo(id, name)) + + // clear environment and service context when project context is set + f.Config.GetContext().ClearService() + f.Config.GetContext().ClearEnvironment() + + f.Log.Infof("Project context is set to <%s>", name) + + return nil +}