github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/internal/plugins/ci/generator.go (about) 1 package ci 2 3 import ( 4 "context" 5 "errors" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "text/template" 10 11 "github.com/markbates/pkger" 12 "github.com/markbates/pkger/pkging" 13 "github.com/spf13/pflag" 14 ) 15 16 type Generator struct { 17 provider string 18 flags *pflag.FlagSet 19 } 20 21 func (Generator) PluginName() string { 22 return "ci" 23 } 24 25 func (Generator) Description() string { 26 return "Generates CI configuration file" 27 } 28 29 func (g Generator) Newapp(ctx context.Context, root string, name string, args []string) error { 30 f, err := g.buildFiles(root) 31 if err != nil { 32 return err 33 } 34 35 tmpl, err := g.buildTemplate() 36 if err != nil { 37 return err 38 } 39 40 data := struct { 41 Name string 42 Database string 43 BuffaloVersion string 44 }{ 45 Name: name, 46 Database: g.dbProvider(args), 47 BuffaloVersion: "", 48 } 49 50 err = tmpl.Execute(f, data) 51 if err != nil { 52 return err 53 } 54 55 return nil 56 } 57 58 func (g Generator) buildTemplate() (*template.Template, error) { 59 file, err := g.templateFile() 60 if err != nil { 61 return nil, err 62 } 63 defer file.Close() 64 65 t, err := ioutil.ReadAll(file) 66 if err != nil { 67 return nil, err 68 } 69 70 template, err := template.New("ci-file").Parse(string(t)) 71 if err != nil { 72 return nil, err 73 } 74 75 return template, nil 76 } 77 78 func (g Generator) buildFiles(root string) (*os.File, error) { 79 var f *os.File 80 var err error 81 82 switch g.provider { 83 case "travis": 84 f, err = os.Create(filepath.Join(root, ".travis.yml")) 85 if err != nil { 86 return nil, err 87 } 88 case "gitlab": 89 f, err = os.Create(filepath.Join(root, ".gitlab-ci.yml")) 90 if err != nil { 91 return nil, err 92 } 93 case "circleci": 94 err = os.MkdirAll(filepath.Join(root, ".circleci"), 0777) 95 if err != nil { 96 return nil, err 97 } 98 99 f, err = os.Create(filepath.Join(root, ".circleci", "config.yml")) 100 if err != nil { 101 return nil, err 102 } 103 case "", "github": 104 err = os.MkdirAll(filepath.Join(root, ".github", "workflows"), 0777) 105 if err != nil { 106 return nil, err 107 } 108 109 f, err = os.Create(filepath.Join(root, ".github", "workflows", "test.yml")) 110 if err != nil { 111 return nil, err 112 } 113 } 114 115 if f == nil { 116 return f, errors.New("unknown CI provider") 117 } 118 119 return f, nil 120 } 121 122 func (g Generator) templateFile() (pkging.File, error) { 123 td := pkger.Include("github.com/gobuffalo/buffalo-cli/v2:/cli/internal/plugins/ci/templates") 124 125 file := g.provider + ".yml.tmpl" 126 if g.provider == "" { 127 file = "github.yml.tmpl" 128 } 129 130 return pkger.Open(filepath.Join(td, file)) 131 } 132 133 func (g Generator) dbProvider(args []string) string { 134 135 var dbType string 136 137 flg := pflag.NewFlagSet(g.PluginName(), pflag.ContinueOnError) 138 flg.SetOutput(ioutil.Discard) 139 flg.StringVarP(&dbType, "db-type", "", "postgres", "") 140 flg.Parse(args) 141 142 return dbType 143 }