Skip to content

Commit

Permalink
feat: add --inline flag (#56)
Browse files Browse the repository at this point in the history
Create files of selected template in current directory instead of a newly created subdirectory
  • Loading branch information
prskr committed Oct 8, 2023
1 parent 6ec2db9 commit ec1991d
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 53 deletions.
32 changes: 14 additions & 18 deletions internal/cloner/gitCloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"

"github.com/denizgursoy/gotouch/internal/auth"
"github.com/denizgursoy/gotouch/internal/logger"
Expand All @@ -35,41 +34,38 @@ func newCloner() Cloner {
}

func (g *gitCloner) CloneFromUrl(rawUrl, branchName string) error {
projectName := g.Store.GetValue(store.ProjectName)
projectFullPath := g.Store.GetValue(store.ProjectFullPath)

var name plumbing.ReferenceName
if len(strings.TrimSpace(branchName)) != 0 {
name = plumbing.NewBranchReferenceName(branchName)
g.Logger.LogInfo(fmt.Sprintf("Cloning branch %s from -> %s", branchName, rawUrl))
} else {
g.Logger.LogInfo("Cloning repository -> " + rawUrl)
cloneOptions := &git.CloneOptions{
Depth: 1,
URL: rawUrl,
Progress: os.Stdout,
}

gitURL, urlParseError := url.Parse(rawUrl)
if urlParseError != nil {
return urlParseError
}

var gitAuth transport.AuthMethod

switch gitURL.Scheme {
case "http", "https":
gitAuth = auth.NewGitNetrcHTTPAuth()
cloneOptions.Auth = auth.NewGitNetrcHTTPAuth()
}

cloneOptions := &git.CloneOptions{
Auth: gitAuth,
URL: rawUrl,
Progress: os.Stdout,
ReferenceName: name,
if len(strings.TrimSpace(branchName)) != 0 {
cloneOptions.ReferenceName = plumbing.NewBranchReferenceName(branchName)
cloneOptions.SingleBranch = true
g.Logger.LogInfo(fmt.Sprintf("Cloning branch %s from -> %s", branchName, rawUrl))
} else {
g.Logger.LogInfo("Cloning repository -> " + rawUrl)
}

_, err := git.PlainClone(projectName, false, cloneOptions)
_, err := git.PlainClone(projectFullPath, false, cloneOptions)
if err != nil {
return err
}

gitDirectory := projectName + string(filepath.Separator) + GitDirectory
gitDirectory := filepath.Join(projectFullPath, GitDirectory)
if err = os.RemoveAll(gitDirectory); err != nil {
return err
}
Expand Down
15 changes: 13 additions & 2 deletions internal/commands/createCommand.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"strconv"
"strings"

"github.com/denizgursoy/gotouch/internal/cloner"
Expand All @@ -9,18 +10,20 @@ import (

"github.com/denizgursoy/gotouch/internal/operator"

"github.com/spf13/cobra"

"github.com/denizgursoy/gotouch/internal/compressor"
"github.com/denizgursoy/gotouch/internal/executor"
"github.com/denizgursoy/gotouch/internal/lister"
"github.com/denizgursoy/gotouch/internal/logger"
"github.com/denizgursoy/gotouch/internal/manager"
"github.com/denizgursoy/gotouch/internal/prompter"
"github.com/denizgursoy/gotouch/internal/store"
"github.com/spf13/cobra"
)

const (
FileFlagName = "file"
FileFlagName = "file"
InlineFlagName = "inline"
)

type (
Expand All @@ -43,7 +46,15 @@ func GetCreateCommandHandler(cmdr operator.Operator) CommandHandler {
pointer = nil
}

inline, inlineError := flags.GetBool(InlineFlagName)
if inlineError != nil {
newLogger.LogErrorIfExists(inlineError)
return
}

appStore := store.GetInstance()
appStore.SetValue(store.Inline, strconv.FormatBool(inline))

options := operator.CreateNewProjectOptions{
Lister: lister.GetInstance(),
Prompter: prompter.GetInstance(),
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/denizgursoy/gotouch/internal/config"
"github.com/denizgursoy/gotouch/internal/logger"

"github.com/denizgursoy/gotouch/internal/operator"
"github.com/spf13/cobra"

"github.com/denizgursoy/gotouch/internal/operator"
)

type BuildInfo struct {
Expand Down Expand Up @@ -49,6 +50,7 @@ Gotouch will ask the questions in the selected project structure in order.`,
Run: GetCreateCommandHandler(cmdr),
}
createCommand.Flags().StringP(FileFlagName, "f", "", "input properties yaml")
createCommand.Flags().BoolP(InlineFlagName, "i", false, "create files in the current directory instead of creating a subdirectory")

createCommand.AddCommand(CreatePackageCommand(cmdr))
createCommand.AddCommand(CreateValidateCommand(cmdr))
Expand Down
7 changes: 6 additions & 1 deletion internal/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package commands
import (
"testing"

"github.com/denizgursoy/gotouch/internal/operator"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"

"github.com/denizgursoy/gotouch/internal/operator"
)

type (
Expand All @@ -30,6 +31,10 @@ func TestCreateRootCommand(t *testing.T) {
shortName: "f",
name: "file",
},
{
shortName: "i",
name: "inline",
},
},
}

Expand Down
64 changes: 44 additions & 20 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/denizgursoy/gotouch/internal/requirements"

"github.com/denizgursoy/gotouch/internal/config"
"github.com/denizgursoy/gotouch/internal/requirements"

"github.com/stretchr/testify/suite"
)
Expand All @@ -35,6 +35,7 @@ type ZippingTestSuite struct {
binaryPath string
binaryDir string
createdProjectPath string
inline bool
}

func TestUnzipping(t *testing.T) {
Expand All @@ -45,15 +46,19 @@ func (z *ZippingTestSuite) SetupSuite() {
err := os.Chdir("../../")
getwd, _ := os.Getwd()
z.binaryDir = getwd
z.binaryPath = getwd + "/gotouch-" + runtime.GOOS
z.binaryPath = filepath.Join(getwd, "gotouch-"+runtime.GOOS)
z.Nil(err, "could not change directory")
}

func (z *ZippingTestSuite) SetupTest() {
mkdirTemp, _ := os.MkdirTemp("", "gotouch-test*")
z.createdProjectPath = mkdirTemp + "/" + "testapp"
mkdirTemp, _ := os.MkdirTemp(z.T().TempDir(), "gotouch-test*")

z.createdProjectPath = filepath.Join(mkdirTemp, "testapp")
z.workingDir = mkdirTemp

// reset inline
z.inline = false

err := os.Chdir(mkdirTemp)
if err != nil {
log.Fatalln("could not change directory")
Expand Down Expand Up @@ -144,6 +149,15 @@ func (z *ZippingTestSuite) TestProjectWithFilesAndDependencies() {
z.checkFileContent("values.txt", "values.txt")
}

func (z *ZippingTestSuite) TestProjectWithFilesInline() {
z.setInputFile("project-with-files-inline.txt")
z.setInline()
z.executeGotouch()

z.checkFileContent("Dockerfile", "Dockerfile")
z.checkFileContent("values.txt", "values.txt")
}

func (z *ZippingTestSuite) TestProjectWithPropertiesYaml() {
z.setInputFile("project-with-files-and-dependencies.txt")
z.executeGotouch()
Expand Down Expand Up @@ -182,39 +196,43 @@ func (z *ZippingTestSuite) checkDefaultProjectStructure() {
}

func (z *ZippingTestSuite) checkFileExists(fileName string, exists bool) {
actualFilePath := fmt.Sprintf("%s/%s", z.createdProjectPath, fileName)
_, err2 := os.Stat(actualFilePath)
_, err2 := os.Stat(filepath.Join(z.createdProjectPath, fileName))
if exists {
z.Nil(err2)
z.NoError(err2)
} else {
z.NotNil(err2)
z.Error(err2)
}
}

func (z *ZippingTestSuite) checkFilesExist(files []string) {
for _, file := range files {
stat, err := os.Stat(fmt.Sprintf("%s/%s", z.createdProjectPath, file))
z.Nil(err, "%s does not exists", file)
z.False(stat.IsDir(), "%s does not exists", file)
z.FileExists(filepath.Join(z.createdProjectPath, file))
}
}

func (z *ZippingTestSuite) checkFileContent(fileName, expectedFile string) {
actualFilePath := fmt.Sprintf("%s/%s", z.createdProjectPath, fileName)
expectedFilePath := fmt.Sprintf("%s/internal/testdata/%s", z.binaryDir, expectedFile)
var actualFilePath string
if z.inline {
dir, _ := filepath.Split(z.createdProjectPath)
actualFilePath = filepath.Join(dir, fileName)
} else {
actualFilePath = filepath.Join(z.createdProjectPath, fileName)
}

expectedFilePath := filepath.Join(z.binaryDir, "internal", "testdata", expectedFile)
z.checkFileContentsWithAbsPath(actualFilePath, expectedFilePath)
}

func (z *ZippingTestSuite) checkFileContentsWithAbsPath(actualFilePath, expectedFilePath string) {
actualFileContent, err := os.ReadFile(actualFilePath)
z.Nil(err)
z.NoError(err)
expectedFileContent, err := os.ReadFile(expectedFilePath)
z.Nil(err)
z.NoError(err)
z.EqualValues(actualFileContent, expectedFileContent)
}

func (z *ZippingTestSuite) checkModuleName(expectedModuleName string, dependencies []string) {
open, err := os.ReadFile(fmt.Sprintf("%s/go.mod", z.createdProjectPath))
open, err := os.ReadFile(filepath.Join(z.createdProjectPath, "go.mod"))
z.Nil(err, "go module file not found")

moduleContent := string(open)
Expand All @@ -229,7 +247,7 @@ func (z *ZippingTestSuite) checkModuleName(expectedModuleName string, dependenci

func (z *ZippingTestSuite) checkDirectoriesExist(directories []string) {
for _, directory := range directories {
directoryPath := fmt.Sprintf("%s/%s", z.createdProjectPath, directory)
directoryPath := filepath.Join(z.createdProjectPath, directory)
stat, err := os.Stat(directoryPath)
z.Nil(err, "%s does not exists", directory)
z.True(stat.IsDir(), "%s does not exists", directory)
Expand Down Expand Up @@ -260,6 +278,9 @@ func (z *ZippingTestSuite) CmdExec(args ...string) {
func (z *ZippingTestSuite) executeGotouch() {
args := make([]string, 0)
args = append(args, z.binaryPath, "-f", PropertiesUrl)
if z.inline {
args = append(args, "-i")
}
z.CmdExec(args...)
}

Expand All @@ -271,6 +292,9 @@ func (z *ZippingTestSuite) executeGotouchWithArgs(gotouchArgs ...string) {
}

func (z *ZippingTestSuite) setInputFile(fileName string) {
source := fmt.Sprintf("%s/internal/testdata/%s", z.binaryDir, fileName)
file = source
file = filepath.Join(z.binaryDir, "internal", "testdata", fileName)
}

func (z *ZippingTestSuite) setInline() {
z.inline = true
}
37 changes: 28 additions & 9 deletions internal/requirements/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/go-playground/validator/v10"
"golang.org/x/mod/module"

"github.com/denizgursoy/gotouch/internal/logger"
"github.com/denizgursoy/gotouch/internal/manager"
"github.com/denizgursoy/gotouch/internal/model"
"github.com/denizgursoy/gotouch/internal/prompter"
"github.com/denizgursoy/gotouch/internal/store"
"github.com/go-playground/validator/v10"
"golang.org/x/mod/module"
)

type (
Expand Down Expand Up @@ -69,17 +71,25 @@ func (p *projectNameTask) Complete() error {
p.Store.SetValue(store.ModuleName, p.ModuleName)
p.Store.SetValue(store.ProjectName, projectName)

inline, inlineParseError := strconv.ParseBool(p.Store.GetValue(store.Inline))
if inlineParseError != nil {
return inlineParseError
}

workingDirectory := p.Manager.GetExtractLocation()
projectFullPath := fmt.Sprintf("%s/%s", workingDirectory, projectName)
dirCreationErr := p.Manager.CreateDirectoryIfNotExist(projectFullPath)
projectFullPath := workingDirectory
if !inline {
projectFullPath = filepath.Join(workingDirectory, projectName)

dirCreationErr := p.Manager.CreateDirectoryIfNotExist(projectFullPath)
if dirCreationErr != nil {
return dirCreationErr
}
}

p.Store.SetValue(store.WorkingDirectory, workingDirectory)
p.Store.SetValue(store.ProjectFullPath, projectFullPath)

if dirCreationErr != nil {
return dirCreationErr
}

p.Logger.LogInfo(fmt.Sprintf("%s is created", projectFullPath))
return nil
}
Expand All @@ -98,9 +108,18 @@ func (p *ProjectNameRequirement) validateModuleName(name any) error {
}
}

inline, parseBoolError := strconv.ParseBool(p.Store.GetValue(store.Inline))
if parseBoolError != nil {
return parseBoolError
}

if inline {
return nil
}

projectName := filepath.Base(moduleName)
workingDirectory := p.Manager.GetExtractLocation()
projectFullPath := fmt.Sprintf("%s/%s", workingDirectory, projectName)
projectFullPath := filepath.Join(workingDirectory, projectName)
if _, err := os.Stat(projectFullPath); !os.IsNotExist(err) {
return fmt.Errorf("directory %s exists, select another name", projectName)
}
Expand Down
Loading

0 comments on commit ec1991d

Please sign in to comment.