-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathroot.go
More file actions
165 lines (152 loc) · 4.68 KB
/
root.go
File metadata and controls
165 lines (152 loc) · 4.68 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2019 Preferred Networks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os"
"github.com/pfnet-research/git-ghost/pkg/ghost/git"
"github.com/pfnet-research/git-ghost/pkg/ghost/types"
"github.com/pfnet-research/git-ghost/pkg/util/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type globalFlags struct {
srcDir string
ghostWorkDir string
ghostPrefix string
ghostRepo string
verbose int
}
func (gf globalFlags) WorkingEnvSpec() types.WorkingEnvSpec {
workingEnvSpec := types.WorkingEnvSpec{
SrcDir: gf.srcDir,
GhostWorkingDir: gf.ghostWorkDir,
GhostRepo: gf.ghostRepo,
}
userName, userEmail, err := git.GetUserConfig(globalOpts.srcDir)
if err == nil {
workingEnvSpec.GhostUserName = userName
workingEnvSpec.GhostUserEmail = userEmail
} else {
log.Debug("failed to get user name and email of the source directory")
}
return workingEnvSpec
}
var (
Version string
Revision string
)
var globalOpts globalFlags
func NewRootCmd() *cobra.Command {
cobra.OnInitialize()
rootCmd := &cobra.Command{
Use: "git-ghost",
Short: "git-ghost",
SilenceErrors: false,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Use == "version" {
return nil
}
err := validateEnvironment()
if err != nil {
return err
}
err = globalOpts.SetDefaults()
if err != nil {
return err
}
err = globalOpts.Validate()
if err != nil {
return err
}
switch globalOpts.verbose {
case 0:
log.SetLevel(log.ErrorLevel)
case 1:
log.SetLevel(log.InfoLevel)
case 2:
log.SetLevel(log.DebugLevel)
case 3:
log.SetLevel(log.TraceLevel)
}
return nil
},
}
rootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)")
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)")
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)")
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)")
rootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode")
rootCmd.AddCommand(NewPushCommand())
rootCmd.AddCommand(NewPullCommand())
rootCmd.AddCommand(NewShowCommand())
rootCmd.AddCommand(NewListCommand())
rootCmd.AddCommand(NewDeleteCommand())
rootCmd.AddCommand(NewGCCommand())
rootCmd.AddCommand(NewVersionCommand())
rootCmd.AddCommand(NewCompletionCommand(rootCmd))
return rootCmd
}
func NewVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number of git-ghost",
Long: `Print the version number of git-ghost`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("git-ghost %s (revision: %s)", Version, Revision)
},
}
}
func validateEnvironment() errors.GitGhostError {
err := git.ValidateGit()
if err != nil {
return errors.New("git is required")
}
return nil
}
func (flags *globalFlags) SetDefaults() errors.GitGhostError {
if globalOpts.srcDir == "" {
globalOpts.srcDir = os.Getenv("PWD")
}
if globalOpts.ghostWorkDir == "" {
globalOpts.ghostWorkDir = os.TempDir()
}
if globalOpts.ghostPrefix == "" {
ghostPrefixEnv := os.Getenv("GIT_GHOST_PREFIX")
if ghostPrefixEnv == "" {
ghostPrefixEnv = "ghost"
}
globalOpts.ghostPrefix = ghostPrefixEnv
}
if globalOpts.ghostRepo == "" {
globalOpts.ghostRepo = os.Getenv("GIT_GHOST_REPO")
}
return nil
}
func (flags *globalFlags) Validate() errors.GitGhostError {
if flags.srcDir == "" {
return errors.New("src-dir must be specified")
}
_, err := os.Stat(flags.ghostWorkDir)
if err != nil {
return errors.Errorf("ghost-working-dir is not found (value: %v)", flags.ghostWorkDir)
}
if flags.ghostPrefix == "" {
return errors.New("ghost-prefix must be specified")
}
if flags.ghostRepo == "" {
return errors.New("ghost-repo must be specified")
}
return nil
}