Skip to content

Commit d0f0693

Browse files
authored
Adds input-path flag to load data locally (#182)
Flag allows data to be read from file instead of collected from clusters using data gatherers Signed-off-by: Jamie Leppard <[email protected]> Co-authored-by: Jamie Leppard <[email protected]>
1 parent 3dfe784 commit d0f0693

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

cmd/agent.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ func init() {
6969
"output-path",
7070
"",
7171
"",
72-
"Output file path, if used will write data to a local file instead of uploading to the preflight server",
72+
"Output file path, if used, it will write data to a local file instead of uploading to the preflight server",
73+
)
74+
agentCmd.PersistentFlags().StringVarP(
75+
&agent.InputPath,
76+
"input-path",
77+
"",
78+
"",
79+
"Input file path, if used, it will read data from a local file instead of gathering data from clusters",
7380
)
7481

7582
}

pkg/agent/run.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ var CredentialsPath string
3737
// OutputPath is where the agent will write data to locally if specified
3838
var OutputPath string
3939

40+
// InputPath is where the agent will read data from instead of gathering from clusters if specified
41+
var InputPath string
42+
4043
// Run starts the agent process
4144
func Run(cmd *cobra.Command, args []string) {
4245
ctx := context.Background()
@@ -134,7 +137,21 @@ func getConfiguration(ctx context.Context) (Config, *client.PreflightClient) {
134137
}
135138

136139
func gatherAndOutputData(ctx context.Context, config Config, preflightClient *client.PreflightClient) {
137-
readings := gatherData(ctx, config)
140+
var readings []*api.DataReading
141+
if InputPath != "" {
142+
log.Println("Reading data from", InputPath)
143+
data, err := ioutil.ReadFile(InputPath)
144+
if err != nil {
145+
log.Fatal(err)
146+
}
147+
err = json.Unmarshal(data, &readings)
148+
if err != nil {
149+
log.Fatal(err)
150+
}
151+
log.Println("Data read successfully.")
152+
} else {
153+
readings = gatherData(ctx, config)
154+
}
138155

139156
if OutputPath != "" {
140157
data, err := json.MarshalIndent(readings, " ", " ")

0 commit comments

Comments
 (0)