added generate function to the ClI
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||||
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/generate"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/plugin"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/plugin"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/server"
|
||||||
@@ -243,6 +244,24 @@ var CliAction = func(ctx *cli.Context) error {
|
|||||||
client.PrettyPrint(groups)
|
client.PrettyPrint(groups)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// -- REMOVE ON REGULAR RELEASE --
|
||||||
|
// when flag --gen is called an extension
|
||||||
|
// of the project is created to repurpose
|
||||||
|
// the project for custom purpose
|
||||||
|
if Generate != "" {
|
||||||
|
err := generate.GenerateNewProject(Generate)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Created new folder: " + Generate)
|
||||||
|
fmt.Println("1. Enter inside " + Generate + "directory")
|
||||||
|
fmt.Println("2. go mod init <name of your module (ex: github.com/akilan1999/lol)>")
|
||||||
|
fmt.Println("3. sh install.sh " + Generate)
|
||||||
|
fmt.Println("4. ./" + Generate + " -h (This is to test if the binary is working")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//--------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
14
cmd/flags.go
14
cmd/flags.go
@@ -28,6 +28,10 @@ var (
|
|||||||
Groups bool
|
Groups bool
|
||||||
RemoveContainerGroup bool
|
RemoveContainerGroup bool
|
||||||
RemoveGroup string
|
RemoveGroup string
|
||||||
|
// Generate only allowed in dev release
|
||||||
|
// -- REMOVE ON REGULAR RELEASE --
|
||||||
|
Generate string
|
||||||
|
//--------------------------------
|
||||||
)
|
)
|
||||||
|
|
||||||
var AppConfigFlags = []cli.Flag{
|
var AppConfigFlags = []cli.Flag{
|
||||||
@@ -187,4 +191,14 @@ var AppConfigFlags = []cli.Flag{
|
|||||||
EnvVars: []string{"REMOVE_GROUP"},
|
EnvVars: []string{"REMOVE_GROUP"},
|
||||||
Destination: &RemoveGroup,
|
Destination: &RemoveGroup,
|
||||||
},
|
},
|
||||||
|
// Generate only allowed in dev release
|
||||||
|
// -- REMOVE ON REGULAR RELEASE --
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "Generate",
|
||||||
|
Aliases: []string{"gen"},
|
||||||
|
Usage: "Generates a new copy of P2PRC which can be modified based on your needs",
|
||||||
|
EnvVars: []string{"GENERATE"},
|
||||||
|
Destination: &Generate,
|
||||||
|
},
|
||||||
|
//--------------------------------
|
||||||
}
|
}
|
||||||
@@ -8,12 +8,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
defaultPath string
|
defaultPath string
|
||||||
defaults = map[string]interface{}{
|
defaults = map[string]interface{}{}
|
||||||
"IPTable": "/etc/p2p-rendering/ip_table.json",
|
|
||||||
"DockerContainers": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/",
|
|
||||||
"DefaultDockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/",
|
|
||||||
"SpeedTestFile":"/etc/p2p-rendering/50.bin",
|
|
||||||
}
|
|
||||||
configName = "config"
|
configName = "config"
|
||||||
configType = "json"
|
configType = "json"
|
||||||
configFile = "config.json"
|
configFile = "config.json"
|
||||||
@@ -67,18 +62,30 @@ func Copy(src, dst string) error {
|
|||||||
return out.Close()
|
return out.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPathP2PRC Getting P2PRC Directory from environment variable
|
||||||
|
func GetPathP2PRC()(string,error) {
|
||||||
|
curDir := os.Getenv("P2PRC")
|
||||||
|
return curDir + "/", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentPath Getting P2PRC Directory from environment variable
|
||||||
|
func GetCurrentPath()(string,error) {
|
||||||
|
curDir := os.Getenv("PWD")
|
||||||
|
return curDir + "/", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// SetDefaults This function to be called only during a
|
// SetDefaults This function to be called only during a
|
||||||
// make install
|
// make install
|
||||||
func SetDefaults() error {
|
func SetDefaults() error {
|
||||||
//Getting Current Directory from environment variable
|
|
||||||
curDir := os.Getenv("P2PRC")
|
|
||||||
|
|
||||||
//Setting current directory to default path
|
//Setting current directory to default path
|
||||||
defaultPath = curDir + "/"
|
defaultPath, err := GetPathP2PRC()
|
||||||
|
if err != nil{
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
//Creates ip_table.json in the json directory
|
//Creates ip_table.json in the json directory
|
||||||
err := Copy("p2p/ip_table.json","p2p/iptable/ip_table.json")
|
err = Copy("p2p/ip_table.json","p2p/iptable/ip_table.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -129,9 +136,11 @@ func SetDefaults() error {
|
|||||||
|
|
||||||
func ConfigInit()(*Config,error) {
|
func ConfigInit()(*Config,error) {
|
||||||
|
|
||||||
curDir := os.Getenv("P2PRC")
|
|
||||||
//Setting current directory to default path
|
//Setting current directory to default path
|
||||||
defaultPath = curDir + "/"
|
defaultPath, err := GetPathP2PRC()
|
||||||
|
if err != nil{
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
//Paths to search for config file
|
//Paths to search for config file
|
||||||
configPaths = append(configPaths, defaultPath)
|
configPaths = append(configPaths, defaultPath)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,3 +18,21 @@ func TestSetDefaults(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetCurrentPath(t *testing.T) {
|
||||||
|
path, err := GetCurrentPath()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetPathP2PRC(t *testing.T) {
|
||||||
|
path, err := GetPathP2PRC()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(path)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,29 +3,43 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type NewProject struct {
|
||||||
|
Name string
|
||||||
|
NewDir string
|
||||||
|
P2PRCPath string
|
||||||
|
Option *copy.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateNewProject creates a new copy of the P2PRC
|
||||||
|
// project for custom modification
|
||||||
func GenerateNewProject(name string) error {
|
func GenerateNewProject(name string) error {
|
||||||
|
// Create new variable of type NewProject
|
||||||
|
var newProject NewProject
|
||||||
// Get path of the current directory
|
// Get path of the current directory
|
||||||
curDir := os.Getenv("PWD")
|
curDir, err := config.GetCurrentPath()
|
||||||
// Add slash to the end
|
if err != nil {
|
||||||
curDir = curDir + "/"
|
return err
|
||||||
|
}
|
||||||
// Folder name of the new generated project
|
// Folder name of the new generated project
|
||||||
NewProject := curDir + name + "/"
|
newProject.NewDir = curDir + name + "/"
|
||||||
fmt.Println(NewProject)
|
|
||||||
// Create a new folder based on name entered
|
// Create a new folder based on name entered
|
||||||
err := CreateFolder(name, curDir)
|
err = CreateFolder(name, curDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// get path of P2PRC
|
// get path of P2PRC
|
||||||
P2PRCPATH := os.Getenv("P2PRC")
|
P2PRCPATH, err := config.GetPathP2PRC()
|
||||||
// Add slash to the end
|
if err != nil {
|
||||||
P2PRCPATH = P2PRCPATH + "/"
|
return err
|
||||||
|
}
|
||||||
|
// Assign P2PRC path to the newly generated project
|
||||||
|
newProject.P2PRCPath = P2PRCPATH
|
||||||
// Steps:
|
// Steps:
|
||||||
// - copy all files from P2PRC
|
// - copy all files from P2PRC
|
||||||
// - remove go.mod and go.sum and create new ones
|
// - remove go.mod and go.sum and create new ones
|
||||||
@@ -33,13 +47,21 @@ func GenerateNewProject(name string) error {
|
|||||||
// Files we require to skip
|
// Files we require to skip
|
||||||
var Options copy.Options
|
var Options copy.Options
|
||||||
|
|
||||||
// Skips the appropriate files and directories not needed
|
// Skip or have the appropriate files and directories not needed
|
||||||
Options.Skip = func(src string) (bool, error) {
|
Options.Skip = func(src string) (bool, error) {
|
||||||
switch {
|
switch {
|
||||||
|
case strings.HasSuffix(src, "main.go"):
|
||||||
|
return false, nil
|
||||||
|
case strings.HasSuffix(src, ".go"):
|
||||||
|
return true, nil
|
||||||
case strings.HasSuffix(src, "go.mod"):
|
case strings.HasSuffix(src, "go.mod"):
|
||||||
return true, nil
|
return true, nil
|
||||||
case strings.HasSuffix(src, "go.sum"):
|
case strings.HasSuffix(src, "go.sum"):
|
||||||
return true, nil
|
return true, nil
|
||||||
|
case strings.HasSuffix(src, ".idea"):
|
||||||
|
return true, nil
|
||||||
|
case strings.HasSuffix(src, "Makefile"):
|
||||||
|
return true, nil
|
||||||
case strings.HasSuffix(src, name):
|
case strings.HasSuffix(src, name):
|
||||||
return true, nil
|
return true, nil
|
||||||
default:
|
default:
|
||||||
@@ -47,16 +69,14 @@ func GenerateNewProject(name string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Storing type option in the struct new project
|
||||||
|
newProject.Option = &Options
|
||||||
|
|
||||||
// Copies all files from P2PRC to the new project created
|
// Copies all files from P2PRC to the new project created
|
||||||
err = copy.Copy(P2PRCPATH, NewProject,Options)
|
err = copy.Copy(newProject.P2PRCPath, newProject.NewDir,*newProject.Option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Installing new project
|
|
||||||
//cmd := exec.Command("sh","install.sh",name)
|
|
||||||
//if err := cmd.Run(); err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user