added function to get current go module and added test case

This commit is contained in:
2021-08-28 23:32:05 +04:00
parent 5c2b36c086
commit 5823cf2028
4 changed files with 87 additions and 11 deletions

View File

@@ -3,24 +3,28 @@
package generate
import (
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
"github.com/otiai10/copy"
"go/ast"
"go/token"
modfile "golang.org/x/mod/modfile"
"io/ioutil"
"os"
"os/exec"
"strings"
)
type NewProject struct {
Name string
Module string
NewDir string
P2PRCPath string
Option *copy.Options
Token *token.FileSet
AST *ast.File
FileNameAST string
Name string
Module string
NewDir string
P2PRCPath string
CurrentModule string
Option *copy.Options
Token *token.FileSet
AST *ast.File
FileNameAST string
}
// GenerateNewProject creates a new copy of the P2PRC
@@ -54,9 +58,22 @@ func GenerateNewProject(name string, module string) error {
// - remove go.mod and go.sum and create new ones
// Files we require to skip
var Options copy.Options
// IF YOU ARE RELEASING AN EXTENSION OF P2PRC
// MODIFY HERE (AN EXTENSION FROM YOUR EXTENSION :P)
// Skip or have the appropriate files and directories not needed
//----------------------------------------------------------------
// Action performed:
// - Ensuring main.go file exists
// - Skipping all .go files
// - Skipping go.mod file
// - Skipping go.sum file
// - Skipping .idea/ directory
// - Skipping Makefile file
// - Skipping <Project Name>/ directory
//----------------------------------------------------------------
Options.Skip = func(src string) (bool, error) {
switch {
case strings.HasSuffix(src, "main.go"):
@@ -88,6 +105,10 @@ func GenerateNewProject(name string, module string) error {
}
// Creating a new go.mod file in the appropriate directory
err = newProject.CreateGoMod()
if err != nil {
return err
}
return nil
}
@@ -112,3 +133,39 @@ func (a *NewProject)CreateGoMod() error {
}
return nil
}
// ChangeImportFiles Changes Appropriate imports in the appropriate file
func (a *NewProject)ChangeImportFiles() error {
// IF YOU ARE RELEASING AN EXTENSION OF P2PRC
// MODIFY HERE (AN EXTENSION FROM YOUR EXTENSION :P)
//----------------------------------------------------------------
// Action performed:
// Files we would need to modify the imports in
// generate/generate.go -> config module
// cmd/action.go -> config module, server module, generate module
// cmd/flags.go -> config module, server module, generate module
// server/server.go -> config module
// main.go -> cmd module
//-----------------------------------------------------------------
a.FileNameAST = a.NewDir + "generate/generator.go"
err := a.ChangeImports("", a.Module+"config")
if err != nil {
return err
}
return nil
}
// GetCurrentGoModule Gets the current go module name
func (a *NewProject)GetCurrentGoModule() (error) {
goModBytes, err := ioutil.ReadFile(a.P2PRCPath + "go.mod")
if err != nil {
return err
}
modName := modfile.ModulePath(goModBytes)
fmt.Fprintf(os.Stdout, "modName=%+v\n", modName)
// Set current module to struct of file NewProject
a.CurrentModule = modName
return nil
}

View File

@@ -89,4 +89,24 @@ func TestNewProject_CreateGoMod(t *testing.T) {
fmt.Println(err)
t.Error(err)
}
}
// Testing if the current go module is returned
func TestNewProject_GetCurrentGoModule(t *testing.T) {
// Create a new variable of type NewProject
var np NewProject
path, err := config.GetPathP2PRC()
if err != nil {
fmt.Println(err)
t.Error(err)
}
// Set Current project path
np.P2PRCPath = path
// Get module name
err = np.GetCurrentGoModule()
if err != nil {
fmt.Println(err)
t.Error(err)
}
fmt.Println(np.CurrentModule)
}

2
go.mod
View File

@@ -7,7 +7,6 @@ require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/apenella/go-ansible v1.1.0
github.com/containerd/continuity v0.0.0-20210315143101-93e15499afd5 // indirect
github.com/dave/jennifer v1.4.1
github.com/docker/docker v20.10.0-beta1.0.20201113105859-b6bfff2a628f+incompatible
github.com/docker/go-connections v0.4.0
github.com/gin-gonic/gin v1.6.3
@@ -26,6 +25,7 @@ require (
github.com/urfave/cli/v2 v2.3.0
gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40 // indirect
gitlab.com/NebulousLabs/go-upnp v0.0.0-20181011194642-3a71999ed0d3
golang.org/x/mod v0.3.0
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
golang.org/x/text v0.3.5 // indirect

3
go.sum
View File

@@ -177,8 +177,6 @@ github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1S
github.com/d2g/dhcp4client v1.0.0/go.mod h1:j0hNfjhrt2SxUOw55nL0ATM/z4Yt3t2Kd1mW34z5W5s=
github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5/go.mod h1:Eo87+Kg/IX2hfWJfwxMzLyuSZyxSoAug2nGa1G2QAi8=
github.com/d2g/hardwareaddr v0.0.0-20190221164911-e7d9fbe030e4/go.mod h1:bMl4RjIciD2oAxI7DmWRx6gbeqrkoLqv3MV0vzNad+I=
github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw=
github.com/dave/jennifer v1.4.1/go.mod h1:7jEdnm+qBcxl8PC0zyp7vxcpSRnzXSt9r39tpTVGlwA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -657,6 +655,7 @@ golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=