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

Template variables #59

Merged
merged 2 commits into from
Apr 4, 2024
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
145 changes: 5 additions & 140 deletions cmd/environment/action/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,32 @@ package action

import (
"errors"
"io"
"os"

"bunnyshell.com/cli/pkg/api"
"bunnyshell.com/cli/pkg/api/environment"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/util"
"bunnyshell.com/sdk"
"github.com/spf13/cobra"
)

var (
errCreateSourceNotProvided = errors.New("template id, content or git repository must be provided")
errK8SIntegrationNotProvided = errors.New("kubernetes integration must be provided when deploying")
)

type CreateSource struct {
TemplateID string

Git string

YamlPath string

GitRepo string
GitBranch string
GitPath string
}

func (createSource *CreateSource) UpdateCommandFlags(command *cobra.Command) {
flags := command.Flags()

flags.StringVar(&createSource.Git, "from-git", createSource.Git, "Use a template git repository during creation")
flags.StringVar(&createSource.TemplateID, "from-template", createSource.TemplateID, "Use a TemplateID during creation")
flags.StringVar(&createSource.YamlPath, "from-path", createSource.YamlPath, "Use a local bunnyshell.yaml during creation")
flags.StringVar(&createSource.GitRepo, "from-git-repo", createSource.GitRepo, "Git repository for the environment")
flags.StringVar(&createSource.GitBranch, "from-git-branch", createSource.GitBranch, "Git branch for the environment")
flags.StringVar(&createSource.GitPath, "from-git-path", createSource.GitPath, "Git path for the environment")

command.MarkFlagsMutuallyExclusive("from-git", "from-template", "from-path", "from-git-repo")
command.MarkFlagsRequiredTogether("from-git-branch", "from-git-repo")
command.MarkFlagsRequiredTogether("from-git-path", "from-git-repo")

_ = command.MarkFlagFilename("from-path", "yaml", "yml")
}

