-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
81 additions
and
72 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package initialization | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spacemeshos/post/shared" | ||
) | ||
|
||
const MetadataFileName = "postdata_metadata.json" | ||
|
||
func SaveMetadata(dir string, v *shared.PostMetadata) error { | ||
err := os.MkdirAll(dir, shared.OwnerReadWriteExec) | ||
if err != nil && !os.IsExist(err) { | ||
return fmt.Errorf("dir creation failure: %w", err) | ||
} | ||
|
||
data, err := json.Marshal(v) | ||
if err != nil { | ||
return fmt.Errorf("serialization failure: %w", err) | ||
} | ||
|
||
err = os.WriteFile(filepath.Join(dir, MetadataFileName), data, shared.OwnerReadWrite) | ||
if err != nil { | ||
return fmt.Errorf("write to disk failure: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func LoadMetadata(dir string) (*shared.PostMetadata, error) { | ||
filename := filepath.Join(dir, MetadataFileName) | ||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil, ErrStateMetadataFileMissing | ||
} | ||
return nil, fmt.Errorf("read file failure: %w", err) | ||
} | ||
|
||
metadata := shared.PostMetadata{} | ||
if err := json.Unmarshal(data, &metadata); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &metadata, nil | ||
} |
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
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