Example #1
0
File: job.go Project: ericaro/sbr
//dorefresh actually run the refresh command, it is unsafe to call it without caution. It should only update errcode, and result
func (j *job) dorefresh(w io.Writer) error {

	// NB: this is  a mammoth function. I know it, but I wasn't able to
	// split it down before having done everything.
	//
	// Step by step I will extract subffunctions to appropriate set of objects
	//

	wd, err := os.Getwd()
	if err != nil {
		return err
	}
	_, err = os.Stat(j.name)
	if os.IsNotExist(err) { // target does not exist, make it.
		fmt.Fprintf(w, "%s dir does not exists. Will create one.\n", j.name)
		result, err := git.Clone(wd, j.name, j.remote, j.branch)
		fmt.Fprintln(w, result)
		if err != nil {
			return err
		}
	}

	wk := sbr.NewWorkspace(filepath.Join(wd, j.name))
	ch := sbr.NewCheckouter(wk, w)
	ch.SetFastForwardOnly(true)
	ch.SetPrune(true)

	digest, err := ch.Checkout()
	if err != nil {
		return err
	}
	copy(j.refresh.version[:], digest)
	return nil
}
Example #2
0
func (c *ExecCmd) Run(args []string) {

	// use wd by default
	var wd string
	if *c.local {
		var err error
		wd, err = os.Getwd()
		if err != nil {
			log.Fatalf("Error, cannot determine the current directory. %s\n", err.Error())
			os.Exit(CodeNoWorkingDir)
		}
	} else {
		wd = FindRootCmd()
	}
	//build the workspace, that is used to trigger all commands
	workspace := sbr.NewWorkspace(wd)

	//again, passing the stdin, and stdout to the subprocess prevent: async, and ability to collect the outputs
	// for special outputers we need to collect outputs, so the 'special' var.
	// special => concurrent mode (because we need to collect outputs)
	// Therefore, selecting the output mode imply selecting "special"= true|false
	// and the ExecutionProcessor function
	xargs := make([]string, 0)
	if len(args) > 1 {
		xargs = args[1:]
	}
	name := args[0]
	executions := ExecConcurrently(workspace, name, xargs...)
	switch {
	case *c.cat:
		ExecutionCat(executions)
	case *c.sum:
		ExecutionSum(executions)
	case *c.count:
		ExecutionCount(executions)
	case *c.digest:
		ExecutionDigest(executions)
	default:
		ExecutionPrinter(executions)
	}
}
Example #3
0
func (c *CloneCmd) Run(args []string) {
	// use wd by default
	wd, err := os.Getwd()
	if err != nil {
		fmt.Printf("Error, cannot determine the current directory. %s\n", err.Error())
		os.Exit(CodeNoWorkingDir)
	}

	var rel, remote string
	switch len(args) {
	case 0:
		fmt.Printf("Usage sbr clone [-b branch] <remote> [target]\n")
		os.Exit(-1)
	case 1:
		remote = args[0]
		rel = strings.TrimSuffix(filepath.Base(remote), ".git")
	case 2:
		remote = args[0]
		rel = args[1]
	}

	res, err := git.Clone(wd, rel, remote, *c.branch)
	fmt.Println(res)
	if err != nil {
		fmt.Printf("Error, cannot clone %s: %s\n", remote, err.Error())
		os.Exit(-1)
	}

	//creates a workspace to be able to read from/to sets
	workspace := sbr.NewWorkspace(filepath.Join(wd, rel))
	ch := sbr.NewCheckouter(workspace, os.Stdout)
	_, err = ch.Checkout()
	if err != nil {
		fmt.Printf("checkout error: %s", err.Error())
		os.Exit(-1)
	}
}
Example #4
0
File: main.go Project: ericaro/sbr
func main() {
	flag.Parse()
	if (flag.NArg() == 0 && !*list) || *help {
		usage()
		os.Exit(-1)
	}

	// use wd by default
	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("Error, cannot determine the current directory. %s\n", err.Error())
	}
	//build the workspace, that is used to trigger all commands
	workspace := sbr.NewWorkspace(wd)

	// parses the remaining args in order to pass them to the underlying process
	args := make([]string, 0)
	if flag.NArg() > 1 {
		args = flag.Args()[1:]
	}
	name := flag.Arg(0)

	if *list {
		//for now there is only one way to print dependencies
		//List just count and print all directories.
		var count int
		for _, prj := range workspace.ScanRel() {
			count++
			rel, err := filepath.Rel(wd, prj)
			if err != nil {
				rel = prj // uses the absolute path in this case
			}
			fmt.Printf("\033[00;32m%s\033[00m$ \n", rel)
		}
		fmt.Printf("Done (\033[00;32m%v\033[00m repositories)\n", count)

	} else {
		// select the output mode

		//again, passing the stdin, and stdout to the subprocess prevent: async, and ability to collect the outputs
		// for special outputers we need to collect outputs, so the 'special' var.
		// special => concurrent mode (because we need to collect outputs)
		// Therefore, selecting the output mode imply selecting "special"= true|false
		// and the ExecutionProcessor function
		var special bool = true
		var xp cmd.ExecutionProcessor
		switch {
		case *cat:
			xp = cmd.ExecutionCat
		case *sum:
			xp = cmd.ExecutionSum
		case *count:
			xp = cmd.ExecutionCount
		case *digest:
			xp = cmd.ExecutionDigest
		default:
			xp = cmd.ExecutionPrinter
			special = false
		}
		if special || *async { // this implies concurrent
			// based on the async option, exec asynchronously or sequentially.
			// we cannot just make "seq" a special case of concurrent, since when running sequentially we provide
			// direct access to the std streams. commands can use stdin, and use term escape codes.
			// When in async mode, we just can't do that.
			executions := cmd.ExecConcurrently(workspace, name, args...)
			xp(executions)

		} else {
			ExecSequentially(workspace, name, args...)
		}
	}

}