-
Notifications
You must be signed in to change notification settings - Fork 6
/
root.go
153 lines (132 loc) · 4.14 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2017 The Reposeed Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/gobuffalo/packr"
"github.com/spf13/cobra"
"go.okkur.org/reposeed/cmd/config"
templatesBox "go.okkur.org/reposeed/cmd/templates"
yaml "gopkg.in/yaml.v2"
)
var outputDir, conf string
var overwrite bool
var rootCmd = &cobra.Command{
Use: "reposeed",
Short: "Extensive repository base files up and running in an instant",
Long: `
Start with the base layer necessary to focus on your project and not on the repository.
Licensing, structure, documentation and more boilerplate to get you started from your first commit.`,
Example: "reposeed -c .seed-config.yaml -o ~/myproject",
Run: RootHandler,
}
func init() {
rootCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "Output directory")
rootCmd.PersistentFlags().StringVarP(&conf, "conf", "c", ".seed-config.yaml", "Config file")
rootCmd.PersistentFlags().BoolVarP(&overwrite, "overwrite", "w", false, "Force overwrite files")
}
// RootHandler handles main cli entrypoint with cobra
func RootHandler(cmd *cobra.Command, args []string) {
box := templatesBox.GetTemplates()
config := parseConfig(conf)
bl := make(map[string]bool)
bl["seed-config.example.yaml"] = true
configVersion := config.Reposeed.ConfigVersion
templates := parseTemplates(box)
if configVersion == SupportedConfigVersion {
for _, templateName := range box.List() {
file, _ := box.Open(templateName)
fileStat, _ := file.Stat()
if bl[fileStat.Name()] {
log.Printf("Skipped %s", fileStat.Name())
continue
}
if fileStat.IsDir() {
continue
}
if strings.Contains(templateName, "partials/") {
continue
}
err := generateFile(config, templates, templateName, overwrite)
if err != nil {
log.Println(err)
}
}
} else {
log.Fatalf("Invalid config version. Currently supported versions: v1")
}
}
// Execute executes a specific command
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Printf("Couldn't execute the command: %s", err.Error())
os.Exit(1)
}
}
// Reads the config file and returns the filled config struct
func parseConfig(path string) config.Config {
var conf config.Config
filename, _ := filepath.Abs(path)
file, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(file, &conf)
if err != nil {
panic(err)
}
return conf
}
// Reads all of the templates from the packr box and returns them
// as a *template.Template instance
func parseTemplates(box packr.Box) *template.Template {
templatesName := box.List()
templates := &template.Template{}
for _, templateName := range templatesName {
templateFile, err := box.Open(templateName)
if err != nil {
log.Fatalf("could not open the template file: %s", templateName)
}
defer templateFile.Close()
templateContent := box.String(templateName)
templates.New(templateName).Parse(templateContent)
}
return templates
}
// Generates the base files using the templates and config struct
func generateFile(config config.Config, templates *template.Template, newPath string, overwrite bool) error {
if _, e := os.Stat(newPath); os.IsNotExist(e) {
os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
}
if !overwrite {
if _, e := os.Stat(newPath); !os.IsNotExist(e) {
return fmt.Errorf("file %s not overwritten", newPath)
}
}
file, err := os.Create(newPath)
if err != nil {
return fmt.Errorf("unable to create file: %s", err)
}
defer file.Close()
err = templates.Lookup(newPath).Execute(file, config)
if err != nil {
return fmt.Errorf("unable to parse template: %s", err)
}
return nil
}