github.com/adharshmk96/stk@v1.2.3/pkg/project/context.go (about) 1 package project 2 3 import ( 4 "os" 5 "path" 6 7 "github.com/adharshmk96/stk/consts" 8 "github.com/adharshmk96/stk/pkg/commands" 9 "github.com/iancoleman/strcase" 10 "github.com/spf13/viper" 11 ) 12 13 const ( 14 DEFAULT_WORKDIR = "./" 15 CONFIG_FILENAME = ".stk.yaml" 16 ) 17 18 type Context struct { 19 IsGitRepo bool 20 IsGoModule bool 21 22 PackageName string 23 AppName string 24 25 WorkDir string 26 27 GitCmd commands.GitCmd 28 GoCmd commands.GoCmd 29 } 30 31 type TemplateConfig struct { 32 PkgName string 33 AppName string 34 ModName string 35 ExportedName string 36 } 37 38 func NewContext(args []string) *Context { 39 40 goCmd := commands.NewGoCmd() 41 gitCmd := commands.NewGitCmd() 42 43 workDir := viper.GetString("project.workdir") 44 45 os.Chdir(workDir) 46 47 packageName := GetPackageName(args) 48 appName := GetAppNameFromPkgName(packageName) 49 50 isGitRepo := gitCmd.IsRepo() 51 isGoMod := goCmd.IsMod() 52 53 ctx := &Context{ 54 PackageName: packageName, 55 AppName: appName, 56 57 IsGoModule: isGoMod, 58 IsGitRepo: isGitRepo, 59 60 WorkDir: workDir, 61 62 GitCmd: gitCmd, 63 GoCmd: goCmd, 64 } 65 66 return ctx 67 } 68 69 func GetTemplateConfig(ctx *Context, module string) *TemplateConfig { 70 71 moduleName := strcase.ToLowerCamel(module) 72 exportedName := strcase.ToCamel(moduleName) 73 74 return &TemplateConfig{ 75 PkgName: ctx.PackageName, 76 AppName: ctx.AppName, 77 ModName: moduleName, 78 ExportedName: exportedName, 79 } 80 } 81 82 func (ctx *Context) WriteDefaultConfig() error { 83 84 // project configs 85 viper.Set("name", ctx.AppName) 86 viper.Set("description", "This project is generated using stk.") 87 viper.Set("author", "STK") 88 89 // Migrator configs 90 viper.Set(consts.CONFIG_MIGRATOR_WORKDIR, "./stk-migrations") 91 viper.Set(consts.CONFIG_MIGRATOR_DB_TYPE, "sqlite3") 92 viper.Set(consts.CONFIG_MIGRATOR_DB_FILEPATH, "stk.db") 93 94 // Create the config file 95 configPath := path.Join(ctx.WorkDir, CONFIG_FILENAME) 96 err := viper.WriteConfigAs(configPath) 97 if err != nil { 98 return err 99 } 100 101 return nil 102 103 }