This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathroot.go
122 lines (100 loc) · 2.84 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
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
package cmd
import (
"errors"
"fmt"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/security"
"github.com/ZupIT/ritchie-cli/pkg/session"
"os"
"runtime"
"github.com/spf13/cobra"
"github.com/ZupIT/ritchie-cli/pkg/env"
"github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil"
"github.com/ZupIT/ritchie-cli/pkg/workspace"
)
const (
versionMsg = "%s (%s)\n Build date: %s\n Built with: %s\n"
cmdUse = "rit"
cmdShortDescription = "rit is a NoOps CLI"
cmdDescription = `A CLI that developers can build and operate
your applications without help from the infra staff.
Complete documentation is available at https://github.com/ZupIT/ritchie-cli`
)
var (
// Version contains the current version.
Version = "dev"
// BuildDate contains a string with the build date.
BuildDate = "unknown"
whitelist = []string{
fmt.Sprintf("%s login", cmdUse),
fmt.Sprintf("%s logout", cmdUse),
fmt.Sprintf("%s completion zsh", cmdUse),
fmt.Sprintf("%s completion bash", cmdUse),
}
)
type rootCmd struct {
workspaceManager workspace.Checker
loginManager security.LoginManager
sessionValidator session.Validator
}
// NewRootCmd creates the root for all ritchie commands.
func NewRootCmd(wm workspace.Checker, l security.LoginManager, sv session.Validator) *cobra.Command {
o := &rootCmd{wm, l, sv}
return &cobra.Command{
Use: cmdUse,
Version: version(),
Short: cmdShortDescription,
Long: cmdDescription,
PersistentPreRunE: o.PreRunFunc(),
SilenceErrors: true,
}
}
func version() string {
return fmt.Sprintf(versionMsg, Version, env.Edition, BuildDate, runtime.Version())
}
func (o *rootCmd) PreRunFunc() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
if err := o.workspaceManager.Check(); err != nil {
return err
}
if err := o.checkSession(cmd.CommandPath()); err != nil {
return err
}
return nil
}
}
func (o *rootCmd) checkSession(commandPath string) error {
if sliceutil.Contains(whitelist, commandPath) {
return nil
}
err := o.sessionValidator.Validate()
if err != nil {
fmt.Print("To use this command, you need to start a session on Ritchie\n\n")
secret, err := sessionPrompt()
if err != nil {
return err
}
if err := o.loginManager.Login(secret); err != nil {
return err
}
fmt.Println("Session created successfully!")
os.Exit(0)
}
return nil
}
func sessionPrompt() (security.Passcode, error) {
var passcode string
var err error
switch env.Edition {
case env.Single:
passcode, err = prompt.Password("Define a passphrase for the session: ")
case env.Team:
passcode, err = prompt.String("Enter your organization: ", true)
default:
err = errors.New("invalid Ritchie build, no edition defined")
}
if err != nil {
return "", err
}
return security.Passcode(passcode), nil
}