basic functionality works
This commit is contained in:
@@ -13,48 +13,49 @@ type Tags struct {
|
||||
}
|
||||
|
||||
var tags Tags
|
||||
var filename string = "data.json"
|
||||
|
||||
func readTags(filename string) (*Tags, error) {
|
||||
func readTags() {
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(bytes, &tags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tags, nil
|
||||
}
|
||||
|
||||
func writeTags(filename string, tags *Tags) error {
|
||||
jsonBytes, err := json.MarshalIndent(tags, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.WriteFile(filename, jsonBytes, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func addTag(tags *Tags, tagKey string, tagValue string) {
|
||||
tags, err := readTags("data.json")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read tags: %v", err)
|
||||
}
|
||||
tags.Tags[tagKey] = tagValue
|
||||
err = writeTags("data.json", tags)
|
||||
err = json.Unmarshal(bytes, &tags)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read tags: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeTags() {
|
||||
jsonBytes, err := json.MarshalIndent(&tags, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write tags: %v", err)
|
||||
}
|
||||
err = os.WriteFile(filename, jsonBytes, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write tags: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func addTag(tags *Tags, tagKey string, tagValue string) {
|
||||
readTags()
|
||||
tags.Tags[tagKey] = tagValue
|
||||
writeTags()
|
||||
}
|
||||
|
||||
func removeTag(tags *Tags, tagKey string) {
|
||||
readTags()
|
||||
delete(tags.Tags, tagKey)
|
||||
writeTags()
|
||||
}
|
||||
|
||||
func (tags Tags) getTagKeys() []string {
|
||||
readTags()
|
||||
keys := make([]string, 0, len(tags.Tags))
|
||||
for k := range tags.Tags {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func modifyTag(tags *Tags, tagKey string, newTagValue string) {
|
||||
|
Reference in New Issue
Block a user