-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchallenge.go
More file actions
49 lines (43 loc) · 1.25 KB
/
challenge.go
File metadata and controls
49 lines (43 loc) · 1.25 KB
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
package main
import (
"fmt"
"os"
"os/exec"
)
var cmdChallenge = &Command{
Run: getChallenge,
UsageLine: "challenge <challenge-name>",
Short: "fetch challenge from server",
Long: `
Fetch challenge <challenge-name> from server.
All needed files will be saved in a directory named <challenge-name>.
`,
}
func getChallenge(cmd *Command, args []string) {
if len(args) < 1 {
// No argument given, list all available challenges
listChallenges(cmd, args)
} else if len(args) == 1 {
// challenge name given, clone into directory
cloneChallenge(cmd, args)
} else {
// Invalid number of arguments, show usage
fmt.Fprintln(os.Stderr, "Invalid number of arguments")
os.Exit(2)
}
}
func listChallenges(cmd *Command, args []string) {
fmt.Printf("%s:\t%s\n", "tictactoe", "Write a bot that plays Tic Tac Toe")
}
func cloneChallenge(cmd *Command, args []string) {
repo := "git@git.cod.uno:" + args[0]
c := exec.Command("git", "clone", repo)
outstr, err := c.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to fetch challenge <%s>.\n", args[0])
fmt.Fprintf(os.Stderr, "Reason: %s\n", outstr) // TODO: Remove message from production tool
os.Exit(2)
} else {
fmt.Printf("Successfully fetched into directory %s\n", args[0])
}
}