A small application to manage Lilypond music repositories
at main 92 lines 2.4 kB view raw
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 "io/ioutil" 19 "os" 20 "path/filepath" 21 22 log "github.com/Sirupsen/logrus" 23 "github.com/jjdekker/ponder/helpers" 24 "github.com/spf13/cobra" 25) 26 27var ( 28 settingsFile = "ponder.json" 29 settingsTemplate = []byte(`{ "default" : { 30 "Name": "", 31 "Author": "", 32 "IgnoreDirs": [".git"], 33 "LilypondIncludes": [], 34 "OutputDir": "out" 35 } 36}`) 37 gitIgnoreTemplate = []byte(`# Output Folder 38out/`) 39) 40 41// initCmd represents the init command 42var initCmd = &cobra.Command{ 43 Use: "init [location]", 44 Short: "Initialize a Ponder Library", 45 Long: `Initialize (ponder init) will create a new library, with a ponder 46settings file and corresponding git ignore file. 47 48 * If a name is provided, it will be created in the current directory; 49 * If no name is provided, the current directory will be assumed; 50Init will not use an existing directory with contents.`, 51 Run: func(cmd *cobra.Command, args []string) { 52 var path string 53 var err error 54 switch len(args) { 55 case 0: 56 path, err = helpers.CleanPath("") 57 case 1: 58 path, err = helpers.CleanPath(args[0]) 59 default: 60 log.Fatal("init command does not support more than 1 parameter") 61 } 62 helpers.Check(err, "Given path is invalid") 63 64 initializePath(path) 65 }, 66} 67 68func initializePath(path string) { 69 if !helpers.Exists(path) { 70 err := os.MkdirAll(path, os.ModePerm) 71 helpers.Check(err, "Could not create directory") 72 } 73 74 createFile(filepath.Join(path, settingsFile), settingsTemplate) 75 createFile(filepath.Join(path, ".gitignore"), gitIgnoreTemplate) 76} 77 78func createFile(path string, content []byte) { 79 b := helpers.Exists(path) 80 81 if !b { 82 err := ioutil.WriteFile(path, content, 0644) 83 if err != nil { 84 log.WithFields(log.Fields{"error": err, "path": path}). 85 Fatal("Unable to create settings file") 86 } 87 } 88} 89 90func init() { 91 RootCmd.AddCommand(initCmd) 92}