diff --git a/cmd/action.go b/cmd/action.go index b8807b2..8665f07 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -4,6 +4,7 @@ import ( "fmt" "git.sr.ht/~akilan1999/p2p-rendering-computation/client" "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/plugin" "git.sr.ht/~akilan1999/p2p-rendering-computation/server" @@ -243,6 +244,24 @@ var CliAction = func(ctx *cli.Context) error { 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 ") + fmt.Println("3. sh install.sh " + Generate) + fmt.Println("4. ./" + Generate + " -h (This is to test if the binary is working") + } + } + //-------------------------------- + return nil diff --git a/cmd/flags.go b/cmd/flags.go index 0fe8fc9..07ec2a5 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -28,6 +28,10 @@ var ( Groups bool RemoveContainerGroup bool RemoveGroup string + // Generate only allowed in dev release + // -- REMOVE ON REGULAR RELEASE -- + Generate string + //-------------------------------- ) var AppConfigFlags = []cli.Flag{ @@ -187,4 +191,14 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"REMOVE_GROUP"}, 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, + }, + //-------------------------------- } \ No newline at end of file diff --git a/config/config.go b/config/config.go index 72c34a7..15e6f9b 100644 --- a/config/config.go +++ b/config/config.go @@ -8,12 +8,7 @@ import ( var ( defaultPath string - 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", - } + defaults = map[string]interface{}{} configName = "config" configType = "json" configFile = "config.json" @@ -67,18 +62,30 @@ func Copy(src, dst string) error { 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 // make install func SetDefaults() error { - //Getting Current Directory from environment variable - curDir := os.Getenv("P2PRC") - //Setting current directory to default path - defaultPath = curDir + "/" + defaultPath, err := GetPathP2PRC() + if err != nil{ + return err + } //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 { return err } @@ -129,9 +136,11 @@ func SetDefaults() error { func ConfigInit()(*Config,error) { - curDir := os.Getenv("P2PRC") //Setting current directory to default path - defaultPath = curDir + "/" + defaultPath, err := GetPathP2PRC() + if err != nil{ + return nil, err + } //Paths to search for config file configPaths = append(configPaths, defaultPath) diff --git a/config/config_test.go b/config/config_test.go index af4e5d9..c88ffb8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "testing" ) @@ -17,3 +18,21 @@ func TestSetDefaults(t *testing.T) { 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) +} diff --git a/generate/generate.go b/generate/generate.go index b65824c..42434a4 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -3,29 +3,43 @@ package generate import ( - "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/config" "github.com/otiai10/copy" "os" "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 { + // Create new variable of type NewProject + var newProject NewProject // Get path of the current directory - curDir := os.Getenv("PWD") - // Add slash to the end - curDir = curDir + "/" + curDir, err := config.GetCurrentPath() + if err != nil { + return err + } // Folder name of the new generated project - NewProject := curDir + name + "/" - fmt.Println(NewProject) + newProject.NewDir = curDir + name + "/" // Create a new folder based on name entered - err := CreateFolder(name, curDir) + err = CreateFolder(name, curDir) if err != nil { return err } // get path of P2PRC - P2PRCPATH := os.Getenv("P2PRC") - // Add slash to the end - P2PRCPATH = P2PRCPATH + "/" + P2PRCPATH, err := config.GetPathP2PRC() + if err != nil { + return err + } + // Assign P2PRC path to the newly generated project + newProject.P2PRCPath = P2PRCPATH // Steps: // - copy all files from P2PRC // - remove go.mod and go.sum and create new ones @@ -33,13 +47,21 @@ func GenerateNewProject(name string) error { // Files we require to skip 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) { switch { + case strings.HasSuffix(src, "main.go"): + return false, nil + case strings.HasSuffix(src, ".go"): + return true, nil case strings.HasSuffix(src, "go.mod"): return true, nil case strings.HasSuffix(src, "go.sum"): return true, nil + case strings.HasSuffix(src, ".idea"): + return true, nil + case strings.HasSuffix(src, "Makefile"): + return true, nil case strings.HasSuffix(src, name): return true, nil 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 - err = copy.Copy(P2PRCPATH, NewProject,Options) + err = copy.Copy(newProject.P2PRCPath, newProject.NewDir,*newProject.Option) 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 }