-
Notifications
You must be signed in to change notification settings - Fork 2
/
root.go
91 lines (80 loc) · 1.89 KB
/
root.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
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
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"runtime/debug"
"github.com/JunNishimura/Goit/internal/file"
"github.com/JunNishimura/Goit/internal/log"
"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)
var (
gLogger *log.GoitLogger
client *store.Client
goitVersion = ""
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "goit",
Short: "Git made by Golang",
Long: "This is a Git-like CLI tool made by Golang",
RunE: func(cmd *cobra.Command, args []string) error {
versionFlag, err := cmd.Flags().GetBool("version")
if err != nil {
return fmt.Errorf("fail to get version flag: %w", err)
}
if versionFlag {
fmt.Println(goitVersion)
}
return nil
},
}
func Execute(version string) {
// set version
if version == "" {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
goitVersion = buildInfo.Main.Version
}
} else {
goitVersion = version
}
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootGoitPath, _ := file.FindGoitRoot(".") // ignore the error since the error is not important
config, err := store.NewConfig(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
index, err := store.NewIndex(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
head, err := store.NewHead(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
refs, err := store.NewRefs(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ignore, err := store.NewIgnore(rootGoitPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
client = store.NewClient(config, index, head, refs, ignore, rootGoitPath)
gLogger = log.NewGoitLogger(client.RootGoitPath)
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().BoolP("version", "v", false, "Show Goit version")
}