Skip to content

Commit

Permalink
Move common behavior to helpers
Browse files Browse the repository at this point in the history
render and generate registry use very similar output behaviors.
Collecting them as helpers will encourage their reuse.
  • Loading branch information
angrycub committed Sep 5, 2023
1 parent 4f34329 commit ebf8640
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 90 deletions.
79 changes: 79 additions & 0 deletions internal/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cli

import (
"fmt"
"io/fs"
"os"
"strings"

Expand Down Expand Up @@ -499,3 +500,81 @@ func clientOptsFromFlags(c *baseCommand, conf *api.Config) {
conf.TLSConfig.Insecure = true
}
}

func validateOutDir(path string) error {
if path == "" {
return nil
}
info, err := os.Stat(path)

if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}

return fmt.Errorf("unexpected error validating --to-dir path: %w", err)
}

if !info.IsDir() {
return errors.New("--to-dir must be a directory")
}

return nil
}

func writeFile(c *outputCommand, path string, content string) error {
// Check to see if the file already exists and validate against the value
// of overwrite.
_, err := os.Stat(path)
if err == nil {
var overwrite bool
overwrite, err = confirmOverwrite(c, path)
if err != nil {
return err
}
if !overwrite {
return fmt.Errorf("destination file exists and overwrite is unset")
}
}

err = os.WriteFile(path, []byte(content), 0644)
if err != nil {
return fmt.Errorf("failed to write rendered template to file: %s", err)
}

return nil
}

func confirmOverwrite(c *outputCommand, path string) (bool, error) {
// For non-interactive UIs, the value must be passed by flag.
if !c.ui.Interactive() {
return c.autoApproved, nil
}

if c.autoApproved || c.overwriteAll {
return true, nil
}

// For interactive UIs, we can do a y/n/a
for {
overwrite, err := c.ui.Input(&terminal.Input{
Prompt: fmt.Sprintf("Output file %q exists, overwrite? [y/n/a] ", path),
Style: terminal.WarningBoldStyle,
})
if err != nil {
return false, err
}
overwrite = strings.ToLower(overwrite)
switch overwrite {
case "a":
c.overwriteAll = true
return true, nil
case "y":
return true, nil
case "n":
return false, nil
default:
c.ui.Output("Please select a valid option.\n", terminal.WithStyle(terminal.ErrorBoldStyle))
}
}
}
88 changes: 0 additions & 88 deletions internal/cli/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ package cli

import (
"context"
"fmt"
"io/fs"
"os"
"path"
"strings"

Expand All @@ -31,20 +28,13 @@ type RenderCommand struct {
// template is rendered.
renderOutputTemplate bool

// renderToDir is the path to write rendered job files to in addition to
// standard output.
renderToDir string

// noRenderAuxFiles is a boolean flag to control whether we should also render
// auxiliary files inside templates/
noRenderAuxFiles bool

// noFormat is a boolean flag to control whether we should hcl-format the
// templates before rendering them.
noFormat bool

// overwriteAll is set to true when someone specifies "a" to the y/n/a
overwriteAll bool
}

type Render struct {
Expand Down Expand Up @@ -81,84 +71,6 @@ func (r Render) toFile(c *RenderCommand, ec *errors.UIErrorContext) error {
return nil
}

func confirmOverwrite(c *RenderCommand, path string) (bool, error) {
// For non-interactive UIs, the value must be passed by flag.
if !c.ui.Interactive() {
return c.autoApproved, nil
}

if c.autoApproved || c.overwriteAll {
return true, nil
}

// For interactive UIs, we can do a y/n/a
for {
overwrite, err := c.ui.Input(&terminal.Input{
Prompt: fmt.Sprintf("Output file %q exists, overwrite? [y/n/a] ", path),
Style: terminal.WarningBoldStyle,
})
if err != nil {
return false, err
}
overwrite = strings.ToLower(overwrite)
switch overwrite {
case "a":
c.overwriteAll = true
return true, nil
case "y":
return true, nil
case "n":
return false, nil
default:
c.ui.Output("Please select a valid option.\n", terminal.WithStyle(terminal.ErrorBoldStyle))
}
}
}

func validateOutDir(path string) error {
if path == "" {
return nil
}
info, err := os.Stat(path)

if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}

return fmt.Errorf("unexpected error validating --to-dir path: %w", err)
}

if !info.IsDir() {
return errors.New("--to-dir must be a directory")
}

return nil
}

func writeFile(c *RenderCommand, path string, content string) error {
// Check to see if the file already exists and validate against the value
// of overwrite.
_, err := os.Stat(path)
if err == nil {
var overwrite bool
overwrite, err = confirmOverwrite(c, path)
if err != nil {
return err
}
if !overwrite {
return fmt.Errorf("destination file exists and overwrite is unset")
}
}

err = os.WriteFile(path, []byte(content), 0644)
if err != nil {
return fmt.Errorf("failed to write rendered template to file: %s", err)
}

return nil
}

// formatRenderName trims the low-value elements from the rendered template
// name.
func formatRenderName(name string) string {
Expand Down
16 changes: 14 additions & 2 deletions internal/creator/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,24 @@ type registryCreator struct {
// - A packs folder that contains all of the packs present in the registry
// - Optionally, a sample pack to get started with
func CreateRegistry(c config.PackConfig) error {
var err error

ui := c.GetUI()
outPath := c.OutPath
if outPath == "" {
outPath = "."
}
ui.Output("Creating %q Registry in %q...\n", c.RegistryName, outPath)

_, err = os.Stat(path.Join(outPath, c.PackName))
if err == nil && !c.Overwrite {
return newCreateRegistryError(
fmt.Errorf(
"%s appears to be non-empty, re-run the command with --overwrite to overwrite",
path.Join(outPath, c.RegistryName),
))
}

ui.Output("Creating %q registry in %q...", c.RegistryName, outPath)

rc := registryCreator{
name: c.RegistryName,
Expand All @@ -44,7 +56,7 @@ func CreateRegistry(c config.PackConfig) error {
// TODO: Make this optional
// TODO: Make this interactive

err := os.MkdirAll(rc.packsPath, cache.DefaultDirPerms)
err = os.MkdirAll(rc.packsPath, cache.DefaultDirPerms)
if err != nil {
return newCreateRegistryError(err)
}
Expand Down

0 comments on commit ebf8640

Please sign in to comment.