Example #1
0
File: get.go Project: litixsoft/lxb
// get install all dependencies and rebuild vendors.
func get(c *taskRunner.Context) {

	// Dependencies for install to GOPATH as string array
	depends := []string{}

	// Install all depends
	log.Println("Install dependencies into GOPATH.")
	for _, url := range depends {
		log.Println("Get <- ", url)
		if err := utils.CmdRun("go", "get", "-u", url); err != nil {
			log.Fatal(err)
		}
	}

	// Check if manifest exists
	ok, err := utils.PathExists(filepath.Join(c.Root, "vendor", "manifest"))
	if err != nil {
		log.Fatal(err)
	}

	// If manifest exists, fetch vendors with gvt
	if ok {
		log.Println("Install vendors into project.")
		if err := utils.CmdRun("gvt", "rebuild", "-connections 4"); err != nil {
			log.Fatal(err)
		}
	}
}
Example #2
0
// Run the binary in binPath
func Run(binPath string, binary string, args []string) error {

	// If windows add .exe to binary name
	if runtime.GOOS == "windows" {
		binary += ".exe"
	}

	// Set and check binFilePath
	binFilePath := filepath.Join(binPath, binary)
	ok, err := utils.PathExists(binFilePath)
	if err != nil {
		return err
	}

	// not found
	if !ok {
		return fmt.Errorf("Binary %s not found", binFilePath)
	}

	// Start the binary
	if err := utils.CmdRun(binFilePath, args...); err != nil {
		return err
	}

	return nil
}
Example #3
0
// buildAllPackages builds each package without vendor packages.
func buildAllPackages(buildPath string, args []string, packages []string) error {

	// Clean buildPath if exists before build
	if err := utils.CleanDirectory(buildPath); err != nil {
		return err
	}

	// Save root and change to build directory
	root, _ := os.Getwd()
	os.Chdir(buildPath)

	// Wait group
	var wg sync.WaitGroup
	wg.Add(len(packages))

	// Error channel
	chErr := make(chan error)

	// Build the packages
	for _, pkg := range packages {

		// Routine for package build
		go func(pkg string, args []string, chErr chan error) {

			defer wg.Done()

			// Add package to arguments
			args = append(args, pkg)

			// Shell command go build [flags] [binary] [package]
			cmdName := "go"
			cmdArgs := []string{"build"}

			// Add arguments
			cmdArgs = append(cmdArgs, args...)

			// Run go build with arguments command, send error to channel
			chErr <- utils.CmdRun(cmdName, cmdArgs...)

		}(pkg, args, chErr)
	}

	// Wait for channel errors
	for i := 0; i < len(packages); i++ {
		err := <-chErr
		if err != nil {
			return err
		}
	}

	// Wait for routines
	wg.Wait()

	// Change back to root
	os.Chdir(root)

	return nil
}
Example #4
0
// createGitRepo init git repository with a initial commit
func createGitRepo() error {

	// git init repository for project
	if err := utils.CmdRun("git", "init"); err != nil {
		return err
	}

	// git add all files
	if err := utils.CmdRun("git", "add", "."); err != nil {
		return err
	}

	// git initial commit
	if err := utils.CmdRun("git", "commit", "-m", "initial commit"); err != nil {
		return err
	}

	return nil
}
Example #5
0
// startTaskRunner run the task runner with arguments
// the task name is in arguments
func startTaskRunner(args []string) error {

	// args must be contains task name
	if len(args) == 0 {
		return fmt.Errorf("Missing task name in arguments.")
	}

	buildFilePath := filepath.Join("_task-runner")

	// main.go is the entry point for the task runner
	if _, err := os.Stat(filepath.Join(buildFilePath, "main.go")); os.IsNotExist(err) {
		return err
	}

	// list files from buildFilePath in buildFiles
	buildFiles, err := ioutil.ReadDir(buildFilePath)
	if err != nil {
		return err
	}

	// files array for go run
	var buildFilesArr []string

	// add only *.go files to buildFilesArr
	for _, f := range buildFiles {
		if strings.HasSuffix(f.Name(), ".go") {
			buildFilesArr = append(buildFilesArr, filepath.Join("_task-runner", f.Name()))
		}
	}

	// create the command arguments
	cmdArgs := append([]string{"run"}, buildFilesArr...)

	// add arguments
	if len(args) > 0 {
		cmdArgs = append(cmdArgs, args...)
	}

	// go run buildFiles arguments
	if err := utils.CmdRun("go", cmdArgs...); err != nil {
		return err
	}

	return nil
}
Example #6
0
// test all packages without vendor
func test(c *taskRunner.Context) {

	// Get all packages without vendor
	pkgs, err := utils.GetAllPackages("/vendor/")
	if err != nil {
		log.Fatal(err)
	}

	// Add packages to command args
	cmdArgs := append([]string{"test"}, pkgs...)

	// If arguments are available to attach to cmdArgs.
	if len(c.Args) > 0 {
		cmdArgs = append(cmdArgs, c.Args...)
	}

	// Run test for all packages without vendor
	if err := utils.CmdRun("go", cmdArgs...); err != nil {
		log.Fatal(err)
	}
}
Example #7
0
func main() {

	// setup application returns *settings ...
	settings := setup()

	// create new cli app
	app := cli.NewApp()
	app.Version = lxb.Version
	app.Name = "lxb"
	app.Usage = `Scaffolding, vendoring and task runner for Go projects.
   Apart from the reserved commands all others are forwarded to the Task Runner.`

	// action of lxb
	app.Action = func(c *cli.Context) {

		// show Help when no args were passed.
		if len(c.Args()) == 0 {
			cli.ShowAppHelp(c)
			return
		}

		// only in GOPATH start task runner
		if !isGoPath(settings) {
			log.Fatal("Error: Project is not in GOPATH.")
		}

		// start the task runner
		// task passed in arguments
		startTaskRunner(c.Args())
	}

	// sub commands for lxb
	app.Commands = []cli.Command{
		{
			Name:  "create",
			Usage: "Create a project in working directory.",
			Flags: []cli.Flag{
				cli.BoolTFlag{
					Name:  "git",
					Usage: "git initial commit. --git=false for disable.",
				},
			},
			Action: func(c *cli.Context) {

				// Only in GOPATH create a project
				if !isGoPath(settings) {
					log.Fatal("Error: Project is not in GOPATH.")
				}

				settings.GitFlag = c.Bool("git")

				// create new project
				if err := createNewProject(settings); err != nil {
					log.Fatal(err)
				}

				log.Printf("Project %s has been created.", settings.ProjectName)
			},
		},
		{
			Name:  "backup",
			Usage: "Backup glide.yaml and glide.lock files.",
			Action: func(c *cli.Context) {

				// glide.yaml and glide.lock paths
				srcYaml := filepath.Join(settings.Root, "glide.yaml")
				srcLock := filepath.Join(settings.Root, "glide.lock")
				dstYaml := filepath.Join(settings.Root, "glide.yaml.bak")
				dstLock := filepath.Join(settings.Root, "glide.lock.bak")

				// check if glide.yaml file exists
				okYaml, err := utils.PathExists(srcYaml)
				if err != nil {
					log.Fatal(err)
				}

				// check if glide.lock file exists
				okLock, err := utils.PathExists(srcLock)
				if err != nil {
					log.Fatal(err)
				}

				// not found glide.yaml
				if !okYaml {
					log.Println("No backup created, can not find glide.yaml file.")
					return
				}

				// create backup from glide.yaml
				if err := fileutils.Copyfile(dstYaml, srcYaml); err != nil {
					log.Fatal(err)
				}

				// if glide.lock exists save this file
				if okLock {
					if err := fileutils.Copyfile(dstLock, srcLock); err != nil {
						log.Fatal(err)
					}
				}

				log.Println("Backup from glide.yaml and glide.lock successfully created.")
			},
		},
		{
			Name:  "restore",
			Usage: "Restore glide.yaml and glide.lock files and install.",
			Action: func(c *cli.Context) {

				// glide.yaml and glide.lock paths
				srcYaml := filepath.Join(settings.Root, "glide.yaml.bak")
				srcLock := filepath.Join(settings.Root, "glide.lock.bak")
				dstYaml := filepath.Join(settings.Root, "glide.yaml")
				dstLock := filepath.Join(settings.Root, "glide.lock")

				// check if backup files exists
				okYaml, err := utils.PathExists(srcYaml)
				if err != nil {
					log.Fatal(err)
				}

				// check if glide.lock file exists
				okLock, err := utils.PathExists(srcLock)
				if err != nil {
					log.Fatal(err)
				}

				// not found backup
				if !okYaml {
					log.Println("No backup restored, can not find glide.yaml.bak file.")
					return
				}

				// restore backups
				log.Println("Restore glide.yaml.bak -> glide.yaml")
				if err := fileutils.Copyfile(dstYaml, srcYaml); err != nil {
					log.Fatal(err)
				}

				// if glide.lock.bak exists restore this file
				if okLock {
					if err := fileutils.Copyfile(dstLock, srcLock); err != nil {
						log.Fatal(err)
					}
				}

				// install vendors
				log.Println("Install the vendors.")
				if err := utils.CmdRun("glide", "install"); err != nil {
					log.Fatal(err)
				}

				// delete old backups
				log.Println("Delete backup files")
				if err := os.Remove(srcYaml); err != nil {
					log.Fatal(err)
				}

				// if glide.lock.bak exists delete this file
				if okLock {
					if err := os.Remove(srcLock); err != nil {
						log.Fatal(err)
					}
				}

				log.Println("Restore vendors successfully.")
			},
		},
	}

	app.Run(os.Args)
}