-
Notifications
You must be signed in to change notification settings - Fork 112
Add tests & small refactoring of recipedb #895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
burov
wants to merge
2
commits into
GoogleCloudPlatform:master
Choose a base branch
from
burov:testing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,10 +16,12 @@ package recipes | |
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "sort" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
@@ -29,76 +31,108 @@ var ( | |
| dbFileName = "osconfig_recipedb" | ||
| ) | ||
|
|
||
| type timeFunc func() time.Time | ||
|
|
||
| // RecipeDB represents local state of installed recipes. | ||
| type RecipeDB map[string]Recipe | ||
| type recipeDB struct { | ||
| file string | ||
| timeFunc timeFunc | ||
|
|
||
| // newRecipeDB instantiates a recipeDB. | ||
| func newRecipeDB() (RecipeDB, error) { | ||
| db := make(RecipeDB) | ||
| f, err := os.Open(filepath.Join(getDbDir(), dbFileName)) | ||
| recipes map[string]Recipe | ||
| } | ||
|
|
||
| func newRecipeDB(path string) (*recipeDB, error) { | ||
| db := &recipeDB{ | ||
| file: path, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe dbFile? |
||
| timeFunc: time.Now, | ||
| recipes: make(map[string]Recipe, 0), | ||
| } | ||
|
|
||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return db, nil | ||
| } | ||
|
|
||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
| bytes, err := ioutil.ReadAll(f) | ||
|
|
||
| raw, err := io.ReadAll(f) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var recipelist []Recipe | ||
| if err := json.Unmarshal(bytes, &recipelist); err != nil { | ||
|
|
||
| var recipes []Recipe | ||
| if err := json.Unmarshal(raw, &recipes); err != nil { | ||
| return nil, err | ||
| } | ||
| for _, recipe := range recipelist { | ||
| db[recipe.Name] = recipe | ||
|
|
||
| for _, recipe := range recipes { | ||
| db.recipes[recipe.Name] = recipe | ||
| } | ||
| return db, nil | ||
| } | ||
|
|
||
| // newRecipeDB instantiates a recipeDB. | ||
| func newRecipeDBWithDefaults() (*recipeDB, error) { | ||
| dir, fileName := getDbDir(), dbFileName | ||
| return newRecipeDB(filepath.Join(dir, fileName)) | ||
| } | ||
|
|
||
| // getRecipe returns the Recipe object for the given recipe name. | ||
| func (db RecipeDB) getRecipe(name string) (Recipe, bool) { | ||
| r, ok := db[name] | ||
| func (db *recipeDB) getRecipe(name string) (Recipe, bool) { | ||
| r, ok := db.recipes[name] | ||
| return r, ok | ||
| } | ||
|
|
||
| // addRecipe marks a recipe as installed. | ||
| func (db RecipeDB) addRecipe(name, version string, success bool) error { | ||
| func (db *recipeDB) addRecipe(name, version string, success bool) error { | ||
| versionNum, err := convertVersion(version) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| db[name] = Recipe{Name: name, Version: versionNum, InstallTime: time.Now().Unix(), Success: success} | ||
| db.recipes[name] = Recipe{Name: name, Version: versionNum, InstallTime: db.timeFunc().Unix(), Success: success} | ||
|
|
||
| var recipelist []Recipe | ||
| for _, recipe := range db { | ||
| recipelist = append(recipelist, recipe) | ||
| return db.saveToFS() | ||
| } | ||
|
|
||
| func (db *recipeDB) saveToFS() error { | ||
| var recipes []Recipe | ||
| for _, recipe := range db.recipes { | ||
| recipes = append(recipes, recipe) | ||
| } | ||
| dbBytes, err := json.Marshal(recipelist) | ||
|
|
||
| sort.Slice(recipes, func(i, j int) bool { | ||
| return recipes[i].Name < recipes[j].Name | ||
| }) | ||
|
|
||
| raw, err := json.Marshal(recipes) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| dbDir := getDbDir() | ||
| if err := os.MkdirAll(dbDir, 0755); err != nil { | ||
| dir := filepath.Dir(db.file) | ||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| f, err := ioutil.TempFile(dbDir, dbFileName+"_*") | ||
| fileName := filepath.Base(db.file) | ||
| f, err := ioutil.TempFile(dir, fileName+"_*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := f.Write(dbBytes); err != nil { | ||
| if _, err := f.Write(raw); err != nil { | ||
| f.Close() | ||
| return err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also delete the temp file when writing error occurs? |
||
| } | ||
|
|
||
| if err := f.Close(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return os.Rename(f.Name(), filepath.Join(dbDir, dbFileName)) | ||
| return os.Rename(f.Name(), db.file) | ||
| } | ||
|
|
||
| func getDbDir() string { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also take a
timeFunchere as an argument? This would result with a clear separation between a factory method that creates from all components and one that uses defaults.