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(api/template): Passing projectID to DeployTemplate #99

Merged
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
20 changes: 15 additions & 5 deletions internal/cmd/template/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

type Options struct {
file string
file string
projectID string
}

func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
Expand All @@ -34,15 +35,15 @@ func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
}

cmd.Flags().StringVarP(&opts.file, "file", "f", "", "Template file")
cmd.Flags().StringVar(&opts.projectID, "project-id", "", "Project ID to deploy on")

return cmd
}

func runDeploy(f *cmdutil.Factory, opts *Options) error {
var err error

err = paramCheck(opts)
if err != nil {
if err := paramCheck(opts); err != nil {
return err
}

Expand Down Expand Up @@ -75,6 +76,10 @@ func runDeploy(f *cmdutil.Factory, opts *Options) error {
}
}

if _, err := f.ParamFiller.ProjectCreatePreferred(&opts.projectID); err != nil {
return err
}

type RawTemplate struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Expand All @@ -99,7 +104,6 @@ func runDeploy(f *cmdutil.Factory, opts *Options) error {

vars := model.Map{}
for _, v := range raw.Spec.Variables {

switch v.Type {
case "DOMAIN":
for {
Expand Down Expand Up @@ -145,7 +149,13 @@ func runDeploy(f *cmdutil.Factory, opts *Options) error {
)

s.Start()
res, err := f.ApiClient.DeployTemplate(context.Background(), string(file), vars, model.RepoConfigs{})
res, err := f.ApiClient.DeployTemplate(
context.Background(),
string(file),
vars,
model.RepoConfigs{},
opts.projectID,
)
s.Stop()

if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ type (
ListAllTemplates(ctx context.Context) (model.Templates, error)
GetTemplate(ctx context.Context, code string) (*model.Template, error)

DeployTemplate(ctx context.Context, rawSpecYaml string, variables model.Map, repoConfigs model.RepoConfigs) (*model.Project, error)
DeployTemplate(
ctx context.Context,
rawSpecYaml string,
variables model.Map,
repoConfigs model.RepoConfigs,
projectID string,
) (*model.Project, error)
DeleteTemplate(ctx context.Context, code string) error
}
)
18 changes: 15 additions & 3 deletions pkg/api/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,24 @@ func (c *client) ListAllTemplates(ctx context.Context) (model.Templates, error)
return templates, nil
}

func (c *client) DeployTemplate(ctx context.Context, rawSpecYaml string, variables model.Map, repoConfigs model.RepoConfigs) (*model.Project, error) {
func (c *client) DeployTemplate(
ctx context.Context,
rawSpecYaml string,
variables model.Map,
repoConfigs model.RepoConfigs,
projectID string,
) (*model.Project, error) {
var mutation struct {
DeployTemplate model.Project `graphql:"deployTemplate(rawSpecYaml: $rawSpecYaml, variables: $variables)"`
DeployTemplate model.Project `graphql:"deployTemplate(rawSpecYaml: $rawSpecYaml, variables: $variables, projectID: $projectID)"`
}

err := c.Mutate(ctx, &mutation, V{"rawSpecYaml": rawSpecYaml, "variables": variables})
deployParams := V{
"rawSpecYaml": rawSpecYaml,
"variables": variables,
"projectID": ObjectID(projectID),
}

err := c.Mutate(ctx, &mutation, deployParams)
if err != nil {
return nil, err
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/fill/fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
type ParamFiller interface {
// Project fills the projectID if it is empty by asking user to select a project
Project(projectID *string) (changed bool, err error)
// ProjectCreatePreferred fills the projectID if it is empty by asking user to select a project
// and makes "Create New Project…" the prioritized option
ProjectCreatePreferred(projectID *string) (changed bool, err error)
// ProjectByName makes sure either projectID or projectName is not empty
// if necessary, it will ask user to select a project first
ProjectByName(projectID, projectName *string) (changed bool, err error)
Expand Down Expand Up @@ -57,6 +60,25 @@ func (f *paramFiller) Project(projectID *string) (changed bool, err error) {
return true, nil
}

func (f *paramFiller) ProjectCreatePreferred(projectID *string) (changed bool, err error) {
if err = paramNilCheck(projectID); err != nil {
return false, err
}

if *projectID != "" {
return false, nil
}

_, project, err := f.selector.SelectProject(selector.WithCreatePreferred())
if err != nil {
return false, err
}

*projectID = project.ID

return true, nil
}

func (f *paramFiller) ProjectByName(projectID, projectName *string) (changed bool, err error) {
if err = paramNilCheck(projectID, projectName); err != nil {
return false, err
Expand Down Expand Up @@ -247,7 +269,6 @@ func (f *paramFiller) ServiceByNameWithEnvironment(opt ServiceByNameWithEnvironm
}

return changed1 || changed2, nil

}

func paramNilCheck(params ...*string) error {
Expand Down
41 changes: 32 additions & 9 deletions pkg/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type (
}

ProjectSelector interface {
SelectProject() (zcontext.BasicInfo, *model.Project, error)
SelectProject(opts ...SelectProjectOptionsApplyer) (zcontext.BasicInfo, *model.Project, error)
}

ServiceSelector interface {
Expand All @@ -47,15 +47,33 @@ func New(client api.Client, log *zap.SugaredLogger, prompter prompt.Prompter) Se
}
}

func (s *selector) SelectProject() (zcontext.BasicInfo, *model.Project, error) {
type SelectProjectOptions struct {
// CreatePreferred selects "Create New Project…" by default,
// and not auto-select the only project.
CreatePreferred bool
}

type SelectProjectOptionsApplyer func(*SelectProjectOptions)

func WithCreatePreferred() SelectProjectOptionsApplyer {
return func(opt *SelectProjectOptions) {
opt.CreatePreferred = true
}
}

func (s *selector) SelectProject(opts ...SelectProjectOptionsApplyer) (zcontext.BasicInfo, *model.Project, error) {
options := SelectProjectOptions{
CreatePreferred: false,
}
for _, applyer := range opts {
applyer(&options)
}

projects, err := s.client.ListAllProjects(context.Background())
if err != nil {
return nil, nil, fmt.Errorf("list projects failed: %w", err)
}
if len(projects) == 0 {
return nil, nil, fmt.Errorf("no projects found")
}
if len(projects) == 1 {
if len(projects) == 1 && !options.CreatePreferred {
s.log.Infof("Only one project found, select <%s> automatically\n", projects[0].Name)
project := projects[0]
return zcontext.NewBasicInfo(project.ID, project.Name), project, nil
Expand All @@ -66,7 +84,13 @@ func (s *selector) SelectProject() (zcontext.BasicInfo, *model.Project, error) {
projectsName[i] = project.Name
}
projectsName = append(projectsName, "Create a new project")
index, err := s.prompter.Select("Select project", projectsName[0], projectsName)

defaultChoice := projectsName[0]
if options.CreatePreferred {
defaultChoice = projectsName[len(projectsName)-1] // the final item = create project
}

index, err := s.prompter.Select("Select project", defaultChoice, projectsName)
if err != nil {
return nil, nil, fmt.Errorf("select project failed: %w", err)
}
Expand Down Expand Up @@ -102,7 +126,6 @@ func (s *selector) SelectProject() (zcontext.BasicInfo, *model.Project, error) {
project := projects[index]

return zcontext.NewBasicInfo(project.ID, project.Name), project, nil

}

type SelectServiceOptions struct {
Expand Down Expand Up @@ -160,7 +183,7 @@ func (s *selector) SelectService(opt SelectServiceOptions) (zcontext.BasicInfo,
}

if index == len(services) {
var from = []rune("abcdefghijklmnopqrstuvwxyz")
from := []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, 8)
for i := range b {
b[i] = from[rand.Intn(len(from))]
Expand Down
Loading