Skip to content

Commit

Permalink
refactor: localize crud templates (#172)
Browse files Browse the repository at this point in the history
* refactor: localize crud templates

* refactor: make locale path dynamic

* chore: add flag entries to crud locale file

* fix: resolve dir already exists error

* fix: delete the broken crud dir if error occurs

* docs: update inline docs

* fix: resolve build issues
  • Loading branch information
namit-chandwani authored Jul 27, 2021
1 parent 1b65d54 commit 020b6c4
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 4 deletions.
52 changes: 48 additions & 4 deletions pkg/cmd/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"io/fs"
"os"
"path/filepath"

"github.com/aerogear/charmil/core/factory"
"github.com/aerogear/charmil/pkg/template/crud"
Expand Down Expand Up @@ -71,16 +72,20 @@ func generateCrudFiles() error {
crudDirPath := flagVars.path + "/crud"

// Creates a directory using value in the `crudDirPath` variable
err := os.Mkdir(crudDirPath, 0755)
if err != nil {
return err
if _, err := os.Stat(crudDirPath); os.IsNotExist(err) {
err := os.Mkdir(crudDirPath, 0755)
if err != nil {
return err
}
}

// Generates CRUD files in the `crud` directory by looping through the template files
err = fs.WalkDir(crud.CrudTemplates, ".", func(path string, info fs.DirEntry, err error) error {
err := fs.WalkDir(crud.CrudTemplates, ".", func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}

// Excludes non-template files from generation
if info.Name() == "." || info.Name() == "tmpl.go" {
return nil
}
Expand Down Expand Up @@ -109,6 +114,21 @@ func generateCrudFiles() error {
// generateFileFromTemplate uses the passed contents and data object of a
// template to generate a new file using the specified file name and output path
func generateFileFromTemplate(name, path, tmplContent string, tmplData interface{}) error {
localePath, err := getLocalePath()
if err != nil {
// Removes the broken `crud` directory if there is an error
if e := os.RemoveAll(path); e != nil {
return e
}

return err
}

// Sets appropriate target path for the locale file
if name == "crud.en.yaml" {
path = fmt.Sprintf("./%s/en", localePath)
}

// Creates a new file using the specified name and path
f, err := os.Create(fmt.Sprintf("%s/%s", path, name))
if err != nil {
Expand All @@ -125,3 +145,27 @@ func generateFileFromTemplate(name, path, tmplContent string, tmplData interface

return nil
}

// getLocalePath returns the path of the `locale` directory of CLI
func getLocalePath() (string, error) {
// Stores the path of `locale` directory
var localePath string

// Walks through each directory and file in `cmd` dir to find the `locale` dir
err := filepath.Walk("./cmd", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() && info.Name() == "locales" {
localePath = path
}

return nil
})
if err != nil {
return "", err
}

return localePath, nil
}
102 changes: 102 additions & 0 deletions pkg/template/crud/crud.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Create Command

crud.cmd.create.use:
one: create

crud.cmd.create.shortDescription:
one: Create an instance

crud.cmd.create.longDescription:
one: Create an instance.

crud.cmd.create.example:
one: cli_name create [args] [flags]

# Delete Command

crud.cmd.delete.use:
one: delete

crud.cmd.delete.shortDescription:
one: Delete an instance

crud.cmd.delete.longDescription:
one: Delete an instance.

crud.cmd.delete.example:
one: cli_name delete [args] [flags]

# Describe Command

crud.cmd.describe.use:
one: describe

crud.cmd.describe.shortDescription:
one: Describe an instance

crud.cmd.describe.longDescription:
one: Describe an instance.

crud.cmd.describe.example:
one: cli_name describe [args] [flags]

# List Command

crud.cmd.list.use:
one: list

crud.cmd.list.shortDescription:
one: List instances

crud.cmd.list.longDescription:
one: List all instances.

crud.cmd.list.example:
one: cli_name list [args] [flags]

# Use Command

crud.cmd.use.use:
one: use

crud.cmd.use.shortDescription:
one: Use an instance

crud.cmd.use.longDescription:
one: Select an instance to use with all instance-specific commands.

crud.cmd.use.example:
one: cli_name use [args] [flags]

# Flags

crud.cmd.flag.output.description:
description: "Description for --output flag"
one: 'The format in which the instance should be displayed. Choose from: "json", "yml", "yaml".'

crud.cmd.create.flag.use.description:
one: "Set the new instance to the current instance"

crud.common.flag.id:
description: "Description for the --id flag"
one: "Unique ID of the instance you want to delete. If not set, the current instance is used."

crud.common.flag.yes:
description: "Description for the --yes flag"
one: "Skip confirmation to forcibly delete this instance."

crud.list.flag.page:
description: "Description for the --page flag"
one: "Display the instances from the specified page number."

crud.list.flag.limit:
description: "Description for the --limit flag"
one: "The maximum number of instances to be returned."

crud.list.flag.search:
description: "Description for the --search flag"
one: "Text search to filter the instances by name."

crud.use.flag.id:
description: "Description for the --id flag"
one: "Unique ID of the instance you want to set as the current instance."
1 change: 1 addition & 0 deletions pkg/template/crud/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package crud

import "embed"

// CrudTemplates stores embedded contents of all the CRUD template files
//go:embed *
var CrudTemplates embed.FS

0 comments on commit 020b6c4

Please sign in to comment.