Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a4901b9ae7
commit
d27eca0a07
13 changed files with 71 additions and 57 deletions
43
internal/json/main.go
Normal file
43
internal/json/main.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/ansi"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func AsJSON(zets []string) (string, error) {
|
||||
var items []Item
|
||||
|
||||
for _, entry := range zets {
|
||||
cleanEntry := ansi.StripANSI(entry)
|
||||
parts := strings.SplitN(cleanEntry, " ", 2)
|
||||
|
||||
id, _ := strconv.Atoi(parts[0])
|
||||
|
||||
item := Item{
|
||||
ID: id,
|
||||
Title: parts[1],
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(items, "", " ")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling to JSON:", err)
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(jsonData), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue