grim/hgkeeper
Clone
Summary
Browse
Changes
Graph
Serve all static files from /static and handle it from go instead of passing it to python. Fixes HGKEEPER-7
2020-04-24, Gary Kramlich
fbcab4551b60
Serve all static files from /static and handle it from go instead of passing it to python. Fixes HGKEEPER-7
package
commands
import
(
"io"
"sync"
"github.com/gliderlabs/ssh"
log
"github.com/sirupsen/logrus"
"bitbucket.org/rw_grim/hgkeeper/hg"
)
func
run
(
hgCmd
*
hg
.
Command
,
session
ssh
.
Session
)
error
{
cmd
:=
hgCmd
.
Cmd
()
if
err
:=
hgCmd
.
Setup
();
err
!=
nil
{
return
err
}
defer
hgCmd
.
Teardown
()
stdout
,
err
:=
cmd
.
StdoutPipe
()
if
err
!=
nil
{
return
err
}
stderr
,
err
:=
cmd
.
StderrPipe
()
if
err
!=
nil
{
return
err
}
stdin
,
err
:=
cmd
.
StdinPipe
()
if
err
!=
nil
{
return
err
}
wg
:=
&
sync
.
WaitGroup
{}
// we wait for stderr and stdout to finish
wg
.
Add
(
2
)
if
err
:=
cmd
.
Start
();
err
!=
nil
{
return
err
}
go
func
()
{
defer
stdin
.
Close
()
if
_
,
err
:=
io
.
Copy
(
stdin
,
session
);
err
!=
nil
{
log
.
Errorf
(
"Failed to read stdin from session: %v"
,
err
)
}
}()
go
func
()
{
defer
wg
.
Done
()
if
_
,
err
:=
io
.
Copy
(
session
,
stdout
);
err
!=
nil
{
log
.
Errorf
(
"Failed to write stdout to session: %v"
,
err
)
}
}()
go
func
()
{
defer
wg
.
Done
()
if
_
,
err
:=
io
.
Copy
(
session
.
Stderr
(),
stderr
);
err
!=
nil
{
log
.
Errorf
(
"Failed to write stderr to session: %v"
,
err
)
}
}()
// wait until all output is processed
wg
.
Wait
()
return
cmd
.
Wait
()
}