Archived
1
0

Completes json creation command

This commit is contained in:
Jip J. Dekker 2016-04-05 17:57:47 +02:00
parent 9a3375c477
commit 3189fc9c71
2 changed files with 23 additions and 4 deletions

View File

@ -36,7 +36,7 @@ The information saved in the json file will be used when compiling the songbook.
switch len(args) { switch len(args) {
case 1: case 1:
path, err = helpers.CleanPath(args[0]) path, err = helpers.CleanPath(args[0])
helpers.Check(err, "Unable to ") helpers.Check(err, "Unable to create valid path")
default: default:
log.Fatal("the add command needs exactly 1 parameter") log.Fatal("the add command needs exactly 1 parameter")
} }

View File

@ -16,18 +16,22 @@ package settings
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath"
"strings"
"time" "time"
log "github.com/Sirupsen/logrus"
"github.com/jjdekker/ponder/helpers" "github.com/jjdekker/ponder/helpers"
) )
// Score represents the settings for a specific score file // Score represents the settings for a specific score file
type Score struct { type Score struct {
Name string // The name of the score in the songbook Name string // The name of the score in the songbook
Categories []string // Categories to which the scores belong Categories []string `json:"omitempty"` // Categories to which the scores belong
Path string // The path to the scores (uncompiled) file Path string // The path to the scores (uncompiled) file
LastModified time.Time // Time when the score source was last modified (will be set internally) LastModified time.Time `json:"omitempty"` // Time when the score source was last modified (will be set internally)
} }
// FromJSON reads the settings of a score from a JSON file // FromJSON reads the settings of a score from a JSON file
@ -47,6 +51,21 @@ func FromJSON(path string) (*Score, error) {
return &s, nil return &s, nil
} }
// CreateScore creates a json file for a score given its path
func CreateScore(path string) { func CreateScore(path string) {
if filepath.Ext(path) != ".pdf" {
log.WithFields(log.Fields{"path": path}).
Warning("Unsupported sheet music file")
}
jsonPath := path[:strings.LastIndex(path, ".")]
fmt.Println(jsonPath)
s := Score{
Path: path,
Name: filepath.Base(jsonPath),
// TODO: Add folder as category when not in main folder
}
data, err := json.MarshalIndent(s, "", " ")
helpers.Check(err, "Unable to generate valid json")
err = ioutil.WriteFile(jsonPath+".json", data, 0644)
helpers.Check(err, "Unable to save json to file")
} }