Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
baksetercx committed Dec 3, 2024
1 parent 6840632 commit ef03882
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ jobs:

- name: 'Run tests for ${{ matrix.command }} command'
run: './tests/${{ matrix.command }}/run_tests.sh'
env:
3LV_NON_INTERACTIVE: 'true'
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func main() {
Usage: "Command Line Interface tool for developing, building and securing Elvia applications ⚡",
Version: version,
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "non-interactive",
Usage: "Run in non-interactive mode. Accepts all default values without prompting for confirmation.",
Sources: cli.EnvVars("3LV_NON_INTERACTIVE"),
},
},
After: func(ctx context.Context, c *cli.Command) error {
latestVersion, _ := getLatestVersion(ctx)
if semver.Compare("v"+version, "v"+latestVersion) == -1 {
Expand Down
13 changes: 7 additions & 6 deletions pkg/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package create

import (
"context"
"fmt"
"log"
"os/exec"
"path"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/3lvia/cli/pkg/command"
"github.com/3lvia/cli/pkg/githubactions"
"github.com/3lvia/cli/pkg/shared"
"github.com/3lvia/cli/pkg/utils"
"github.com/urfave/cli/v3"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -57,6 +57,8 @@ func Create(ctx context.Context, c *cli.Command) error {
return cli.ShowAppHelp(c)
}

nonInteractive := c.Bool("non-interactive")

outputDirectory := c.Args().First()
if outputDirectory == "" {
return cli.Exit("Output directory not provided", 1)
Expand All @@ -78,14 +80,12 @@ func Create(ctx context.Context, c *cli.Command) error {
checkCoooiecutterInstalledOutput := checkCookiecutterInstalledCommand(nil)
if command.IsError(checkCoooiecutterInstalledOutput) {
log.Printf("Cookiecutter is not installed. Do you want to install it? (Y/n)")

var answer string
_, err := fmt.Scanln(&answer)
yes, err := utils.PromptYesNo("Cookiecutter is not installed. Do you want to install it?", nonInteractive)
if err != nil {
log.Fatal(err)
return cli.Exit(err, 1)
}

if answer != "n" {
if yes {
checkPipxInstalledOutput := checkPipxÌnstalledCommand(nil)
if command.IsError(checkPipxInstalledOutput) {
log.Fatal("pipx, which is required for installing cookiecutter, is not installed. Please install it first.")
Expand Down Expand Up @@ -127,6 +127,7 @@ func Create(ctx context.Context, c *cli.Command) error {
applicationName,
"",
defaultBranch,
nonInteractive,
)
if err != nil {
return cli.Exit(err, 1)
Expand Down
21 changes: 14 additions & 7 deletions pkg/githubactions/githubactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func GitHubActions(ctx context.Context, c *cli.Command) error {
c.String("application-name"),
c.String("helm-values-file"),
c.String("default-branch"),
c.Bool("non-interactive"),
)
if err != nil {
return cli.Exit(err, 1)
Expand All @@ -69,6 +70,7 @@ func CreateDeployWorkflow(
applicationName string,
helmValuesFile string,
defaultBranch string,
nonInteractive bool,
) error {
const githubActionsDir = ".github/workflows"
fullPath := path.Join(outputDirectory, githubActionsDir)
Expand All @@ -85,7 +87,7 @@ func CreateDeployWorkflow(
return err
}

helmValuesFile_, err := resolveHelmValuesFile(helmValuesFile, outputDirectory)
helmValuesFile_, err := resolveHelmValuesFile(helmValuesFile, outputDirectory, nonInteractive)
if err != nil {
return err
}
Expand Down Expand Up @@ -291,14 +293,19 @@ func getExampleWorkflowFileURL(language string, runtimeCloudProvider string) (st
)
}

func resolveHelmValuesFile(outputDirectory string, helmValuesFile string) (string, error) {
func resolveHelmValuesFile(outputDirectory string, helmValuesFile string, nonInteractive bool) (string, error) {
if helmValuesFile == "" {
const defaultHelmValuesFile = ".github/deploy/values.yml"

var answer string
fmt.Printf("You have not provided a Helm values file, which is required for the deployment. Do you want to create a default Helm values file at '.github/deploy/values.yml'? (Y/n): ")
fmt.Scanln(&answer)
if strings.ToLower(answer) == "n" {
yes, err := utils.PromptYesNo(
"You have not provided a Helm values file, which is required for the deployment. Do you want to create a default Helm values file?",
nonInteractive,
)
if err != nil {
return "", err
}

if !yes {
return "", fmt.Errorf("Helm values file is required for the deployment.")
}

Expand All @@ -313,7 +320,7 @@ func resolveHelmValuesFile(outputDirectory string, helmValuesFile string) (strin
return "", fmt.Errorf("Failed to create directory for Helm values file: %w", err)
}

// TODO: use tempalte file
// TODO: use template file
if err := os.WriteFile(newHelmValuesFile, []byte("image:\n tag: latest\n"), 0644); err != nil {
return "", fmt.Errorf("Failed to create default Helm values file: %w", err)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,19 @@ func WriteFileWithTemplate(

return filePath, nil
}

// Will only return false if the response is "n"
func PromptYesNo(question string, nonInteractive bool) (bool, error) {
fmt.Printf("%s (Y/n): ", question)
if nonInteractive {
return true, nil
}

var response string
_, err := fmt.Scanln(&response)
if err != nil {
return false, fmt.Errorf("Failed to read response: %s", err)
}

return strings.ToLower(response) != "n", nil
}

0 comments on commit ef03882

Please sign in to comment.