Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #117 Specify base go.mod for app creation #119

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ env:
- GO111MODULE=on
install: skip
script:
- GO111MODULE=off go get ./... # to populate $GOPATH/src for tests
- go build ./...
- go test ./...
deploy:
Expand Down
9 changes: 5 additions & 4 deletions api/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var fileSampleEngineMain = filepath.Join("examples", "engine", "main.go")

func CreateProject(basePath, appName, appCfgPath, coreVersion string) (common.AppProject, error) {
func CreateProject(basePath, appName, appCfgPath, modFilePath, coreVersion string) (common.AppProject, error) {

var err error
var appJson string
Expand Down Expand Up @@ -58,7 +58,7 @@ func CreateProject(basePath, appName, appCfgPath, coreVersion string) (common.Ap
fmt.Printf("Setting up app directory: %s\n", appDir)
}

err = setupAppDirectory(dm, appDir, coreVersion)
err = setupAppDirectory(dm, appDir, modFilePath, coreVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func createAppDirectory(basePath, appName string) (string, error) {
}

//setupAppDirectory sets up the flogo app directory
func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {
func setupAppDirectory(dm util.DepManager, appPath, modFilePath, coreVersion string) error {

err := os.Mkdir(filepath.Join(appPath, dirBin), os.ModePerm)
if err != nil {
Expand All @@ -140,7 +140,8 @@ func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {
return err
}

err = dm.Init()
// create go.mod only if modFilePath is not specified or the specified file does not exist
err = dm.Init(modFilePath)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions api/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestCmdCreate_noflag(t *testing.T) {
defer testEnv.cleanup()

t.Logf("Current dir '%s'", testEnv.currentDir)
_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")
assert.Equal(t, nil, err)

_, err = os.Stat(filepath.Join(tempDir, "myApp", "src", "go.mod"))
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestCmdCreate_flag(t *testing.T) {
}
defer file.Close()
fmt.Fprintf(file, jsonString)
_, err = CreateProject(testEnv.currentDir, "flogo", "flogo.json", "")
_, err = CreateProject(testEnv.currentDir, "flogo", "flogo.json", "", "")
assert.Equal(t, nil, err)

_, err = os.Stat(filepath.Join(tempDir, "flogo", "src", "go.mod"))
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestCmdCreate_masterCore(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
os.Chdir(testEnv.currentDir)

_, err = CreateProject(testEnv.currentDir, "myApp", "", "master")
_, err = CreateProject(testEnv.currentDir, "myApp", "", "", "master")
assert.Equal(t, nil, err)
}

Expand Down
8 changes: 4 additions & 4 deletions api/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestInstallLegacyPkg(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "v0.9.2")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "v0.9.2")

assert.Nil(t, err)

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestInstallPkg(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Nil(t, err)

Expand Down Expand Up @@ -168,7 +168,7 @@ func TestInstallPkgWithVersion(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Nil(t, err)

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestListPkg(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
_ = os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Equal(t, nil, err)

Expand Down
4 changes: 2 additions & 2 deletions api/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestListAllContribs(t *testing.T) {
t.Logf("Current dir '%s'", testEnv.currentDir)
os.Chdir(testEnv.currentDir)

_, err := CreateProject(testEnv.currentDir, "myApp", "", "")
_, err := CreateProject(testEnv.currentDir, "myApp", "", "", "")

assert.Equal(t, nil, err)

Expand Down Expand Up @@ -50,7 +50,7 @@ func TestListWithLegacyPkg(t *testing.T) {
}
defer file.Close()
fmt.Fprintf(file, newJsonString)
_, err = CreateProject(testEnv.currentDir, "temp", "flogo.json", "")
_, err = CreateProject(testEnv.currentDir, "temp", "flogo.json", "", "")
assert.Equal(t, nil, err)

err = ListContribs(NewAppProject(filepath.Join(testEnv.currentDir, "temp")), true, "")
Expand Down
6 changes: 3 additions & 3 deletions api/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ var fileSampleShimSupport = filepath.Join("examples", "engine", "shim", fileShim

var flogoImportPattern = regexp.MustCompile(`^(([^ ]*)[ ]+)?([^@:]*)@?([^:]*)?:?(.*)?$`)


type ShimBuilder struct {
appBuilder common.Builder
shim string
shim string
}

func (sb *ShimBuilder) Build(project common.AppProject) error {
Expand All @@ -39,7 +38,8 @@ func (sb *ShimBuilder) Build(project common.AppProject) error {
return err
}

defer shimCleanup(project)
// do not clean up shim source code!
// defer shimCleanup(project)

err = createShimSupportGoFile(project)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var buildCmd = &cobra.Command{
}

api.SetVerbose(verbose)
tempProject, err := api.CreateProject(tempDir, "", flogoJsonFile, "latest")
tempProject, err := api.CreateProject(tempDir, "", flogoJsonFile, "", "latest")
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating temp project: %v\n", err)
os.Exit(1)
Expand Down
4 changes: 3 additions & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

var flogoJsonPath string
var coreVersion string
var modFilePath string

func init() {
CreateCmd.Flags().StringVarP(&flogoJsonPath, "file", "f", "", "specify a flogo.json to create project from")
CreateCmd.Flags().StringVarP(&coreVersion, "cv", "", "", "specify core library version (ex. master)")
CreateCmd.Flags().StringVarP(&modFilePath, "mod", "m", "", "go.mod file to override default settings")
rootCmd.AddCommand(CreateCmd)
}

Expand All @@ -36,7 +38,7 @@ var CreateCmd = &cobra.Command{
fmt.Fprintf(os.Stderr, "Error determining working directory: %v\n", err)
os.Exit(1)
}
_, err = api.CreateProject(currentDir, appName, flogoJsonPath, coreVersion)
_, err = api.CreateProject(currentDir, appName, flogoJsonPath, modFilePath, coreVersion)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating project: %v\n", err)
os.Exit(1)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module github.com/project-flogo/cli

replace go.uber.org/multierr => go.uber.org/multierr v1.6.0

require (
github.com/coreos/go-semver v0.2.0
github.com/msoap/byline v1.1.1
github.com/pkg/errors v0.8.1 // indirect
github.com/project-flogo/core v0.9.5-beta.1
github.com/project-flogo/core v1.2.0
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
)

go 1.12
go 1.14
17 changes: 13 additions & 4 deletions util/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type DepManager interface {
Init() error
Init(string) error
AddDependency(flogoImport Import) error
GetPath(flogoImport Import) (string, error)
AddReplacedContribForBuild() error
Expand All @@ -35,7 +35,16 @@ type ModDepManager struct {
localMods map[string]string
}

func (m *ModDepManager) Init() error {
func (m *ModDepManager) Init(modFilePath string) error {
if len(modFilePath) > 0 {
// try to copy specified go.mod file
err := CopyFile(modFilePath, filepath.Join(m.srcDir, "go.mod"))
if err != nil {
fmt.Printf("WARNING: failed to copy go.mod file %+v\n", err)
} else {
return nil
}
}

err := ExecCmd(exec.Command("go", "mod", "init", "main"), m.srcDir)
if err == nil {
Expand All @@ -55,7 +64,6 @@ func (m *ModDepManager) AddDependency(flogoImport Import) error {
return err
}


err = ExecCmd(exec.Command("go", "mod", "verify"), m.srcDir)
if err == nil {
err = ExecCmd(exec.Command("go", "mod", "download", flogoImport.ModulePath()), m.srcDir)
Expand Down Expand Up @@ -299,7 +307,8 @@ func (m *ModDepManager) AddReplacedContribForBuild() error {
localModules := strings.Split(data[index-1:], "\n")

for _, val := range localModules {
if val != "" {
// check lines starting with 'replace' only
if val != "" && strings.HasPrefix(val, "replace") {
mods := strings.Split(val, " ")
//If the length of mods is more than 4 it contains the versions of package
//so it is stating to use different version of pkg rather than
Expand Down