<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"strings"
	"github.com/codegangsta/cli"
	"github.com/hoodiez/kapal/btrfs"
)

func CreateVolume (pool string, name string, dockerize bool, dockername string, dockervol string) {
	err := btrfscmd.SubvolumeCreate(pool,name)
	if err != nil {
		fmt.Errorf("Failed creating Volume in the file system")
	} else {
		/*Todo: should be using Docker API and or it's own wrapper Lib*/
		if dockerize {
			var cmd *exec.Cmd
			var containervol string
			if dockervol != "" {
				containervol = dockervol
				if strings.HasPrefix(containervol, "/") == false {
					containervol = "/"+containervol
				}
			} else {
				containervol = "/data"
			}
			if dockername != "" {
				cmd = exec.Command("docker","create","-v",path.Join(pool,name)+":"+containervol,"--name",dockername,"ubuntu")
			} else {
				cmd = exec.Command("docker","create","-v",path.Join(pool,name)+":"+containervol,"ubuntu")
			}
			cmd.Run()
		}
	}
}

func main() {
	version := "0.0.1"
	app := cli.NewApp()
	app.Name = "kapal"
	app.Usage = "Linux Container data orchestration tool"
	app.Version = version 
	app.Authors = []cli.Author{cli.Author{Name: "hoodiez", Email:"https://github.com/hoodiez"},cli.Author{Name: "jteso",Email: "https://github.com/jteso"}}
	app.Commands = []cli.Command {
		{
			Name: "pool",
			Aliases: []string{"p"},
			Usage: "Manage storage pools",
			Subcommands: []cli.Command {
			 {
			 	Name: "create",
				Usage: "Create new storage pool from a device",
				Action: func(c *cli.Context) {
					fmt.Println("Add Storage Pool: ", c.String("device"), c.String("mount"))
				},
				Flags: []cli.Flag {
				 cli.StringFlag{
				  Name: "device, d",
				  Usage: "device name, e.g. /dev/sdb",
				 },
				 cli.StringFlag{
				  Name: "mount, m",
				  Usage: "mount point path of the storage pool, e.g. /var/lib/kapal",
				 },
				},
			 },
			 {
				Name: "list",
				Usage: "List storage pools that can be used or managed by kapal",
				Action: func(c *cli.Context) {
                                        fmt.Println("List Storage Pool: ")
                                },
			 },
			},
		},
		{
			Name: "volume",
			Aliases: []string{"vol"},
			Usage: "Manage volumes in a storage pool, such as create, clone, remove, etc.",
			Subcommands: []cli.Command {
			{
                                Name: "create",
                                Usage: "Create a new volume in a pool",
                                Action: func(c *cli.Context) {
                                        fmt.Println("Create Volume: ", c.String("pool"), c.String("name"))
					CreateVolume(c.String("pool"),c.String("name"),c.Bool("dockerize"),c.String("dockername"),c.String("dockervol"))
                                },
                                Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                 cli.StringFlag{
                                  Name: "name, n",
                                  Usage: "name of the volume, e.g. vol01",
                                 },
				 cli.BoolFlag{
                                  Name: "dockerize, d",
                                  Usage: "Also create a docker data volume container, default is false",
                                 },
                                 cli.StringFlag{
                                  Name: "dockername",
                                  Usage: "Name of docker data volume container, default will use docker automatic naming",
                                 },
                                 cli.StringFlag{
                                  Name: "dockervol",
                                  Usage: "Docker Volume path mounted inside the container, default is /data",
                                 },
                                },
                         },
                         {
                                Name: "list",
                                Usage: "List volumes in a pool",
                                Action: func(c *cli.Context) {
                                        fmt.Println("List Volumes: ", c.String("pool"))
                                },
				Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                },
                         },
		         {
                                Name: "remove",
                                Usage: "Remove a volume in a pool",
                                Action: func(c *cli.Context) {
                                        fmt.Println("Remove Volume: ", c.String("pool"))
                                },      
                                Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                 cli.StringFlag{
                                  Name: "name, n",
                                  Usage: "name of the volume, e.g. vol01",
                                 },
                                },
                         },
			 {
				Name: "clone",
				Usage: "Clone a volume into a another volume in a pool",
				Action: func(c *cli.Context) {
                                        fmt.Println("Clone Volume: ", c.String("pool"),c.String("source"),c.String("target"))
                                },
                                Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                 cli.StringFlag{
                                  Name: "source, s",
                                  Usage: "source volume name",
                                 },
				 cli.StringFlag{
				  Name: "target, t",
				  Usage: "target clone volume name",
				 },
				 cli.BoolFlag{
				  Name: "readonly, r",
				  Usage: "make target clone volume readonly, default is false",
                                 },
                                 cli.BoolFlag{
                                  Name: "dockerize, d",
                                  Usage: "Also create a docker data volume container, default is false",<commit_msg>Initial Implementation of Clone volume<commit_after>package main

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"strings"
	"github.com/codegangsta/cli"
	"github.com/hoodiez/kapal/btrfs"
)

func CreateVolume (pool string, name string, dockerize bool, dockername string, dockervol string) {
	err := btrfscmd.SubvolumeCreate(pool,name)
	if err != nil {
		fmt.Println("Failed creating Volume in the file system")
	} else {
		/*Todo: should be using Docker API and or it's own wrapper Lib*/
		if dockerize {
			var cmd *exec.Cmd
			var containervol string
			if dockervol != "" {
				containervol = dockervol
				if strings.HasPrefix(containervol, "/") == false {
					containervol = "/"+containervol
				}
			} else {
				containervol = "/data"
			}
			if dockername != "" {
				cmd = exec.Command("docker","create","-v",path.Join(pool,name)+":"+containervol,"--name",dockername,"ubuntu")
			} else {
				cmd = exec.Command("docker","create","-v",path.Join(pool,name)+":"+containervol,"ubuntu")
			}
			err2 := cmd.Run()
			if err2 != nil {
				fmt.Println("Error creating Docker Data Volume Container")
			}
			
		}
	}
}

func CloneVolume (pool string, source string, target string, readonly bool, dockerize bool, dockername string, dockervol string) {
	err := btrfscmd.SubvolumeSnapshot(pool,source,target,readonly)
	if err != nil {
                fmt.Println("Failed cloning Volume in the file system")
        } else {
                /*Todo: should be using Docker API and or it's own wrapper Lib*/
                if dockerize {
                        var cmd *exec.Cmd
                        var containervol string
                        if dockervol != "" {
                                containervol = dockervol
                                if strings.HasPrefix(containervol, "/") == false {
                                        containervol = "/"+containervol
                                }
                        } else {
                                containervol = "/data"
                        }
                        if dockername != "" {
                                cmd = exec.Command("docker","create","-v",path.Join(pool,target)+":"+containervol,"--name",dockername,"ubuntu")
                        } else {
                                cmd = exec.Command("docker","create","-v",path.Join(pool,target)+":"+containervol,"ubuntu")
                        }
                        err2 := cmd.Run()
                        if err2 != nil {
                                fmt.Println("Error creating Docker Data Volume Container")
                        }

                }
        }
}

func main() {
	version := "0.0.1"
	app := cli.NewApp()
	app.Name = "kapal"
	app.Usage = "Linux Container data orchestration tool"
	app.Version = version 
	app.Authors = []cli.Author{cli.Author{Name: "hoodiez", Email:"https://github.com/hoodiez"},cli.Author{Name: "jteso",Email: "https://github.com/jteso"}}
	app.Commands = []cli.Command {
		{
			Name: "pool",
			Aliases: []string{"p"},
			Usage: "Manage storage pools",
			Subcommands: []cli.Command {
			 {
			 	Name: "create",
				Usage: "Create new storage pool from a device",
				Action: func(c *cli.Context) {
					fmt.Println("Add Storage Pool: ", c.String("device"), c.String("mount"))
				},
				Flags: []cli.Flag {
				 cli.StringFlag{
				  Name: "device, d",
				  Usage: "device name, e.g. /dev/sdb",
				 },
				 cli.StringFlag{
				  Name: "mount, m",
				  Usage: "mount point path of the storage pool, e.g. /var/lib/kapal",
				 },
				},
			 },
			 {
				Name: "list",
				Usage: "List storage pools that can be used or managed by kapal",
				Action: func(c *cli.Context) {
                                        fmt.Println("List Storage Pool: ")
                                },
			 },
			},
		},
		{
			Name: "volume",
			Aliases: []string{"vol"},
			Usage: "Manage volumes in a storage pool, such as create, clone, remove, etc.",
			Subcommands: []cli.Command {
			{
                                Name: "create",
                                Usage: "Create a new volume in a pool",
                                Action: func(c *cli.Context) {
                                        fmt.Println("Create Volume: ", c.String("pool"), c.String("name"))
					CreateVolume(c.String("pool"),c.String("name"),c.Bool("dockerize"),c.String("dockername"),c.String("dockervol"))
                                },
                                Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                 cli.StringFlag{
                                  Name: "name, n",
                                  Usage: "name of the volume, e.g. vol01",
                                 },
				 cli.BoolFlag{
                                  Name: "dockerize, d",
                                  Usage: "Also create a docker data volume container, default is false",
                                 },
                                 cli.StringFlag{
                                  Name: "dockername",
                                  Usage: "Name of docker data volume container, default will use docker automatic naming",
                                 },
                                 cli.StringFlag{
                                  Name: "dockervol",
                                  Usage: "Docker Volume path mounted inside the container, default is /data",
                                 },
                                },
                         },
                         {
                                Name: "list",
                                Usage: "List volumes in a pool",
                                Action: func(c *cli.Context) {
                                        fmt.Println("List Volumes: ", c.String("pool"))
                                },
				Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                },
                         },
		         {
                                Name: "remove",
                                Usage: "Remove a volume in a pool",
                                Action: func(c *cli.Context) {
                                        fmt.Println("Remove Volume: ", c.String("pool"))
                                },      
                                Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                 cli.StringFlag{
                                  Name: "name, n",
                                  Usage: "name of the volume, e.g. vol01",
                                 },
                                },
                         },
			 {
				Name: "clone",
				Usage: "Clone a volume into a another volume in a pool",
				Action: func(c *cli.Context) {
                                        fmt.Println("Clone Volume: ", c.String("pool"),c.String("source"),c.String("target"))
					CloneVolume(c.String("pool"),c.String("source"),c.String("target"),c.Bool("readonly"),c.Bool("dockerize"),c.String("dockername"),c.String("dockervol"))
                                },
                                Flags: []cli.Flag {
                                 cli.StringFlag{
                                  Name: "pool, p",
                                  Usage: "pool mount point, e.g. /var/lib/kapal",
                                 },
                                 cli.StringFlag{
                                  Name: "source, s",
                                  Usage: "source volume name",
                                 },
				 cli.StringFlag{
				  Name: "target, t",
				  Usage: "target clone volume name",
				 },
				 cli.BoolFlag{
				  Name: "readonly, r",
				  Usage: "make target clone volume readonly, default is false",
                                 },
                                 cli.BoolFlag{
                                  Name: "dockerize, d",
                                  Usage: "Also create a docker data volume container, default is false",
{'fraction_non_alphanumeric': 0.11592306685856339, 'fraction_numerical': 0.0011775480832133978, 'mean_word_length': 1.0480975348338692, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 80, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '689687', 'n_tokens_mistral': 4398, 'n_tokens_neox': 3733, 'n_words': 927}