Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-shell
Commits
60280bbf
Commit
60280bbf
authored
May 20, 2019
by
Igor Drozdov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pass readWriter to Command constructor
parent
58d8c769
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
52 additions
and
40 deletions
+52
-40
go/cmd/gitlab-shell/main.go
go/cmd/gitlab-shell/main.go
+3
-3
go/internal/command/command.go
go/internal/command/command.go
+6
-6
go/internal/command/command_test.go
go/internal/command/command_test.go
+2
-2
go/internal/command/discover/discover.go
go/internal/command/discover/discover.go
+6
-5
go/internal/command/discover/discover_test.go
go/internal/command/discover/discover_test.go
+12
-4
go/internal/command/fallback/fallback.go
go/internal/command/fallback/fallback.go
+1
-3
go/internal/command/fallback/fallback_test.go
go/internal/command/fallback/fallback_test.go
+3
-3
go/internal/command/twofactorrecover/twofactorrecover.go
go/internal/command/twofactorrecover/twofactorrecover.go
+13
-12
go/internal/command/twofactorrecover/twofactorrecover_test.go
...nternal/command/twofactorrecover/twofactorrecover_test.go
+6
-2
No files found.
go/cmd/gitlab-shell/main.go
View file @
60280bbf
...
@@ -29,7 +29,7 @@ func findRootDir() (string, error) {
...
@@ -29,7 +29,7 @@ func findRootDir() (string, error) {
func
execRuby
(
rootDir
string
,
readWriter
*
readwriter
.
ReadWriter
)
{
func
execRuby
(
rootDir
string
,
readWriter
*
readwriter
.
ReadWriter
)
{
cmd
:=
&
fallback
.
Command
{
RootDir
:
rootDir
,
Args
:
os
.
Args
}
cmd
:=
&
fallback
.
Command
{
RootDir
:
rootDir
,
Args
:
os
.
Args
}
if
err
:=
cmd
.
Execute
(
readWriter
);
err
!=
nil
{
if
err
:=
cmd
.
Execute
();
err
!=
nil
{
fmt
.
Fprintf
(
readWriter
.
ErrOut
,
"Failed to exec: %v
\n
"
,
err
)
fmt
.
Fprintf
(
readWriter
.
ErrOut
,
"Failed to exec: %v
\n
"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
}
}
...
@@ -56,7 +56,7 @@ func main() {
...
@@ -56,7 +56,7 @@ func main() {
execRuby
(
rootDir
,
readWriter
)
execRuby
(
rootDir
,
readWriter
)
}
}
cmd
,
err
:=
command
.
New
(
os
.
Args
,
config
)
cmd
,
err
:=
command
.
New
(
os
.
Args
,
config
,
readWriter
)
if
err
!=
nil
{
if
err
!=
nil
{
// For now this could happen if `SSH_CONNECTION` is not set on
// For now this could happen if `SSH_CONNECTION` is not set on
// the environment
// the environment
...
@@ -66,7 +66,7 @@ func main() {
...
@@ -66,7 +66,7 @@ func main() {
// The command will write to STDOUT on execution or replace the current
// The command will write to STDOUT on execution or replace the current
// process in case of the `fallback.Command`
// process in case of the `fallback.Command`
if
err
=
cmd
.
Execute
(
readWriter
);
err
!=
nil
{
if
err
=
cmd
.
Execute
();
err
!=
nil
{
fmt
.
Fprintf
(
readWriter
.
ErrOut
,
"%v
\n
"
,
err
)
fmt
.
Fprintf
(
readWriter
.
ErrOut
,
"%v
\n
"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
}
}
...
...
go/internal/command/command.go
View file @
60280bbf
...
@@ -10,10 +10,10 @@ import (
...
@@ -10,10 +10,10 @@ import (
)
)
type
Command
interface
{
type
Command
interface
{
Execute
(
*
readwriter
.
ReadWriter
)
error
Execute
()
error
}
}
func
New
(
arguments
[]
string
,
config
*
config
.
Config
)
(
Command
,
error
)
{
func
New
(
arguments
[]
string
,
config
*
config
.
Config
,
readWriter
*
readwriter
.
ReadWriter
)
(
Command
,
error
)
{
args
,
err
:=
commandargs
.
Parse
(
arguments
)
args
,
err
:=
commandargs
.
Parse
(
arguments
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -21,18 +21,18 @@ func New(arguments []string, config *config.Config) (Command, error) {
...
@@ -21,18 +21,18 @@ func New(arguments []string, config *config.Config) (Command, error) {
}
}
if
config
.
FeatureEnabled
(
string
(
args
.
CommandType
))
{
if
config
.
FeatureEnabled
(
string
(
args
.
CommandType
))
{
return
buildCommand
(
args
,
config
),
nil
return
buildCommand
(
args
,
config
,
readWriter
),
nil
}
}
return
&
fallback
.
Command
{
RootDir
:
config
.
RootDir
,
Args
:
arguments
},
nil
return
&
fallback
.
Command
{
RootDir
:
config
.
RootDir
,
Args
:
arguments
},
nil
}
}
func
buildCommand
(
args
*
commandargs
.
CommandArgs
,
config
*
config
.
Config
)
Command
{
func
buildCommand
(
args
*
commandargs
.
CommandArgs
,
config
*
config
.
Config
,
readWriter
*
readwriter
.
ReadWriter
)
Command
{
switch
args
.
CommandType
{
switch
args
.
CommandType
{
case
commandargs
.
Discover
:
case
commandargs
.
Discover
:
return
&
discover
.
Command
{
Config
:
config
,
Args
:
args
}
return
&
discover
.
Command
{
Config
:
config
,
Args
:
args
,
ReadWriter
:
readWriter
}
case
commandargs
.
TwoFactorRecover
:
case
commandargs
.
TwoFactorRecover
:
return
&
twofactorrecover
.
Command
{
Config
:
config
,
Args
:
args
}
return
&
twofactorrecover
.
Command
{
Config
:
config
,
Args
:
args
,
ReadWriter
:
readWriter
}
}
}
return
nil
return
nil
...
...
go/internal/command/command_test.go
View file @
60280bbf
...
@@ -65,7 +65,7 @@ func TestNew(t *testing.T) {
...
@@ -65,7 +65,7 @@ func TestNew(t *testing.T) {
restoreEnv
:=
testhelper
.
TempEnv
(
tc
.
environment
)
restoreEnv
:=
testhelper
.
TempEnv
(
tc
.
environment
)
defer
restoreEnv
()
defer
restoreEnv
()
command
,
err
:=
New
(
tc
.
arguments
,
tc
.
config
)
command
,
err
:=
New
(
tc
.
arguments
,
tc
.
config
,
nil
)
assert
.
NoError
(
t
,
err
)
assert
.
NoError
(
t
,
err
)
assert
.
IsType
(
t
,
tc
.
expectedType
,
command
)
assert
.
IsType
(
t
,
tc
.
expectedType
,
command
)
...
@@ -78,7 +78,7 @@ func TestFailingNew(t *testing.T) {
...
@@ -78,7 +78,7 @@ func TestFailingNew(t *testing.T) {
restoreEnv
:=
testhelper
.
TempEnv
(
map
[
string
]
string
{})
restoreEnv
:=
testhelper
.
TempEnv
(
map
[
string
]
string
{})
defer
restoreEnv
()
defer
restoreEnv
()
_
,
err
:=
New
([]
string
{},
&
config
.
Config
{})
_
,
err
:=
New
([]
string
{},
&
config
.
Config
{}
,
nil
)
assert
.
Error
(
t
,
err
,
"Only ssh allowed"
)
assert
.
Error
(
t
,
err
,
"Only ssh allowed"
)
})
})
...
...
go/internal/command/discover/discover.go
View file @
60280bbf
...
@@ -12,18 +12,19 @@ import (
...
@@ -12,18 +12,19 @@ import (
type
Command
struct
{
type
Command
struct
{
Config
*
config
.
Config
Config
*
config
.
Config
Args
*
commandargs
.
CommandArgs
Args
*
commandargs
.
CommandArgs
ReadWriter
*
readwriter
.
ReadWriter
}
}
func
(
c
*
Command
)
Execute
(
readWriter
*
readwriter
.
ReadWriter
)
error
{
func
(
c
*
Command
)
Execute
()
error
{
response
,
err
:=
c
.
getUserInfo
()
response
,
err
:=
c
.
getUserInfo
()
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Failed to get username: %v"
,
err
)
return
fmt
.
Errorf
(
"Failed to get username: %v"
,
err
)
}
}
if
response
.
IsAnonymous
()
{
if
response
.
IsAnonymous
()
{
fmt
.
Fprintf
(
r
eadWriter
.
Out
,
"Welcome to GitLab, Anonymous!
\n
"
)
fmt
.
Fprintf
(
c
.
R
eadWriter
.
Out
,
"Welcome to GitLab, Anonymous!
\n
"
)
}
else
{
}
else
{
fmt
.
Fprintf
(
r
eadWriter
.
Out
,
"Welcome to GitLab, @%s!
\n
"
,
response
.
Username
)
fmt
.
Fprintf
(
c
.
R
eadWriter
.
Out
,
"Welcome to GitLab, @%s!
\n
"
,
response
.
Username
)
}
}
return
nil
return
nil
...
...
go/internal/command/discover/discover_test.go
View file @
60280bbf
...
@@ -78,10 +78,14 @@ func TestExecute(t *testing.T) {
...
@@ -78,10 +78,14 @@ func TestExecute(t *testing.T) {
for
_
,
tc
:=
range
testCases
{
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
desc
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
desc
,
func
(
t
*
testing
.
T
)
{
cmd
:=
&
Command
{
Config
:
&
config
.
Config
{
GitlabUrl
:
url
},
Args
:
tc
.
arguments
}
buffer
:=
&
bytes
.
Buffer
{}
buffer
:=
&
bytes
.
Buffer
{}
cmd
:=
&
Command
{
Config
:
&
config
.
Config
{
GitlabUrl
:
url
},
Args
:
tc
.
arguments
,
ReadWriter
:
&
readwriter
.
ReadWriter
{
Out
:
buffer
},
}
err
:=
cmd
.
Execute
(
&
readwriter
.
ReadWriter
{
Out
:
buffer
}
)
err
:=
cmd
.
Execute
()
assert
.
NoError
(
t
,
err
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
tc
.
expectedOutput
,
buffer
.
String
())
assert
.
Equal
(
t
,
tc
.
expectedOutput
,
buffer
.
String
())
...
@@ -118,10 +122,14 @@ func TestFailingExecute(t *testing.T) {
...
@@ -118,10 +122,14 @@ func TestFailingExecute(t *testing.T) {
for
_
,
tc
:=
range
testCases
{
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
desc
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
desc
,
func
(
t
*
testing
.
T
)
{
cmd
:=
&
Command
{
Config
:
&
config
.
Config
{
GitlabUrl
:
url
},
Args
:
tc
.
arguments
}
buffer
:=
&
bytes
.
Buffer
{}
buffer
:=
&
bytes
.
Buffer
{}
cmd
:=
&
Command
{
Config
:
&
config
.
Config
{
GitlabUrl
:
url
},
Args
:
tc
.
arguments
,
ReadWriter
:
&
readwriter
.
ReadWriter
{
Out
:
buffer
},
}
err
:=
cmd
.
Execute
(
&
readwriter
.
ReadWriter
{
Out
:
buffer
}
)
err
:=
cmd
.
Execute
()
assert
.
Empty
(
t
,
buffer
.
String
())
assert
.
Empty
(
t
,
buffer
.
String
())
assert
.
EqualError
(
t
,
err
,
tc
.
expectedError
)
assert
.
EqualError
(
t
,
err
,
tc
.
expectedError
)
...
...
go/internal/command/fallback/fallback.go
View file @
60280bbf
...
@@ -4,8 +4,6 @@ import (
...
@@ -4,8 +4,6 @@ import (
"os"
"os"
"path/filepath"
"path/filepath"
"syscall"
"syscall"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter"
)
)
type
Command
struct
{
type
Command
struct
{
...
@@ -22,7 +20,7 @@ const (
...
@@ -22,7 +20,7 @@ const (
RubyProgram
=
"gitlab-shell-ruby"
RubyProgram
=
"gitlab-shell-ruby"
)
)
func
(
c
*
Command
)
Execute
(
*
readwriter
.
ReadWriter
)
error
{
func
(
c
*
Command
)
Execute
()
error
{
rubyCmd
:=
filepath
.
Join
(
c
.
RootDir
,
"bin"
,
RubyProgram
)
rubyCmd
:=
filepath
.
Join
(
c
.
RootDir
,
"bin"
,
RubyProgram
)
// Ensure rubyArgs[0] is the full path to gitlab-shell-ruby
// Ensure rubyArgs[0] is the full path to gitlab-shell-ruby
...
...
go/internal/command/fallback/fallback_test.go
View file @
60280bbf
...
@@ -49,7 +49,7 @@ func TestExecuteExecsCommandSuccesfully(t *testing.T) {
...
@@ -49,7 +49,7 @@ func TestExecuteExecsCommandSuccesfully(t *testing.T) {
fake
.
Setup
()
fake
.
Setup
()
defer
fake
.
Cleanup
()
defer
fake
.
Cleanup
()
require
.
NoError
(
t
,
cmd
.
Execute
(
nil
))
require
.
NoError
(
t
,
cmd
.
Execute
())
require
.
True
(
t
,
fake
.
Called
)
require
.
True
(
t
,
fake
.
Called
)
require
.
Equal
(
t
,
fake
.
Filename
,
"/tmp/bin/gitlab-shell-ruby"
)
require
.
Equal
(
t
,
fake
.
Filename
,
"/tmp/bin/gitlab-shell-ruby"
)
require
.
Equal
(
t
,
fake
.
Args
,
[]
string
{
"/tmp/bin/gitlab-shell-ruby"
,
"foo"
,
"bar"
})
require
.
Equal
(
t
,
fake
.
Args
,
[]
string
{
"/tmp/bin/gitlab-shell-ruby"
,
"foo"
,
"bar"
})
...
@@ -64,12 +64,12 @@ func TestExecuteExecsCommandOnError(t *testing.T) {
...
@@ -64,12 +64,12 @@ func TestExecuteExecsCommandOnError(t *testing.T) {
fake
.
Setup
()
fake
.
Setup
()
defer
fake
.
Cleanup
()
defer
fake
.
Cleanup
()
require
.
Error
(
t
,
cmd
.
Execute
(
nil
))
require
.
Error
(
t
,
cmd
.
Execute
())
require
.
True
(
t
,
fake
.
Called
)
require
.
True
(
t
,
fake
.
Called
)
}
}
func
TestExecuteGivenNonexistentCommand
(
t
*
testing
.
T
)
{
func
TestExecuteGivenNonexistentCommand
(
t
*
testing
.
T
)
{
cmd
:=
&
Command
{
RootDir
:
"/tmp/does/not/exist"
,
Args
:
fakeArgs
}
cmd
:=
&
Command
{
RootDir
:
"/tmp/does/not/exist"
,
Args
:
fakeArgs
}
require
.
Error
(
t
,
cmd
.
Execute
(
nil
))
require
.
Error
(
t
,
cmd
.
Execute
())
}
}
go/internal/command/twofactorrecover/twofactorrecover.go
View file @
60280bbf
...
@@ -13,31 +13,32 @@ import (
...
@@ -13,31 +13,32 @@ import (
type
Command
struct
{
type
Command
struct
{
Config
*
config
.
Config
Config
*
config
.
Config
Args
*
commandargs
.
CommandArgs
Args
*
commandargs
.
CommandArgs
ReadWriter
*
readwriter
.
ReadWriter
}
}
func
(
c
*
Command
)
Execute
(
readWriter
*
readwriter
.
ReadWriter
)
error
{
func
(
c
*
Command
)
Execute
()
error
{
if
c
.
canContinue
(
readWriter
)
{
if
c
.
canContinue
()
{
c
.
displayRecoveryCodes
(
readWriter
)
c
.
displayRecoveryCodes
()
}
else
{
}
else
{
fmt
.
Fprintln
(
r
eadWriter
.
Out
,
"
\n
New recovery codes have *not* been generated. Existing codes will remain valid."
)
fmt
.
Fprintln
(
c
.
R
eadWriter
.
Out
,
"
\n
New recovery codes have *not* been generated. Existing codes will remain valid."
)
}
}
return
nil
return
nil
}
}
func
(
c
*
Command
)
canContinue
(
readWriter
*
readwriter
.
ReadWriter
)
bool
{
func
(
c
*
Command
)
canContinue
()
bool
{
question
:=
question
:=
"Are you sure you want to generate new two-factor recovery codes?
\n
"
+
"Are you sure you want to generate new two-factor recovery codes?
\n
"
+
"Any existing recovery codes you saved will be invalidated. (yes/no)"
"Any existing recovery codes you saved will be invalidated. (yes/no)"
fmt
.
Fprintln
(
r
eadWriter
.
Out
,
question
)
fmt
.
Fprintln
(
c
.
R
eadWriter
.
Out
,
question
)
var
answer
string
var
answer
string
fmt
.
Fscanln
(
r
eadWriter
.
In
,
&
answer
)
fmt
.
Fscanln
(
c
.
R
eadWriter
.
In
,
&
answer
)
return
answer
==
"yes"
return
answer
==
"yes"
}
}
func
(
c
*
Command
)
displayRecoveryCodes
(
readWriter
*
readwriter
.
ReadWriter
)
{
func
(
c
*
Command
)
displayRecoveryCodes
()
{
codes
,
err
:=
c
.
getRecoveryCodes
()
codes
,
err
:=
c
.
getRecoveryCodes
()
if
err
==
nil
{
if
err
==
nil
{
...
@@ -47,9 +48,9 @@ func (c *Command) displayRecoveryCodes(readWriter *readwriter.ReadWriter) {
...
@@ -47,9 +48,9 @@ func (c *Command) displayRecoveryCodes(readWriter *readwriter.ReadWriter) {
"
\n\n
During sign in, use one of the codes above when prompted for
\n
"
+
"
\n\n
During sign in, use one of the codes above when prompted for
\n
"
+
"your two-factor code. Then, visit your Profile Settings and add
\n
"
+
"your two-factor code. Then, visit your Profile Settings and add
\n
"
+
"a new device so you do not lose access to your account again.
\n
"
"a new device so you do not lose access to your account again.
\n
"
fmt
.
Fprint
(
r
eadWriter
.
Out
,
messageWithCodes
)
fmt
.
Fprint
(
c
.
R
eadWriter
.
Out
,
messageWithCodes
)
}
else
{
}
else
{
fmt
.
Fprintf
(
r
eadWriter
.
Out
,
"
\n
An error occurred while trying to generate new recovery codes.
\n
%v
\n
"
,
err
)
fmt
.
Fprintf
(
c
.
R
eadWriter
.
Out
,
"
\n
An error occurred while trying to generate new recovery codes.
\n
%v
\n
"
,
err
)
}
}
}
}
...
...
go/internal/command/twofactorrecover/twofactorrecover_test.go
View file @
60280bbf
...
@@ -122,9 +122,13 @@ func TestExecute(t *testing.T) {
...
@@ -122,9 +122,13 @@ func TestExecute(t *testing.T) {
output
:=
&
bytes
.
Buffer
{}
output
:=
&
bytes
.
Buffer
{}
input
:=
bytes
.
NewBufferString
(
tc
.
answer
)
input
:=
bytes
.
NewBufferString
(
tc
.
answer
)
cmd
:=
&
Command
{
Config
:
&
config
.
Config
{
GitlabUrl
:
url
},
Args
:
tc
.
arguments
}
cmd
:=
&
Command
{
Config
:
&
config
.
Config
{
GitlabUrl
:
url
},
Args
:
tc
.
arguments
,
ReadWriter
:
&
readwriter
.
ReadWriter
{
Out
:
output
,
In
:
input
},
}
err
:=
cmd
.
Execute
(
&
readwriter
.
ReadWriter
{
Out
:
output
,
In
:
input
}
)
err
:=
cmd
.
Execute
()
assert
.
NoError
(
t
,
err
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
tc
.
expectedOutput
,
output
.
String
())
assert
.
Equal
(
t
,
tc
.
expectedOutput
,
output
.
String
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment