Start in json mode if stdin contains json msg
parent
95f53bafea
commit
ab680dca8b
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
"git.kettek.net/accord/service/internal/cfg"
|
||||
"git.kettek.net/accord/service/internal/version"
|
||||
"git.kettek.net/accord/service/pkg/istty"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -20,10 +19,25 @@ func main() {
|
|||
readChannel := make(chan AMessage)
|
||||
quitChannel := make(chan error)
|
||||
|
||||
if istty.Is() {
|
||||
fmt.Println("Running from TTY, starting as stand-alone service.")
|
||||
var jsonEncoder *json.Encoder
|
||||
|
||||
stat, _ := os.Stdin.Stat()
|
||||
var startMsg StartMessage
|
||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||
dec := json.NewDecoder(os.Stdin)
|
||||
dec.Decode(&startMsg)
|
||||
}
|
||||
|
||||
if !startMsg.EnableJSON {
|
||||
fmt.Println("No EnableJSON message received, starting as stand-alone service.")
|
||||
fmt.Printf(" Git Id: %s\n", version.GitCommitId)
|
||||
} else {
|
||||
jsonEncoder = json.NewEncoder(os.Stdout)
|
||||
jsonEncoder.Encode(VersionMessage{
|
||||
GitCommitId: version.GitCommitId,
|
||||
})
|
||||
fmt.Fprintf(os.Stderr, "Starting in bi-directional JSON mode")
|
||||
|
||||
// Start our STDIN read loop
|
||||
if cfg.UsePipes {
|
||||
go readLoop(os.Stdin, quitChannel, readChannel)
|
||||
|
@ -34,12 +48,12 @@ func main() {
|
|||
for {
|
||||
select {
|
||||
case msg := <-readChannel:
|
||||
fmt.Printf("%+v\n", msg)
|
||||
fmt.Fprintf(os.Stderr, "%+v\n", msg)
|
||||
case err := <-quitChannel:
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Quitting")
|
||||
fmt.Fprintf(os.Stderr, "Quitting\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +62,16 @@ func main() {
|
|||
// AMessage is our base Accord message.
|
||||
type AMessage struct{}
|
||||
|
||||
// StartMessage is a bogus message for enabling JSON mode
|
||||
type StartMessage struct {
|
||||
EnableJSON bool `json:"enableJSON"`
|
||||
}
|
||||
|
||||
// VersionMessage is a message sent to the frontend immediately after JSON mode has been initiated.
|
||||
type VersionMessage struct {
|
||||
GitCommitId string
|
||||
}
|
||||
|
||||
func readLoop(reader io.Reader, quitChannel chan error, readChannel chan AMessage) {
|
||||
var msg AMessage
|
||||
|
||||
|
|
Loading…
Reference in New Issue