Skip to content

Commit

Permalink
feat: modify init command to not use templates (#243)
Browse files Browse the repository at this point in the history
* feat: modify init command to not use placeholders

* fix: resolve build issues

* refactor: update default starter repo name for replacement

* chore: update language file for completion command

* chore: apply suggestions from code review
  • Loading branch information
namit-chandwani committed Sep 9, 2021
1 parent 65f4501 commit 330af4a
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 74 deletions.
2 changes: 1 addition & 1 deletion cli/internal/cmd/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"path"
"path/filepath"

"github.com/aerogear/charmil/cli/internal/common/modname"
"github.com/aerogear/charmil/cli/internal/template/add"
"github.com/aerogear/charmil/cli/pkg/common/modname"
"github.com/aerogear/charmil/cli/pkg/factory"
"github.com/aerogear/charmil/core/utils/color"
"github.com/spf13/cobra"
Expand Down
4 changes: 2 additions & 2 deletions cli/internal/cmd/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"

"github.com/aerogear/charmil/cli/internal/common/generate"
"github.com/aerogear/charmil/cli/internal/common/modname"
"github.com/aerogear/charmil/cli/pkg/common/generate"
"github.com/aerogear/charmil/cli/pkg/common/modname"

"github.com/aerogear/charmil/cli/internal/template/crud"
"github.com/aerogear/charmil/cli/pkg/factory"
Expand Down
84 changes: 34 additions & 50 deletions cli/internal/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package init
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/aerogear/charmil/cli/pkg/factory"
"github.com/aerogear/charmil/core/utils/color"
Expand Down Expand Up @@ -58,11 +58,15 @@ func InitCommand(f *factory.Factory) *cobra.Command {
}

cloneStarter(f)

f.Logger.Infoln(color.Info("updating starter code with names"))
if err := renderTemplates(templateContext, f); err != nil {

// Searches the generated files for default values and replaces it with the user-specified values
if err := replaceText(templateContext); err != nil {
f.Logger.Error(err)
os.Exit(1)
}

f.Logger.Infof(color.Success("Your %s CLI has been initialized in this directory.\n"), templateContext.CliName)
},
}
Expand Down Expand Up @@ -113,79 +117,59 @@ func cloneStarter(f *factory.Factory) {
}
}

// render templates
func renderTemplates(templateContext TemplateContext, f *factory.Factory) error {
path, pathErr := os.Getwd()
if pathErr != nil {
f.Logger.Error(pathErr)
os.Exit(1)
// replaceText replaces the default values in files with the user-specified values
func replaceText(templateContext TemplateContext) error {
replacerList := []string{
"aerogear/charmil-starter", fmt.Sprintf("%s/%s", templateContext.Owner, templateContext.Repo),
"placeholdercli", templateContext.CliName,
"[name of copyright owner]", templateContext.Owner,
}

rep := strings.NewReplacer(replacerList...)

path, err := os.Getwd()
if err != nil {
return err
}

// rename cli name as given by user
oldPath := path + "/cmd/cli"
oldPath := path + "/cmd/placeholdercli"
newPath := path + "/cmd/" + templateContext.CliName
if err := os.Rename(oldPath, newPath); err != nil {
if err = os.Rename(oldPath, newPath); err != nil {
return err
}

err := filepath.Walk(path,
err = filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// ignore folders
ignoreSlice := []string{".git", ".github", ".chglog", ".goreleaser.yml", "CONTRIBUTING.md", "bin"}
pathSplit := strings.Split(path, string(os.PathSeparator))
intersected := intersection(pathSplit, ignoreSlice)
if len(intersected) > 0 {
// Skips directories
if info.IsDir() || info.Name() == "." {
return nil
}

fi, err := os.Stat(path)
// Stores contents of the current file
fileContents, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file info: %w", err)
}

if fi.IsDir() {
return nil
return err
}

tmpl, tmplErr := template.ParseFiles(path)
if tmplErr != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
// Replaces default values
updatedFileContents := rep.Replace(string(fileContents))

f, PathErr := os.Create(path)
if PathErr != nil {
return fmt.Errorf("failed to create file: %w", err)
}

// apply templateContext to cloned repo
if err := tmpl.Execute(f, templateContext); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
// Writes the updated contents to the current file
err = ioutil.WriteFile(path, []byte(updatedFileContents), 0600)
if err != nil {
return err
}

return nil
})
if err != nil {
return fmt.Errorf("failed to walk directory: %w", err)
return err
}

return nil
}

// find intersection between two slices
func intersection(s1, s2 []string) (inter []string) {
hash := make(map[string]bool)
for _, e := range s1 {
hash[e] = true
}
for _, e := range s2 {
// If elements present in the hashmap then append intersection list.
if hash[e] {
inter = append(inter, e)
}
}
return
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion starter/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/

/cli
/placeholdercli
2 changes: 1 addition & 1 deletion starter/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] {{ .Owner }}
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion starter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BUILDFLAGS := -gcflags "all=-N -l" $(BUILDFLAGS)
endif

# The details of the application:
binary:=cli
binary:=placeholdercli

# Enable Go modules:
export GO111MODULE=on
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"

"github.com/aerogear/charmil/core/config"
"github.com/aerogear/charmil/core/localize"
"github.com/aerogear/charmil/core/utils/localize"
"github.com/aerogear/charmil/starter/internal/update"
"github.com/aerogear/charmil/starter/pkg/factory"
"github.com/spf13/cobra"
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions starter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ require (
github.com/spf13/cobra v1.2.1
golang.org/x/text v0.3.6
)

// replace github.com/aerogear/charmil => ../

0 comments on commit 330af4a

Please sign in to comment.