-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaemon_internal.go
108 lines (93 loc) · 2.76 KB
/
daemon_internal.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
// Copyright 2016, 2024 The TrueBlocks Authors. All rights reserved.
// Use of this source code is governed by a license that can
// be found in the LICENSE file.
/*
* Parts of this file were auto generated. Edit only those parts of
* the code inside of 'EXISTING_CODE' tags.
*/
package sdk
import (
// EXISTING_CODE
"encoding/json"
"fmt"
"io"
"strings"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
daemon "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/sdk"
// EXISTING_CODE
)
type daemonOptionsInternal struct {
Url string `json:"url,omitempty"`
Api DaemonApi `json:"api,omitempty"`
Scrape DaemonScrape `json:"scrape,omitempty"`
Monitor bool `json:"monitor,omitempty"`
Silent bool `json:"silent,omitempty"`
RenderCtx *output.RenderCtx `json:"-"`
Globals
}
// String implements the stringer interface
func (opts *daemonOptionsInternal) String() string {
bytes, _ := json.Marshal(opts)
return string(bytes)
}
// DaemonBytes implements the chifra daemon command for the SDK.
func (opts *daemonOptionsInternal) DaemonBytes(w io.Writer) error {
values, err := structToValues(*opts)
if err != nil {
return fmt.Errorf("error converting daemon struct to URL values: %v", err)
}
if opts.RenderCtx == nil {
opts.RenderCtx = output.NewRenderContext()
}
return daemon.Daemon(opts.RenderCtx, w, values)
}
// daemonParseFunc handles special cases such as structs and enums (if any).
func daemonParseFunc(target any, key, value string) (bool, error) {
var found bool
opts, ok := target.(*daemonOptionsInternal)
if !ok {
return false, fmt.Errorf("parseFunc(daemon): target is not of correct type")
}
if key == "api" {
var err error
values := strings.Split(value, ",")
if opts.Api, err = enumFromDaemonApi(values); err != nil {
return false, err
} else {
found = true
}
}
if key == "scrape" {
var err error
values := strings.Split(value, ",")
if opts.Scrape, err = enumFromDaemonScrape(values); err != nil {
return false, err
} else {
found = true
}
}
// EXISTING_CODE
// EXISTING_CODE
return found, nil
}
// GetDaemonOptions returns a filled-in options instance given a string array of arguments.
func GetDaemonOptions(args []string) (*daemonOptionsInternal, error) {
var opts daemonOptionsInternal
if err := assignValuesFromArgs(args, daemonParseFunc, &opts, &opts.Globals); err != nil {
return nil, err
}
return &opts, nil
}
// EXISTING_CODE
func (opts *DaemonOptions) toInternal() *daemonOptionsInternal {
return &daemonOptionsInternal{
Url: opts.Url,
Api: opts.Api,
Scrape: opts.Scrape,
Monitor: opts.Monitor,
Silent: opts.Silent,
RenderCtx: opts.RenderCtx,
Globals: opts.Globals,
}
}
// EXISTING_CODE