git.go 3.48 KB
Newer Older
Manuel Pinzón 's avatar
Manuel Pinzón committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
package commands

import (
	"fmt"
	"os"
	"strings"

	"gitlab.com/mifori/envpops/architecture"
	"gitlab.com/mifori/envpops/config/cmdi"
	"gitlab.com/mifori/envpops/config/configuration"
	"gitlab.com/mifori/envpops/config/execi"
	"gitlab.com/mifori/envpops/messages"
)

// Git  depliega los sistemas necesarios para crear un sistema git
func Git(args []string) {
	permitted := []string{"-p", "--help"}
	commands, commandsArgs, initial := check(args, permitted)

	path := configuration.POPSPATH + "/.gitpops"
	configuration.FOLDER = path
	configuration.PROJECT = "git"

	if len(initial) == 0 && len(commands) == 0 {
		if _, err := os.Stat(path); os.IsNotExist(err) {
			cmdi.Mkdir(path)
		}

		architecture.Add("git")
		return
	}

	if len(initial) > 0 {
		if c := configuration.GetConfig(); c.RepositoryType != "local" {
			messages.Exit("git/not_create")
		}

		gitActions(initial, commandsArgs)
	} else {
		gitCommand(commands)
	}
}

//gitActions  realiza las acciones permitidas por git
func gitActions(words []string, commandsArgs map[string][]string) {
	switch words[0] {
	case "add":
		add(words, commandsArgs)
	case "rm":
		remove(words, commandsArgs)
	default:
		messages.Exit("git/unknown")
	}
}

//gitCommand  realiza las acciones relacionadas a un comando
func gitCommand(commands []string) {
	switch commands[0] {
	case "--help":
		fmt.Printf(messages.Read("git/help")+"\n", configuration.PROGRAM)
		os.Exit(0)
	default:
		messages.Exit("git/unknown")
	}
}

//add  crea los repositorios
func add(words []string, commands map[string][]string) {
	typeF := "public"

	private := commands["-p"]
	if private != nil {
		typeF = "private"
		words = append(words, private...)
	}

	if len(words) == 1 {
		messages.Exit("git/add")
	}

	if len(commands) > 1 {
		messages.Exit("git/unknown")
	}

	folder := configuration.FOLDER + "/"
	folders := words[1:]
	repositorio := folder + typeF

	for i := 0; i < len(folders); i++ {
		f := strings.ToLower(folders[i])

		if _, err := os.Stat(folder + "private/" + f + ".git"); !os.IsNotExist(err) {
			fmt.Printf(messages.Read("git/exist"), f)
			continue
		}

		if _, err := os.Stat(folder + "public/" + f + ".git"); !os.IsNotExist(err) {
			fmt.Printf(messages.Read("git/exist"), f)
			continue
		}

		cmdi.MkdirAll(repositorio + "/" + f)
		generate(f, repositorio)

		if typeF == "private" {
			src := repositorio + "/" + f + ".git"
			cmdi.Chown("-R", os.Getenv("USER"), src)
			cmdi.Chmod("-R", "ug+rwX", src)
			cmdi.Exec("find", src, "-type", "d", "-exec",
				"chmod", "g+s", "{}", "+",
			)
		}
	}
}

//remove  elimina los repositorios
func remove(words []string, commands map[string][]string) {
	if len(words) == 1 {
		messages.Exit("git/rm")
	}

	if len(commands) > 0 {
		messages.Exit("git/unknown")
	}

	folders := words[1:]

	for i := 0; i < len(folders); i++ {
		f := strings.ToLower(folders[i])

		cmdi.ExecE("rm", "-r",
			configuration.FOLDER+"/private/"+f+".git",
			configuration.FOLDER+"/public/"+f+".git",
		)
	}
}

//generate  crea un folder git para el servidor
func generate(name, repositorio string) {
	name = strings.ToLower(name)
	newRepository := repositorio + "/" + name

	execi.CommandDir(newRepository, configuration.Permission(),
		"git", "init", "--shared=true")
	execi.CommandDir(newRepository, configuration.Permission(),
		"git", "add", ".")
	execi.CommandDir(newRepository, configuration.Permission(),
		"git", "commit", "-m", "Iniciando Git")
	execi.CommandDir(repositorio, configuration.Permission(),
		"git", "clone", "--bare", name, name+".git")

	cmdi.RemoveFolder(newRepository)
}