/
create.go
105 lines (91 loc) · 2.36 KB
/
create.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
package cmd
import (
"fmt"
"github.com/chill-cloud/chill-cli/pkg/cache"
"github.com/chill-cloud/chill-cli/pkg/integrations/server"
"github.com/chill-cloud/chill-cli/pkg/service/naming"
"github.com/go-git/go-git/v5"
copy2 "github.com/otiai10/copy"
"github.com/spf13/cobra"
"html/template"
"os"
"path"
"path/filepath"
)
func RunCreate(cmd *cobra.Command, args []string) error {
// No project exists for now, so no cwd lookup will be performed
cwd := Cwd
name := args[0]
base := server.DefaultServerName
if len(args) > 1 {
base = args[1]
}
if !naming.Validate(name) {
return fmt.Errorf("project name does not follow criteria")
}
cacheContext, err := cache.DefaultCacheContext()
if err != nil {
return err
}
integration := server.ForName(base)
if integration == nil {
return fmt.Errorf("no integration found for name %s", base)
}
src := cache.GitSource{Remote: integration.GetBaseProjectRemote()}
err = src.Update(cacheContext)
if err != nil {
return err
}
targetPath := filepath.Join(cwd, name)
err = os.MkdirAll(targetPath, os.ModePerm)
if err != nil {
return err
}
err = copy2.Copy(src.GetPath(cacheContext), targetPath)
if err != nil {
return err
}
err = os.RemoveAll(filepath.Join(targetPath, ".git"))
if err != nil {
return fmt.Errorf("could not remove .git folder: %w\n", err)
}
_, err = git.PlainInit(targetPath, false)
if err != nil {
return fmt.Errorf("could not init a Git repository: %w\n", err)
}
var replacement struct {
ServiceName string
IntegrationName string
}
replacement.ServiceName = name
replacement.IntegrationName = base
configName := path.Join(targetPath, "chill.yaml")
tmpl, err := template.New("chill.yaml").ParseFiles(configName)
if err != nil {
return fmt.Errorf("could not parse template: %w\n", err)
}
out, err := os.OpenFile(configName, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer out.Close()
err = tmpl.Execute(out, replacement)
if err != nil {
return fmt.Errorf("could not process template: %w\n", err)
}
return nil
}
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create <name> [integration]",
Short: "Creates a new Chill service",
Long: `Possible integrations for languages:
go Golang
dart Dart
python Python`,
Args: cobra.MinimumNArgs(1),
RunE: RunCreate,
}
func init() {
rootCmd.AddCommand(createCmd)
}