Skip to content
Permalink
Browse files

Implemented the `drycop init` command.

  • Loading branch information...
artob committed Jun 21, 2019
1 parent 74b834d commit 682657e17b7ec839471e4d88e87b9c45aa163ef2
Showing with 128 additions and 1 deletion.
  1. +17 −0 config.yml
  2. +87 −1 drycop/cmd/init.go
  3. +11 −0 drycop/cmd/util.go
  4. +13 −0 drycop/util/config.go
@@ -1,4 +1,5 @@
output: text

check:
dirs:
- .git
@@ -30,3 +31,19 @@ check:
markup: md
- file: UNLICENSE
- file: VERSION

templates:
c: https://github.com/dryc/c-template
cpp: https://github.com/drycpp/cpp-template
dart: https://github.com/drydart/dart-template
elixir: https://github.com/dryex/elixir-template
go: https://github.com/drygopher/go-template
lisp: https://github.com/drylisp/lisp-template
lua: https://github.com/drylua/lua-template
ocaml: https://github.com/drycaml/ocaml-template
php: https://github.com/dryphp/php-template
python: https://github.com/drypy/python-template
ruby: https://github.com/dryruby/ruby-template
rust: https://github.com/dryrust/rust-template
swift: https://github.com/dryswift/swift-template
zig: https://github.com/dryzig/zig-template
@@ -3,6 +3,14 @@
package cmd

import (
"fmt"
"os"
"os/exec"

log "github.com/Sirupsen/logrus"
"github.com/dryproject/drycop/drycop/enum"
"github.com/dryproject/drycop/drycop/util"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)

@@ -12,10 +20,88 @@ var InitCmd = &cobra.Command{
Short: "Initializes configuration",
Long: "Initializes configuration",
Run: func(cmd *cobra.Command, args []string) {
// TODO
initTemplates()
},
}

func init() {
RootCmd.AddCommand(InitCmd)
}

func initTemplates() {
config, err := util.LoadTemplatesConfig()
if err != nil {
panic(err)
}

if languageOverride != enum.UnknownLanguage {
initTemplateForLanguage(config, languageOverride)
} else {
initTemplateForLanguage(config, enum.C)
initTemplateForLanguage(config, enum.Csharp)
initTemplateForLanguage(config, enum.Cpp)
initTemplateForLanguage(config, enum.CommonLisp)
initTemplateForLanguage(config, enum.D)
initTemplateForLanguage(config, enum.Dart)
initTemplateForLanguage(config, enum.DRY)
initTemplateForLanguage(config, enum.Elixir)
initTemplateForLanguage(config, enum.Erlang)
initTemplateForLanguage(config, enum.Go)
initTemplateForLanguage(config, enum.Java)
initTemplateForLanguage(config, enum.JavaScript)
initTemplateForLanguage(config, enum.Julia)
initTemplateForLanguage(config, enum.Kotlin)
initTemplateForLanguage(config, enum.Lua)
initTemplateForLanguage(config, enum.Markdown)
initTemplateForLanguage(config, enum.ObjectiveC)
initTemplateForLanguage(config, enum.OCaml)
initTemplateForLanguage(config, enum.PHP)
initTemplateForLanguage(config, enum.Python)
initTemplateForLanguage(config, enum.RestructuredText)
initTemplateForLanguage(config, enum.Ruby)
initTemplateForLanguage(config, enum.Rust)
initTemplateForLanguage(config, enum.Swift)
initTemplateForLanguage(config, enum.TypeScript)
initTemplateForLanguage(config, enum.YAML)
initTemplateForLanguage(config, enum.Zig)
}
}

func initTemplateForLanguage(config *util.TemplatesConfig, language enum.Language) {
logger := log.WithField("language", language.String())

gitURL := config.Templates[language.String()]
if gitURL == "" {
return // skip any language without a configured template
}

gitPath, err := homedir.Expand(fmt.Sprintf("~/.drycop/templates/%s", language.String()))
if err != nil {
panic(err)
}

exists, err := checkPathExists(gitPath)
if err != nil {
panic(err)
}
if exists {
return // skip any already cloned templates
}

command := fmt.Sprintf("git clone %s %s", gitURL, gitPath)

logger = logger.WithField("command", command)
logger.Info("Cloning template")

cmd := exec.Command("sh", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()

if err != nil {
logger.WithError(err).Errorf("Cloning template: failed")
os.Exit(70) // EX_SOFTWARE
} else {
logger.Info("Cloning template: OK")
}
}
@@ -20,3 +20,14 @@ func validateInputDirectory(arg string) (int, error) {
}
return 0, nil
}

func checkPathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
@@ -15,6 +15,10 @@ type CheckConfig struct {
Files []File `mapstructure:"files"`
}

type TemplatesConfig struct {
Templates map[string]string
}

func LoadCheckConfig() (*CheckConfig, error) {
var config CheckConfig
err := viper.UnmarshalKey("check", &config)
@@ -23,3 +27,12 @@ func LoadCheckConfig() (*CheckConfig, error) {
}
return &config, nil
}

func LoadTemplatesConfig() (*TemplatesConfig, error) {
var config TemplatesConfig
err := viper.UnmarshalKey("templates", &config.Templates)
if err != nil {
return nil, err
}
return &config, nil
}

0 comments on commit 682657e

Please sign in to comment.
You can’t perform that action at this time.