Skip to content

Commit 3dfe784

Browse files
authored
Adds write-to-file flag to store data locally (#180)
* Adds write-to-file flag to store data locally Only stores data locally if no organization is specified. This can be added if needed. Signed-off-by: Jamie Leppard <[email protected]> * Restructure gatherAndPostData to allow local write gatherAndPostData has been split into: gatherData which returns config information, the prefight client and readings. postData which sends this data to preflight server as previously happened in gatherAndPostData The outputpath flag now determines if postData is called. If the flag is set, postData is not called and the data is written locally. This also fixes the issue of the flag only working when no org is set. Signed-off-by: Jamie Leppard <[email protected]> * Breaks down gatherData into smaller functions gatherData is broken down into: getConfiguration - returns config and preflight client this is now implemented outside of gatherAndOutputData in the main Run loop gatherData - creates data gatherers and generates readings This is run inside gatherAndOutputData as previously done Signed-off-by: Jamie Leppard <[email protected]> Co-authored-by: Jamie Leppard <[email protected]>
1 parent 99edf3f commit 3dfe784

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

cmd/agent.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,12 @@ func init() {
6464
false,
6565
"Runs agent a single time if true, or continously if false",
6666
)
67+
agentCmd.PersistentFlags().StringVarP(
68+
&agent.OutputPath,
69+
"output-path",
70+
"",
71+
"",
72+
"Output file path, if used will write data to a local file instead of uploading to the preflight server",
73+
)
6774

6875
}

pkg/agent/run.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,23 @@ var OneShot bool
3434
// CredentialsPath is where the agent will try to loads the credentials. (Experimental)
3535
var CredentialsPath string
3636

37+
// OutputPath is where the agent will write data to locally if specified
38+
var OutputPath string
3739

3840
// Run starts the agent process
3941
func Run(cmd *cobra.Command, args []string) {
4042
ctx := context.Background()
4143
for {
42-
gatherAndPostData(ctx)
44+
config, preflightClient := getConfiguration(ctx)
45+
gatherAndOutputData(ctx, config, preflightClient)
4346
if OneShot {
4447
break
4548
}
4649
time.Sleep(time.Duration(Period) * time.Second)
4750
}
4851
}
4952

50-
func gatherAndPostData(ctx context.Context) {
53+
func getConfiguration(ctx context.Context) (Config, *client.PreflightClient) {
5154
log.Printf("Preflight agent version: %s (%s)", version.PreflightVersion, version.Commit)
5255
file, err := os.Open(ConfigFilePath)
5356
if err != nil {
@@ -127,6 +130,26 @@ func gatherAndPostData(ctx context.Context) {
127130
}
128131
}
129132

133+
return config, preflightClient
134+
}
135+
136+
func gatherAndOutputData(ctx context.Context, config Config, preflightClient *client.PreflightClient) {
137+
readings := gatherData(ctx, config)
138+
139+
if OutputPath != "" {
140+
data, err := json.MarshalIndent(readings, " ", " ")
141+
err = ioutil.WriteFile(OutputPath, data, 0644)
142+
if err != nil {
143+
log.Fatal(err)
144+
}
145+
log.Println("Data saved locally to", OutputPath)
146+
} else {
147+
postData(config, preflightClient, readings)
148+
}
149+
}
150+
151+
func gatherData(ctx context.Context, config Config) []*api.DataReading {
152+
130153
dataGatherers := make(map[string]datagatherer.DataGatherer)
131154

132155
for _, dgConfig := range config.DataGatherers {
@@ -172,6 +195,11 @@ func gatherAndPostData(ctx context.Context) {
172195
strings.Join(failedDataGatherers, ", "),
173196
)
174197
}
198+
return readings
199+
}
200+
201+
func postData(config Config, preflightClient *client.PreflightClient, readings []*api.DataReading) {
202+
baseURL := config.Server
175203

176204
log.Println("Running Agent...")
177205
log.Println("Posting data to ", baseURL)
@@ -185,6 +213,7 @@ func gatherAndPostData(ctx context.Context) {
185213
path = "/api/v1/datareadings"
186214
}
187215
res, err := preflightClient.Post(path, bytes.NewBuffer(data))
216+
188217
if err != nil {
189218
log.Fatalf("Failed to post data: %+v", err)
190219
}
@@ -199,7 +228,7 @@ func gatherAndPostData(ctx context.Context) {
199228
log.Fatalf("Received response with status code %d. Body: %s", code, errorContent)
200229
}
201230
} else {
202-
err = preflightClient.PostDataReadings(config.OrganizationID, readings)
231+
err := preflightClient.PostDataReadings(config.OrganizationID, readings)
203232
// TODO: handle errors gracefully: e.g. handle retries when it is possible
204233
if err != nil {
205234
log.Fatalf("Post to server failed: %+v", err)

0 commit comments

Comments
 (0)