graphql-engine/cli/pkg/deploy/project_deploy.go
Varun Dey 030f9f44f2 cli: Add cli-ext path option for Deploy API [GT-307]
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7114
GitOrigin-RevId: 643639717f3bb18984a29b6f95dfbb3179c92064
2022-12-01 06:04:25 +00:00

70 lines
1.6 KiB
Go

package deploy
import (
"fmt"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/spf13/viper"
"io"
)
type ProjectDeploy struct {
ec *cli.ExecutionContext
}
type ProjectDeployOption func(deploy *ProjectDeploy)
func (p *ProjectDeploy) Deploy(opts ...ProjectDeployExecutorOptions) error {
var op errors.Op = "deploy.ProjectDeploy.Deploy"
executor := newProjectDeployExecutor(p.ec)
err := executor.deploy(opts...)
if err != nil {
return errors.E(op, err)
}
return nil
}
func WithAdminSecret(adminSecret string) ProjectDeployOption {
return func(deploy *ProjectDeploy) {
deploy.ec.Viper.Set("admin_secret", adminSecret)
}
}
func WithEndpoint(endpoint string) ProjectDeployOption {
return func(deploy *ProjectDeploy) {
deploy.ec.Viper.Set("endpoint", endpoint)
}
}
func WithCliExtPath(path string) ProjectDeployOption {
return func(d *ProjectDeploy) {
d.ec.CliExtSourceBinPath = path
}
}
func NewProjectDeploy(projectDirectory string, opts ...ProjectDeployOption) (*ProjectDeploy, error) {
var op errors.Op = "deploy.NewProjectDeploy"
ec := cli.NewExecutionContext()
ec.ExecutionDirectory = projectDirectory
ec.Viper = viper.New()
ec.IsTerminal = false
ec.Stdout = io.Discard
ec.Stderr = io.Discard
if err := ec.Prepare(); err != nil {
return nil, errors.E(op, err)
}
d := &ProjectDeploy{ec}
for _, opt := range opts {
opt(d)
}
if err := ec.Validate(); err != nil {
return nil, errors.E(op, err)
}
if ec.Config.Version <= cli.V1 {
return nil, errors.E(op, fmt.Errorf("config %v is not supported", ec.Config.Version))
}
return d, nil
}