File: fakegit.go

package info (click to toggle)
golang-github-docopt-docopt-go 0.6.2%2Bgit20180111.ee0de3b-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 284 kB
  • sloc: makefile: 10
file content (106 lines) | stat: -rw-r--r-- 3,016 bytes parent folder | download
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
package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
	"os"
	"os/exec"
)

func main() {
	usage := `usage: git [--version] [--exec-path=<path>] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=<path>] [--work-tree=<path>]
           [-c <name>=<value>] [--help]
           <command> [<args>...]

options:
   -c <name=value>
   -h, --help
   -p, --paginate

The most commonly used git commands are:
   add        Add file contents to the index
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   push       Update remote refs along with associated objects
   remote     Manage set of tracked repositories

See 'git help <command>' for more information on a specific command.
`
	parser := &docopt.Parser{OptionsFirst: true}
	args, _ := parser.ParseArgs(usage, nil, "git version 1.7.4.4")

	cmd := args["<command>"].(string)
	cmdArgs := args["<args>"].([]string)

	fmt.Println("global arguments:", args)
	fmt.Println("command arguments:", cmd, cmdArgs)

	err := runCommand(cmd, cmdArgs)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func goRun(scriptName string, args []string) (err error) {
	cmdArgs := make([]string, 2)
	cmdArgs[0] = "run"
	cmdArgs[1] = scriptName
	cmdArgs = append(cmdArgs, args...)
	osCmd := exec.Command("go", cmdArgs...)
	var out []byte
	out, err = osCmd.Output()
	fmt.Println(string(out))
	if err != nil {
		return
	}
	return
}

func runCommand(cmd string, args []string) (err error) {
	argv := append([]string{cmd}, args...)
	switch cmd {
	case "add":
		// subcommand is a function call
		return cmdAdd(argv)
	case "branch":
		// subcommand is a script
		return goRun("branch/git_branch.go", argv)
	case "checkout", "clone", "commit", "push", "remote":
		// subcommand is a script
		scriptName := fmt.Sprintf("%s/git_%s.go", cmd, cmd)
		return goRun(scriptName, argv)
	case "help", "":
		return goRun("git.go", append(argv[1:], "--help"))
	}

	return fmt.Errorf("%s is not a git command. See 'git help'", cmd)
}

func cmdAdd(argv []string) (err error) {
	usage := `usage: git add [options] [--] [<filepattern>...]

options:
	-h, --help
	-n, --dry-run        dry run
	-v, --verbose        be verbose
	-i, --interactive    interactive picking
	-p, --patch          select hunks interactively
	-e, --edit           edit current diff and apply
	-f, --force          allow adding otherwise ignored files
	-u, --update         update tracked files
	-N, --intent-to-add  record only the fact that the path will be added later
	-A, --all            add all, noticing removal of tracked files
	--refresh            don't add, only refresh the index
	--ignore-errors      just skip files which cannot be added because of errors
	--ignore-missing     check if - even missing - files are ignored in dry run
`

	args, _ := docopt.ParseDoc(usage)
	fmt.Println(args)
	return
}