/
init.go
49 lines (45 loc) · 1.06 KB
/
init.go
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 cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
)
var (
initCmd = &cobra.Command{
Use: "init",
Short: "Initialize ~/.gameover.",
Run: func(cmd *cobra.Command, args []string) {
tmp, err := ioutil.TempDir("", "gameover")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.RemoveAll(gameoverDir(""))
_, err = git.PlainClone(tmp, false, &git.CloneOptions{
URL: "https://github.com/kbrgl/gameover.git",
})
if err != nil {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
err = os.Rename(filepath.Join(tmp, ".gameover"), gameoverDir(""))
if err != nil {
fmt.Printf("failed to rename: %s\n", err)
os.Exit(1)
}
err = os.RemoveAll(tmp)
if err != nil {
fmt.Printf("failed to delete directory: %s\n", err)
os.Exit(1)
}
fmt.Println("Initialized gameover in", gameoverDir(""))
fmt.Println("Add 'source ~/.gameover/env' to your ~/.profile or whatever shell init script you use")
},
}
)
func init() {
rootCmd.AddCommand(initCmd)
}