-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move Saver, Submitter, and InputProcessor to oonirun (#1543)
My current objective is to cleanup and rationalize the engine package such that it is more explicit how we're using probeservices APIs. In performing this task, I noticed that Saver and InputProcessor are only ever used by the oonirun package. This happens because: 1. the Saver is just a tiny wrapper around SaveMeasurement that constructs either a model.Saver or a fake one depending on the configuration, which seems a pretty specific use case of oonirun given the current API we're using; 2. we can same basically the same for the Submitter; 3. the InputProcessor, albeit nice, is probably not flexible enough to be used elsewhere unless we hammer it a lot. Additionally, in the future, ./cmd/ooniprobe should probably be based on oonirun, and possibly also ./pkg/oonimkall. Also, ./internal/cmd/miniooni is already based on oonirun. To conclude, oonirun should be the focus of future work and changes, while the functionality I'm moving here, could either be in engine or oonirun. But my current goal is to focus on the engine, so it's fine to move these types. Part of ooni/probe#2700
- Loading branch information
1 parent
244dd1e
commit 75c36e6
Showing
13 changed files
with
206 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package engine | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
) | ||
|
||
// SaveMeasurement saves a measurement on the specified file path. | ||
func SaveMeasurement(measurement *model.Measurement, filePath string) error { | ||
return saveMeasurement( | ||
measurement, filePath, json.Marshal, os.OpenFile, | ||
func(fp *os.File, b []byte) (int, error) { | ||
return fp.Write(b) | ||
}, | ||
) | ||
} | ||
|
||
func saveMeasurement( | ||
measurement *model.Measurement, filePath string, | ||
marshal func(v interface{}) ([]byte, error), | ||
openFile func(name string, flag int, perm os.FileMode) (*os.File, error), | ||
write func(fp *os.File, b []byte) (n int, err error), | ||
) error { | ||
data, err := marshal(measurement) | ||
if err != nil { | ||
return err | ||
} | ||
data = append(data, byte('\n')) | ||
filep, err := openFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := write(filep, data); err != nil { | ||
return err | ||
} | ||
return filep.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.