-
Notifications
You must be signed in to change notification settings - Fork 1
/
ondemand.go
78 lines (63 loc) · 1.64 KB
/
ondemand.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
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package command
import (
"context"
"fmt"
"io"
"math"
"time"
"github.com/ctron/hot/pkg/async"
"github.com/ctron/hot/pkg/encoding"
)
// on demand reader
type OnDemandReader struct {
CommandName string
Stream io.Reader
Prompt io.Writer
Encoder encoding.PayloadEncoder
reader async.Reader
}
func (r *OnDemandReader) Start() error {
r.reader = async.NewReader(r.Stream)
return nil
}
func (r *OnDemandReader) Stop() error {
return r.reader.Close()
}
func (r *OnDemandReader) Read(ctx context.Context, _ string) *Command {
if r.Prompt != nil {
deadline, ok := ctx.Deadline()
if !ok {
_, _ = fmt.Fprint(r.Prompt, "Command: ")
} else {
var rem float64 = float64(deadline.Sub(time.Now())) / float64(time.Second)
_, _ = fmt.Fprintf(r.Prompt, "Command (%.0fs): ", math.Ceil(rem))
}
}
payload := r.reader.ReadLine(ctx)
if payload == nil {
return nil
} else {
p, err := r.Encoder.Encode(*payload)
if err != nil {
return nil
}
return &Command{
Command: r.CommandName,
ContentType: r.Encoder.GetMimeType(),
Payload: p,
}
}
}
var _ Reader = &OnDemandReader{}