From 0f8b31f7a6eef25c3d8a8d7381213b41e3765bcb Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Tue, 5 Apr 2016 18:12:45 +0200 Subject: [PATCH] Use directory as category if not package directory --- cmd/add.go | 3 ++- settings/score.go | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 33384a4..0f8a88f 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -40,7 +40,8 @@ The information saved in the json file will be used when compiling the songbook. default: log.Fatal("the add command needs exactly 1 parameter") } - settings.CreateScore(path) + dir, _ := getSettings() + settings.CreateScore(path, dir) }, } diff --git a/settings/score.go b/settings/score.go index ade4e2d..837bef3 100644 --- a/settings/score.go +++ b/settings/score.go @@ -16,7 +16,6 @@ package settings import ( "encoding/json" - "fmt" "io/ioutil" "path/filepath" "strings" @@ -29,9 +28,9 @@ import ( // Score represents the settings for a specific score file type Score struct { Name string // The name of the score in the songbook - Categories []string `json:"omitempty"` // 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 `json:"omitempty"` // Time when the score source was last modified (will be set internally) + LastModified time.Time `json:"-"` // Time when the score source was last modified (will be set internally) } // FromJSON reads the settings of a score from a JSON file @@ -52,18 +51,22 @@ func FromJSON(path string) (*Score, error) { } // CreateScore creates a json file for a score given its path -func CreateScore(path string) { +func CreateScore(path, workDir 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 } + + if filepath.Dir(path) != workDir { + s.Categories = []string{filepath.Base(filepath.Dir(path))} + } + data, err := json.MarshalIndent(s, "", " ") helpers.Check(err, "Unable to generate valid json") err = ioutil.WriteFile(jsonPath+".json", data, 0644)