func init() {
options := config.GetOptions()
settings := config.GetSettings()

createOptions := environment.NewCreateOptions()
createSource := CreateSource{}

command := &cobra.Command{
Use: "create",

ValidArgsFunction: cobra.NoFileCompletions,

PreRunE: func(cmd *cobra.Command, args []string) error {
if createSource.Git == "" && createSource.TemplateID == "" && createSource.YamlPath == "" && createSource.GitRepo == "" {
return errCreateSourceNotProvided
if err := createOptions.Validate(); err != nil {
return err
}

if createOptions.WithDeploy && createOptions.GetKubernetesIntegration() == "" {
Expand All @@ -77,19 +42,13 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
createOptions.Project = settings.Profile.Context.Project

if err := parseCreateOptions(createSource, createOptions); err != nil {
if err := createOptions.AttachGenesis(); err != nil {
return lib.FormatCommandError(cmd, err)
}

model, err := environment.Create(createOptions)
if err != nil {
var apiError api.Error

if errors.As(err, &apiError) {
return handleCreateErrors(cmd, apiError, createOptions)
}

return lib.FormatCommandError(cmd, err)
return createOptions.HandleError(cmd, err)
}

if !createOptions.WithDeploy {
Expand All @@ -112,101 +71,7 @@ func init() {
util.FlagRequired,
))

createOptions.UpdateFlagSet(flags)

createSource.UpdateCommandFlags(command)
createOptions.UpdateCommandFlags(command)

mainCmd.AddCommand(command)
}

func handleCreateErrors(cmd *cobra.Command, apiError api.Error, createOptions *environment.CreateOptions) error {
genesisName := getCreateGenesisName(createOptions)

if len(apiError.Violations) == 0 {
return apiError
}

for _, violation := range apiError.Violations {
cmd.Printf("Problem with %s: %s\n", genesisName, violation.GetMessage())
}

return lib.ErrGeneric
}

func getCreateGenesisName(createOptions *environment.CreateOptions) string {
if createOptions.Genesis.FromGitSpec != nil {
return "--from-git"
}

if createOptions.Genesis.FromTemplate != nil {
return "--from-template"
}

if createOptions.Genesis.FromString != nil {
return "--from-path"
}

return "arguments"
}

func parseCreateOptions(createSource CreateSource, createOptions *environment.CreateOptions) error {
createOptions.Genesis = &sdk.EnvironmentCreateActionGenesis{}

if createSource.Git != "" {
fromGitSpec := sdk.NewFromGitSpec()
fromGitSpec.Spec = &createSource.Git

createOptions.Genesis.FromGitSpec = fromGitSpec

return nil
}

if createSource.TemplateID != "" {
fromTemplate := sdk.NewFromTemplate()
fromTemplate.Template = &createSource.TemplateID

createOptions.Genesis.FromTemplate = fromTemplate

return nil
}

if createSource.YamlPath != "" {
fromString := sdk.NewFromString()

bytes, err := readFile(createSource.YamlPath)
if err != nil {
return err
}

content := string(bytes)
fromString.Yaml = &content

createOptions.Genesis.FromString = fromString

return nil
}

if createSource.GitRepo != "" {
fromGit := sdk.NewFromGit()
fromGit.Url = &createSource.GitRepo
fromGit.Branch = &createSource.GitBranch
fromGit.YamlPath = &createSource.GitPath

createOptions.Genesis.FromGit = fromGit

return nil
}

return errCreateSourceNotProvided
}

func readFile(fileName string) ([]byte, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}

defer file.Close()

return io.ReadAll(file)
}
138 changes: 7 additions & 131 deletions cmd/environment/action/update.configuration.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,37 @@
package action

import (
"errors"

"bunnyshell.com/cli/pkg/api"
"bunnyshell.com/cli/pkg/api/environment"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/sdk"
"github.com/spf13/cobra"
)

type EditSource struct {
TemplateID string

Git string

YamlPath string

GitRepo string
GitBranch string
GitPath string
}

func (es *EditSource) UpdateCommandFlags(command *cobra.Command) {
flags := command.Flags()

flags.StringVar(&es.Git, "from-git", es.Git, "Use a template git repository during update")
flags.StringVar(&es.TemplateID, "from-template", es.TemplateID, "Use a template ID during update")
flags.StringVar(&es.YamlPath, "from-path", es.YamlPath, "Use a local environment yaml during update")
flags.StringVar(&es.GitRepo, "from-git-repo", es.GitRepo, "Git repository for the environment template")
flags.StringVar(&es.GitBranch, "from-git-branch", es.GitBranch, "Git branch for the environment template")
flags.StringVar(&es.GitPath, "from-git-path", es.GitPath, "Git path for the environment template")

command.MarkFlagsMutuallyExclusive("from-git", "from-template", "from-path", "from-git-repo")
command.MarkFlagsRequiredTogether("from-git-branch", "from-git-repo")
command.MarkFlagsRequiredTogether("from-git-path", "from-git-repo")

_ = command.MarkFlagFilename("from-path", "yaml", "yml")
}

func init() {
options := config.GetOptions()
settings := config.GetSettings()

editConfigurationOptions := environment.NewEditConfigurationOptions("")
editSource := EditSource{}

command := &cobra.Command{
Use: "update-configuration",

ValidArgsFunction: cobra.NoFileCompletions,

PreRunE: func(cmd *cobra.Command, args []string) error {
return editConfigurationOptions.Validate()
},

RunE: func(cmd *cobra.Command, args []string) error {
editConfigurationOptions.ID = settings.Profile.Context.Environment

if err := parseEditConfigurationOptions(editSource, editConfigurationOptions); err != nil {
if err := editConfigurationOptions.AttachGenesis(); err != nil {
return lib.FormatCommandError(cmd, err)
}

model, err := environment.EditConfiguration(editConfigurationOptions)
if err != nil {
var apiError api.Error

if errors.As(err, &apiError) {
return handleEditErrors(cmd, apiError, editConfigurationOptions)
}

return lib.FormatCommandError(cmd, err)
return editConfigurationOptions.HandleError(cmd, err)
}

if !editConfigurationOptions.WithDeploy {
Expand All @@ -87,95 +51,7 @@ func init() {
flags.AddFlag(idFlag)
_ = command.MarkFlagRequired(idFlag.Name)

editConfigurationOptions.UpdateFlagSet(flags)
editSource.UpdateCommandFlags(command)
editConfigurationOptions.UpdateCommandFlags(command)

mainCmd.AddCommand(command)
}

func handleEditErrors(cmd *cobra.Command, apiError api.Error, editOptions *environment.EditConfigurationOptions) error {
genesisName := getEditGenesisName(editOptions)

if len(apiError.Violations) == 0 {
return apiError
}

for _, violation := range apiError.Violations {
cmd.Printf("Problem with %s: %s\n", genesisName, violation.GetMessage())
}

return lib.ErrGeneric
}

func getEditGenesisName(editOptions *environment.EditConfigurationOptions) string {
configuration := editOptions.Configuration

if configuration.FromGitSpec != nil {
return "--from-git"
}

if configuration.FromTemplate != nil {
return "--from-template"
}

if configuration.FromString != nil {
return "--from-path"
}

return "arguments"
}

//nolint:dupl
func parseEditConfigurationOptions(
editSource EditSource,
editConfigurationOptions *environment.EditConfigurationOptions,
) error {
editConfigurationOptions.Configuration = &sdk.EnvironmentEditConfigurationConfiguration{}

if editSource.Git != "" {
fromGitSpec := sdk.NewFromGitSpec()
fromGitSpec.Spec = &editSource.Git

editConfigurationOptions.Configuration.FromGitSpec = fromGitSpec

return nil
}

if editSource.TemplateID != "" {
fromTemplate := sdk.NewFromTemplate()
fromTemplate.Template = &editSource.TemplateID

editConfigurationOptions.Configuration.FromTemplate = fromTemplate

return nil
}

if editSource.YamlPath != "" {
fromString := sdk.NewFromString()

bytes, err := readFile(editSource.YamlPath)
if err != nil {
return err
}

content := string(bytes)
fromString.Yaml = &content

editConfigurationOptions.Configuration.FromString = fromString

return nil
}

if editSource.GitRepo != "" {
fromGit := sdk.NewFromGit()
fromGit.Url = &editSource.GitRepo
fromGit.Branch = &editSource.GitBranch
fromGit.YamlPath = &editSource.GitPath

editConfigurationOptions.Configuration.FromGit = fromGit

return nil
}

return errCreateSourceNotProvided
}
Loading
Loading