buit base functions

This commit is contained in:
2021-08-25 13:27:31 +04:00
parent 1a7035164b
commit e5334cbd3c
61 changed files with 124 additions and 908 deletions

72
generate/generate.go Normal file
View File

@@ -0,0 +1,72 @@
// Package generate The purpose of this package is to ensure that we can extend the use-case of P2PRC.
// We will create a project directory with the template to extend the use-case of P2PRC
package generate
import (
"fmt"
"github.com/otiai10/copy"
"os"
"strings"
)
func GenerateNewProject(name string) error {
// Get path of the current directory
curDir := os.Getenv("PWD")
// Add slash to the end
curDir = curDir + "/"
// Folder name of the new generated project
NewProject := curDir + name + "/"
fmt.Println(NewProject)
// Create a new folder based on name entered
err := CreateFolder(name, curDir)
if err != nil {
return err
}
// get path of P2PRC
P2PRCPATH := os.Getenv("P2PRC")
// Add slash to the end
P2PRCPATH = P2PRCPATH + "/"
// Steps:
// - copy all files from P2PRC
// - remove go.mod and go.sum and create new ones
// Files we require to skip
var Options copy.Options
// Skips the appropriate files and directories not needed
Options.Skip = func(src string) (bool, error) {
switch {
case strings.HasSuffix(src, "go.mod"):
return true, nil
case strings.HasSuffix(src, "go.sum"):
return true, nil
case strings.HasSuffix(src, name):
return true, nil
default:
return false, nil
}
}
// Copies all files from P2PRC to the new project created
err = copy.Copy(P2PRCPATH, NewProject,Options)
if err != nil {
return err
}
// Installing new project
//cmd := exec.Command("sh","install.sh",name)
//if err := cmd.Run(); err != nil {
// return err
//}
return nil
}
// CreateFolder Creates a new folder based on the name and path provided
func CreateFolder(name string,path string) error {
//Create a folder/directory at a full qualified path
err := os.Mkdir(path + name, 0755)
if err != nil {
return err
}
return nil
}

26
generate/generate_test.go Normal file
View File

@@ -0,0 +1,26 @@
package generate
import (
"fmt"
"testing"
)
// Tests the create folder function creates a folder
// This test will create a folder in the temporary
// directory
func TestCreateFolder(t *testing.T) {
err := CreateFolder("test", "/tmp/")
if err != nil {
t.Error(err)
}
}
// Testing if a new project is created successfully
func TestGenerateNewProject(t *testing.T) {
// Checking if a new project is created successfully
err := GenerateNewProject("p2prctest")
if err != nil {
fmt.Println(err)
t.Error(err)
}
}