Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ivancorrales committed Feb 28, 2022
1 parent 3d37027 commit 5dd72ef
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 26 deletions.
5 changes: 3 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ linters:
- varcheck
- whitespace
- asciicheck
- gochecknoglobals
- gocognit
- godot
- godox
Expand All @@ -113,6 +112,8 @@ linters:
#- interfacer
#- maligned
######
###### ignoring
#- gochecknoglobals

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
Expand All @@ -133,4 +134,4 @@ run:
- test/testdata_etc
- internal/cache
- internal/renameio
- internal/robustio
- internal/robustio
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ before:
builds:
- env:
- CGO_ENABLED=0
main: ./cmd/templatizer
goos:
- darwin
- linux
Expand All @@ -30,4 +31,4 @@ dockers:
- "--label=org.opencontainers.image.name={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--label=org.opencontainers.image.source={{.GitURL}}"
- "--label=org.opencontainers.image.source={{.GitURL}}"
23 changes: 15 additions & 8 deletions cmd/templatizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"

"github.com/anotherlife/templatizer/internal/action"
"github.com/wesovilabs/templatizer/internal/action"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -30,13 +30,20 @@ func command() *cobra.Command {
Run: runTemplatizer,
}

cmd.PersistentFlags().BoolVar(&debug, "verbose", false, "verbose logging")
cmd.PersistentFlags().StringVar(&repoPath, "source", "", "path to the repo i.e. github.com/ivancorrales/go-rest-template)")
cmd.PersistentFlags().StringVarP(&username, "username", "u", "", "user handle")
cmd.PersistentFlags().StringVarP(&password, "password", "p", "", "user secret for the provided username")
cmd.PersistentFlags().StringVarP(&templateMode, "mode", "m", "goTemplate", "template mode used tod efine the variables. ")
cmd.PersistentFlags().StringVarP(&inputPath, "input", "i", "", "path to the input files that contains the variables")
cmd.PersistentFlags().StringVar(&targetDir, "target", "", "Path to the folder in which the repository will be created")
cmd.PersistentFlags().BoolVar(&debug, "verbose", false,
"verbose logging")
cmd.PersistentFlags().StringVar(&repoPath, "source", "",
"path to the repo i.e. github.com/ivancorrales/go-rest-template)")
cmd.PersistentFlags().StringVarP(&username, "username", "u", "",
"user handle")
cmd.PersistentFlags().StringVarP(&password, "password", "p", "",
"user secret for the provided username")
cmd.PersistentFlags().StringVarP(&templateMode, "mode", "m", "goTemplate",
"template mode used tod efine the variables. ")
cmd.PersistentFlags().StringVarP(&inputPath, "input", "i", "",
"path to the input files that contains the variables")
cmd.PersistentFlags().StringVar(&targetDir, "target", "",
"Path to the folder in which the repository will be created")
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/anotherlife/templatizer
module github.com/wesovilabs/templatizer

go 1.17

Expand Down
15 changes: 12 additions & 3 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
log "github.com/sirupsen/logrus"
)

type Action interface {
Execute() error
}
type action struct {
repoURL string
username string
Expand Down Expand Up @@ -72,11 +75,13 @@ func WithVariables(inputVarsPath string) Option {
log.Fatalf("unexpected error unmarhsaling the input file %s: '%s'", inputVarsPath, err)
}
act.variablesPath = inputVarsPath

}
}

func New(opts ...Option) *action {
/*
New creates an instance of action struct
*/
func New(opts ...Option) Action {
act := &action{
templateMode: "goTemplate",
variablesPath: "tempaltize-vars.yml",
Expand Down Expand Up @@ -106,7 +111,11 @@ func (act *action) Execute() error {
variables := extract(repoFiles, act.templateMode)
if len(variables) > 0 && act.inputVars == nil {
variables.ToYAML(act.variablesPath)
log.Warnf("The repository contains variables that need to be provided.\nPlease, set the values for file '%s' and invoke the command again.", act.variablesPath)
log.Warnf(`
The repository contains variables that need to be provided.
Please set the values for file '%s' and invoke the command again.
`,
act.variablesPath)
return nil
}
log.Debugf("create repository in path %s", act.targetDir)
Expand Down
8 changes: 4 additions & 4 deletions internal/action/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func extract(repoFiles []repoFile, mode string) Variables {
vars = append(vars, v[2])
}
}
all_vars := findMatches(repoFile.content, reVars, reExcluded)
if len(all_vars) > 0 {
allVars := findMatches(repoFile.content, reVars, reExcluded)
if len(allVars) > 0 {
log.Debugf("\tThe file %s contains variables", filepath.Base(repoFile.name))
for _, v := range all_vars {
for _, v := range allVars {
vars = append(vars, v[2])
}
}
Expand All @@ -57,7 +57,7 @@ func findMatches(text string, re *regexp.Regexp, reExclude *regexp.Regexp) [][]s
excludes := reExclude.FindAllStringSubmatch(text, -1)
if len(excludes) > 0 {
for _, v := range excludes {
text = strings.Replace(text, v[0], "", -1)
text = strings.ReplaceAll(text, v[0], "")
}
}
return re.FindAllStringSubmatch(text, -1)
Expand Down
6 changes: 4 additions & 2 deletions internal/action/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func cloneRepositorty(repoURL string, branch string, auth http.AuthMethod) (*git
}
if branch != "" {
log.Debugf("- pull branch %s", branch)
w.Pull(&git.PullOptions{
if err := w.Pull(&git.PullOptions{
SingleBranch: true,
RemoteName: branch,
})
}); err != nil {
log.Warnf("branch %s cannot be pulled by %s", branch, err.Error())
}
}
return w, nil
}
Expand Down
1 change: 0 additions & 1 deletion internal/action/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func processRepositoryFiles(targetDir string, repoFiles []repoFile, path string,
}

func executeTemplate(name, content string, variables interface{}) string {
//t := template.Must(template.New(name).Parse(content))
t, err := template.New(name).Parse(content)
if err != nil {
log.Warn(err)
Expand Down
10 changes: 7 additions & 3 deletions internal/action/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package action
import (
"fmt"
"io/ioutil"
"os"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

Expand All @@ -26,13 +28,15 @@ func varWithFullPath(prefix string, children Variables) []string {
return output
}

func (v Variables) ToYAML(path string) error {
func (v Variables) ToYAML(path string) {
v.pruneVariables()
data, err := yaml.Marshal(&v)
if err != nil {
return err
log.Warnf("it fails silently creating the params file: %s", err.Error())
}
if err := ioutil.WriteFile(path, data, os.ModePerm); err != nil {
log.Warnf("it fails silently creating the params file: %s", err.Error())
}
return ioutil.WriteFile(path, data, 0666)
}

func (v Variables) pruneVariables() {
Expand Down
5 changes: 4 additions & 1 deletion scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
brew install pre-commit

# Install Hadoling
brew install hadolint
brew install hadolint

# Install goreleaser
brew install goreleaser

0 comments on commit 5dd72ef

Please sign in to comment.