Skip to content

Commit 9b8f4c9

Browse files
committed
Update command
1 parent 6d59e7a commit 9b8f4c9

File tree

4 files changed

+364
-110
lines changed

4 files changed

+364
-110
lines changed

cli.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
)
12+
13+
// ScanStr scans the given input
14+
func ScanStr(input *string, label string) {
15+
fmt.Print(fmt.Sprintf("Enter the %s name: ", label))
16+
_, err := fmt.Scanln(input)
17+
if err != nil {
18+
log.Fatal(fmt.Sprintf("Error reading %s name:", label), err)
19+
}
20+
}
21+
22+
// CopyFile is a helper function to copy a file
23+
func CopyFile(src, dst string) error {
24+
sourceFile, err := os.Open(src)
25+
if err != nil {
26+
return err
27+
}
28+
defer sourceFile.Close()
29+
30+
destinationFile, err := os.Create(dst)
31+
if err != nil {
32+
return err
33+
}
34+
defer destinationFile.Close()
35+
36+
_, err = io.Copy(destinationFile, sourceFile)
37+
if err != nil {
38+
return err
39+
}
40+
//err = os.Chmod(dst, 0644) // Set the file permissions
41+
//if err != nil {
42+
// return err
43+
//}
44+
//
45+
// Copy file info (like modification times)
46+
info, err := os.Stat(src)
47+
if err != nil {
48+
return err
49+
}
50+
return os.Chtimes(dst, info.ModTime(), info.ModTime())
51+
}
52+
53+
// CopyDir copies the src dir to dst
54+
func CopyDir(src string, dst string) error {
55+
entries, err := ioutil.ReadDir(src)
56+
if err != nil {
57+
return err
58+
}
59+
60+
if err := os.MkdirAll(dst, 0755); err != nil {
61+
return err
62+
}
63+
64+
for _, entry := range entries {
65+
sourcePath := filepath.Join(src, entry.Name())
66+
destPath := filepath.Join(dst, entry.Name())
67+
68+
if entry.IsDir() {
69+
err = CopyDir(sourcePath, destPath)
70+
if err != nil {
71+
return err
72+
}
73+
} else {
74+
// Check if the destination file exists
75+
if _, err := os.Stat(destPath); err == nil {
76+
// If the file exists, remove it before copying
77+
if err := os.Remove(destPath); err != nil {
78+
return fmt.Errorf("failed to remove existing destination file: %v", err)
79+
}
80+
}
81+
err = CopyFile(sourcePath, destPath)
82+
if err != nil {
83+
return err
84+
}
85+
}
86+
}
87+
return nil
88+
}
89+
90+
// RemoveDirs removes the list of given directories
91+
func RemoveDirs(dirPath string, dirNames []string) {
92+
fmt.Println("> Cleaning up...")
93+
for _, dirName := range dirNames {
94+
if err := os.RemoveAll(filepath.Join(dirPath, dirName)); err != nil {
95+
fmt.Println("Warning: Unable to remove .git directory:", err)
96+
}
97+
}
98+
}
99+
100+
// EnsureBinary ensures the git binary is present in the system
101+
func EnsureBinary(binary string) {
102+
// Check if the given binary is installed
103+
if _, err := exec.LookPath(binary); err != nil {
104+
log.Fatal(fmt.Sprintf("Error: %s must be installed to use this command.", binary))
105+
}
106+
}
107+
108+
// HasBinary returns if the given binary is installed in the system
109+
func HasBinary(binary string) bool {
110+
if _, err := exec.LookPath(binary); err != nil {
111+
return false
112+
}
113+
return true
114+
}
115+
116+
// DirPath returns the full directory path for a directory name
117+
func DirPath(dirname string) string {
118+
currentDir, _ := os.Getwd()
119+
return filepath.Join(currentDir, dirname)
120+
}
121+
122+
// EnsureEmptyDir ensures the directory in which the project should be created is empty
123+
func EnsureEmptyDir(dirname string) {
124+
dirPath := DirPath(dirname)
125+
// Check if directory exists
126+
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
127+
// If directory doesn't exist, create it
128+
fmt.Printf("> Creating new directory: %s\n", dirname)
129+
if err := os.MkdirAll(dirPath, 0755); err != nil {
130+
log.Fatal("Error creating directory:", err)
131+
}
132+
} else {
133+
// Directory exists, check if it's empty
134+
files, _ := filepath.Glob(filepath.Join(dirPath, "*"))
135+
if len(files) > 0 {
136+
log.Fatal("Error: The directory must be empty to create a new project.")
137+
}
138+
}
139+
}
140+
141+
// HasDirectory returns true if a directory exists and false otherwise
142+
func HasDirectory(dirPath string) bool {
143+
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
144+
return false
145+
}
146+
return true
147+
}
148+
149+
// CreateDirIfNotExists creates a directory if it does not exist
150+
func CreateDirIfNotExists(dirPath string) {
151+
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
152+
err := os.MkdirAll(dirPath, 0755)
153+
if err != nil {
154+
log.Fatal("Error creating directory:", err)
155+
}
156+
}
157+
}
158+
159+
// RunCommand runs a command in a specific directory
160+
func RunCommand(dirPath string, command string, args ...string) {
161+
cmd := exec.Command(command, args...)
162+
cmd.Dir = dirPath
163+
stdoutStderr, err := cmd.CombinedOutput()
164+
if err != nil {
165+
log.Fatal(err)
166+
}
167+
fmt.Printf("%s\n", stdoutStderr)
168+
}

input_gen.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ var inputFieldTypes = []string{
2020
}
2121

2222
type InputField struct {
23-
Name string
24-
Type string
25-
Required bool
26-
Unique bool
27-
Table string
23+
Name string
24+
Type string
25+
Required bool
26+
Unique bool
27+
Table string
28+
WhereClauses []map[string]interface{}
2829
}
2930

3031
type InputConfig struct {

migration.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ func mig_{{.Version}}_{{.Name}}_up(tx *sql.Tx) error {
5555
{{- end}})
5656
{{- end}}
5757

58+
{{- range $i, $ucs := .UniqueColumns}}
59+
{{- if gt (len $ucs) 0}}
60+
t.UniqueKey(
61+
{{- range $j, $uc := $ucs}}
62+
{{- if $j -}}, {{- end -}}
63+
{{printf "\"%s\"" $uc}}
64+
{{- end}})
65+
{{- end}}
66+
{{- end}}
67+
5868
{{- range $i, $fcs := .ForeignColumns}}
5969
{{- if gt (len $fcs) 0}}
6070
t.Foreign(

0 commit comments

Comments
 (0)