-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
174 lines (150 loc) · 5.43 KB
/
main.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
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
166
167
168
169
170
171
172
173
174
/*
Copyright (C) 2019-Present Pivotal Software, Inc. All rights reserved.
This program and the accompanying materials are made available under the terms of the 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 main
import (
"fmt"
"os"
"github.com/jessevdk/go-flags"
"github.com/pivotal/hammer/bosh"
"github.com/pivotal/hammer/cf"
"github.com/pivotal/hammer/commands"
"github.com/pivotal/hammer/environment"
"github.com/pivotal/hammer/om"
"github.com/pivotal/hammer/open"
"github.com/pivotal/hammer/pks"
"github.com/pivotal/hammer/scripting"
"github.com/pivotal/hammer/ssh"
"github.com/pivotal/hammer/sshuttle"
"github.com/pivotal/hammer/ui"
)
var (
version = "development build"
date = "unknown date"
)
type timeCommand struct{}
func (c *timeCommand) Execute([]string) error {
fmt.Print("Can't touch this")
return nil
}
type versionCommand struct{}
func (c *versionCommand) Execute([]string) error {
fmt.Printf("Version: %s (%s)\n", version, date)
return nil
}
type targetConfigPath struct{}
// If `-t` is specified on the hammer command (rather than a subcommand)
// then set `HAMMER_TARGET_CONFIG` so the subcommand can read it
func (e *targetConfigPath) UnmarshalFlag(path string) error {
return os.Setenv("HAMMER_TARGET_CONFIG", path)
}
type environmentName struct{}
// If `-e` is specified on the hammer command (rather than a subcommand)
// then set `HAMMER_ENVIRONMENT_NAME` so the subcommand can read it
func (e *environmentName) UnmarshalFlag(name string) error {
return os.Setenv("HAMMER_ENVIRONMENT_NAME", name)
}
type options struct {
Bosh commands.BoshCommand `command:"bosh" description:"display BOSH credentials, or run a BOSH command"`
CFLogin commands.CFLoginCommand `command:"cf-login" description:"log in to the cf for the environment"`
PKSLogin commands.PKSLoginCommand `command:"pks-login" description:"log in to pks for the environment"`
Open commands.OpenCommand `command:"open" description:"open a browser to this environment"`
OM commands.OMCommand `command:"om" description:"run the 'om' command with credentials for this environment"`
SSH commands.SSHCommand `command:"ssh" choice:"opsman" choice:"director" description:"open an ssh connection to the ops manager or director of this environment"` //nolint:staticcheck
Sshuttle commands.SshuttleCommand `command:"sshuttle" description:"sshuttle to this environment"`
Time timeCommand `command:"time" description:"duuun dundundun" hidden:"true"`
Version versionCommand `command:"version" alias:"ver" description:"version of command"`
Completion commands.CompletionCommand `command:"completion" description:"command completion script"`
TargetConfig targetConfigPath `short:"t" long:"target" env:"HAMMER_TARGET_CONFIG" description:"path to the target environment config"`
EnvironmentName environmentName `short:"e" long:"environment-name" env:"HAMMER_ENVIRONMENT_NAME" description:"name of the environment in the config to target"`
}
func main() {
envReader := environment.Reader{}
ui := ui.UI{
Out: os.Stdout,
Err: os.Stderr,
}
scriptRunner := scripting.NewScriptRunner()
opts := options{
Bosh: commands.BoshCommand{
Env: &envReader,
UI: &ui,
BoshRunner: &bosh.Runner{
ScriptRunner: scriptRunner,
},
},
CFLogin: commands.CFLoginCommand{
Env: &envReader,
UI: &ui,
CFLoginRunner: &cf.LoginRunner{
ScriptRunner: scriptRunner,
},
},
PKSLogin: commands.PKSLoginCommand{
Env: &envReader,
UI: &ui,
PKSLoginRunner: &pks.LoginRunner{
ScriptRunner: scriptRunner,
},
},
OM: commands.OMCommand{
Env: &envReader,
UI: &ui,
OMRunner: &om.Runner{
ScriptRunner: scriptRunner,
},
},
Open: commands.OpenCommand{
Env: &envReader,
UI: &ui,
OpenRunner: &open.Runner{
ScriptRunner: scriptRunner,
},
},
SSH: commands.SSHCommand{
OpsManager: commands.SSHOpsManagerCommand{
Env: &envReader,
UI: &ui,
SSHRunner: &ssh.OpsManagerRunner{
ScriptRunner: scriptRunner,
},
},
Director: commands.SSHDirectorCommand{
Env: &envReader,
UI: &ui,
SSHRunner: &ssh.DirectorRunner{
ScriptRunner: scriptRunner,
},
},
},
Sshuttle: commands.SshuttleCommand{
Env: &envReader,
UI: &ui,
SshuttleRunner: &sshuttle.Runner{
ScriptRunner: scriptRunner,
},
},
Time: timeCommand{},
}
if len(os.Args) < 2 {
os.Args = append(os.Args, "--help")
}
if _, err := flags.Parse(&opts); err != nil {
if flagsErr, ok := err.(*flags.Error); ok {
if flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
if flagsErr.Type == flags.ErrUnknownFlag {
printDoubleDashMessage()
}
}
os.Exit(1)
}
}
func printDoubleDashMessage() {
fmt.Fprintf(os.Stderr, "\nIf passing flags to 'bosh' or 'om', use a double dash '--', for example:\n")
fmt.Fprintf(os.Stderr, "\n hammer -t environment-path bosh -- -d deployment manifest\n")
}