Skip to content

Commit

Permalink
create a cli module
Browse files Browse the repository at this point in the history
  • Loading branch information
BenJoyenConseil committed Oct 18, 2020
1 parent 909cf14 commit 6125468
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 45 deletions.
80 changes: 80 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cli

import (
"flag"
"fmt"
"log"
"os"
"vcs/storage"
"vcs/tree"
)

var Commands map[string]*Command

type Command struct {
Function func(args []string)
flagset *flag.FlagSet
parameters map[string]string
parsedOutputs map[string]*string
usage string
}

func init() {
Commands = map[string]*Command{
"commit": {
Function: commit,
parameters: map[string]string{"m": "The message of the commit inside double quotes"},
parsedOutputs: map[string]*string{},
usage: "Snapshot the current directory in VCS and return the oid",
},
"init": {
Function: uinit,
usage: "<directory>",
},
"log": {
Function: ulog,
usage: "<ref>",
},
"checkout": {usage: "<ref>"},
}

for name, cmd := range Commands {
cmd.flagset = flag.NewFlagSet(name, flag.ExitOnError)
for p, u := range cmd.parameters {
cmd.parsedOutputs[p] = cmd.flagset.String(p, "", u)
}
}

flag.Usage = func() {
fmt.Printf("Usage of vcs \n")
for name, cmd := range Commands {
fmt.Printf(" %s : %s\n", name, cmd.usage)
cmd.flagset.PrintDefaults()
}
}
}

func commit(args []string) {
Commands["commit"].flagset.Parse(args)
if val, _ := Commands["commit"].parsedOutputs["m"]; *val != "" {
log.Println("Commit hash : ", tree.Commit("./", *Commands["commit"].parsedOutputs["m"]))
os.Exit(0)
} else {
Commands["commit"].flagset.PrintDefaults()
os.Exit(1)
}
}

func uinit(args []string) {
path := "./"
if len(os.Args) >= 3 {
path = os.Args[2]
}
storage.UInit(path)
os.Exit(0)
}

func ulog(args []string) {
tree.PrintLog(tree.Log())
os.Exit(0)
}
48 changes: 3 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,20 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"vcs/storage"
"vcs/tree"
"vcs/cli"
)

var commands = map[string]*flag.FlagSet{
"commit": flag.NewFlagSet("commit", flag.ExitOnError),
"init": flag.NewFlagSet("init", flag.ExitOnError),
"log": flag.NewFlagSet("log", flag.ExitOnError),
}

var usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
for k, v := range commands {
fmt.Fprintf(os.Stderr, "%s\n", k)
v.PrintDefaults()
}
flag.PrintDefaults()
}

var commitMessage *string

func init() {
commitMessage = commands["commit"].String("m", "", "The message of the commit inside double quotes")
}

func main() {
flag.Usage = usage
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
if _, ok := commands[os.Args[1]]; !ok {
if _, ok := cli.Commands[os.Args[1]]; !ok {
flag.Usage()
os.Exit(1)
}
switch os.Args[1] {
case "commit":
commands["commit"].Parse(os.Args[2:])
if *commitMessage != "" {
log.Println("Commit hash : ", tree.Commit("./", *commitMessage))
return
}
case "init":
path := "./"
if len(os.Args) >= 3 {
path = os.Args[2]
}
storage.UInit(path)
return
case "log":
tree.PrintLog(tree.Log())
return
}
cli.Commands[os.Args[1]].Function(os.Args[2:])

flag.Usage()
}

0 comments on commit 6125468

Please sign in to comment.