A small application to manage Lilypond music repositories
1// Copyright © 2016 Jip J. Dekker <jip@dekker.li>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cmd
16
17import (
18 "os"
19 "path/filepath"
20
21 log "github.com/Sirupsen/logrus"
22 "github.com/jjdekker/ponder/helpers"
23 "github.com/jjdekker/ponder/settings"
24 "github.com/spf13/cobra"
25)
26
27var (
28 veryVerbose bool
29 verbose bool
30 target string
31)
32
33// RootCmd represents the base command when called without any subcommands
34var RootCmd = &cobra.Command{
35 Use: "ponder",
36 Short: "A managing tool for lilypond sheet music libraries",
37 Long: `Ponder is a tool to help manage your sheet music library. The main
38purpose is to help in the compilation of your lilypond files into both single
39files and a fully functioning song book. It also accepts other PDF files to be
40part of your song book.`,
41 PersistentPreRun: func(cmd *cobra.Command, args []string) {
42 setLogLevel()
43 },
44 // Uncomment the following line if your bare application
45 // has an action associated with it:
46 // Run: func(cmd *cobra.Command, args []string) { },
47}
48
49// Execute adds all child commands to the root command sets flags appropriately.
50// This is called by main.main(). It only needs to happen once to the rootCmd.
51func Execute() {
52 if err := RootCmd.Execute(); err != nil {
53 os.Exit(-1)
54 }
55}
56
57func init() {
58 RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output of application events")
59 RootCmd.PersistentFlags().BoolVar(&veryVerbose, "vv", false, "Debug output of application events")
60 RootCmd.PersistentFlags().StringVarP(&target, "target", "t", "default", "Settings target to be used")
61}
62
63func setLogLevel() {
64 if veryVerbose {
65 log.SetLevel(log.DebugLevel)
66 } else if verbose {
67 log.SetLevel(log.InfoLevel)
68 } else {
69 log.SetLevel(log.WarnLevel)
70 }
71}
72
73// getSettings return the directory in which the settings file was
74// found and the parsed settings content
75func getSettings() (string, *settings.Settings) {
76 // Find and Unmarshal the settings file
77 path, err := helpers.FindFileDir(settingsFile)
78 helpers.Check(err, "unable to find library directory")
79 opts, err := settings.FromFile(filepath.Join(path, settingsFile))
80 helpers.Check(err, "unable to parse settings file")
81
82 set, ok := opts[target]
83 if !ok {
84 helpers.Crash(nil, "unknown compilation target: "+target)
85 }
86
87 return path, &set
88}