From 3189fc9c7105e2edba3c7ba12033bbe196ec0845 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Tue, 5 Apr 2016 17:57:47 +0200 Subject: [PATCH] Completes json creation command --- cmd/add.go | 2 +- settings/score.go | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 3be9a61..33384a4 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -36,7 +36,7 @@ The information saved in the json file will be used when compiling the songbook. switch len(args) { case 1: path, err = helpers.CleanPath(args[0]) - helpers.Check(err, "Unable to ") + helpers.Check(err, "Unable to create valid path") default: log.Fatal("the add command needs exactly 1 parameter") } diff --git a/settings/score.go b/settings/score.go index b56ae03..ade4e2d 100644 --- a/settings/score.go +++ b/settings/score.go @@ -16,18 +16,22 @@ package settings import ( "encoding/json" + "fmt" "io/ioutil" + "path/filepath" + "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/jjdekker/ponder/helpers" ) // Score represents the settings for a specific score file type Score struct { 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 - 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 @@ -47,6 +51,21 @@ func FromJSON(path string) (*Score, error) { return &s, nil } +// CreateScore creates a json file for a score given its path 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") }