Skip to content

Commit 2d3788a

Browse files
committed
add interactive and non-interactive modes
1 parent 7bdfdff commit 2d3788a

File tree

4 files changed

+142
-50
lines changed

4 files changed

+142
-50
lines changed

ask/ask.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ func StorageBackend() (*S3StorageBackend, error) {
164164

165165
}
166166

167+
func WithLogging() bool {
168+
reader := bufio.NewReader(os.Stdin)
169+
170+
fmt.Print("Generate logging config files as well? (yes no) [no]:")
171+
yesno := ReadInput(reader)
172+
173+
if yesno == "yes" {
174+
return true
175+
}
176+
177+
return false
178+
}
179+
167180
func comparePasswords(password1, password2 []byte) bool {
168181
// Use subtle.ConstantTimeCompare for secure comparison
169182
return subtle.ConstantTimeCompare(password1, password2) == 1

ask/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ask
33
type Credentials struct {
44
Username string
55
Password string
6-
Email string
76
}
87

98
type S3StorageBackend struct {

pmcompose.go

Lines changed: 120 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67
"text/template"
@@ -16,102 +17,173 @@ type ComposeConfig struct {
1617
UserLoginCredentials ask.Credentials
1718
WebAppPort int
1819
S3StorageBackend ask.S3StorageBackend
20+
LoggingConfigs bool // generate and include logging configs?
1921
}
2022

23+
var interactive = flag.Bool("i", false, "start interactive session")
24+
var appVersion = flag.String("av", "3.5", "app version e.g. 3.5")
25+
var appPort = flag.Int("ap", 12000, "web app port")
26+
var username = flag.String("u", "admin", "username")
27+
var password = flag.String("p", "", "password")
28+
var withS3Backend = flag.Bool("s3", false, "with S3 backend")
29+
var withLoggingConfigs = flag.Bool("lc", false, "with logging configs")
30+
2131
func main() {
2232

23-
fmt.Println(`
24-
###################################################
25-
### Papermerge DMS docker compose generator ###
26-
###################################################
27-
`)
33+
flag.Parse()
2834

29-
cfg := ComposeConfig{}
35+
var cfg *ComposeConfig
36+
var err error
3037

31-
version, err := ask.AppVersion("3.5")
38+
if *interactive {
39+
cfg, err = InteractiveSession()
40+
if err != nil {
41+
fmt.Printf("Error: %v\n", err)
42+
return
43+
}
44+
} else {
45+
cfg, err = GetComposeConfig()
46+
}
3247

48+
dir, err := utils.GetExecutableDir()
3349
if err != nil {
3450
fmt.Printf("Error: %v\n", err)
3551
return
3652
}
3753

38-
cfg.AppVersion = version
39-
40-
appPort, err := ask.WebAppPort(12000)
54+
templatePath := fmt.Sprintf("%s/pmcompose_templates/docker-compose.yaml.tmpl", dir)
4155

56+
tmpl, err := template.ParseFiles(templatePath)
4257
if err != nil {
43-
fmt.Printf("Error: %v\n", err)
58+
fmt.Println("Error loading template:", err)
4459
return
4560
}
4661

47-
cfg.WebAppPort = appPort
48-
49-
creds, err := ask.LoginCredentials()
50-
62+
outputFile, err := os.Create("docker-compose.yml")
5163
if err != nil {
52-
fmt.Printf("Error: %v\n", err)
64+
fmt.Println("Error creating docker-compose.yml:", err)
5365
return
5466
}
67+
defer outputFile.Close()
5568

56-
cfg.UserLoginCredentials = *creds
57-
58-
s3StorageBackend, err := ask.StorageBackend()
59-
69+
err = tmpl.Execute(outputFile, cfg)
6070
if err != nil {
61-
fmt.Printf("Error: %v\n", err)
71+
fmt.Println("Error executing template:", err)
6272
return
6373
}
6474

65-
if s3StorageBackend != nil {
66-
cfg.S3StorageBackend = *s3StorageBackend
75+
fmt.Println("✅ docker-compose.yml generated successfully.")
76+
77+
if cfg.LoggingConfigs {
78+
err = os.WriteFile("webapp_logging.yaml", []byte(logs.WEBAPP_LOGGING), 0644)
79+
if err != nil {
80+
fmt.Println("Error writing webapp_logging.yaml file")
81+
}
82+
fmt.Println("✅ webapp_logging.yaml generated successfully.")
6783
}
6884

85+
if cfg.S3StorageBackend.S3BucketName != "" {
86+
if cfg.LoggingConfigs {
87+
err = os.WriteFile("s3worker_logging.yaml", []byte(logs.S3WORKER_LOGGING), 0644)
88+
if err != nil {
89+
fmt.Println("Error writing s3worker_logging.yaml file")
90+
}
91+
fmt.Println("✅ s3worker_logging.yaml generated successfully.")
92+
}
93+
}
94+
95+
}
96+
97+
func GetComposeConfig() (*ComposeConfig, error) {
98+
99+
var cfg ComposeConfig
100+
69101
secretKey, err := utils.GenerateSecretString(32)
70102
if err != nil {
71-
fmt.Printf("Error: %v\n", err)
72-
return
103+
return nil, err
73104
}
74-
cfg.SecretKey = secretKey
75105

76-
dir, err := utils.GetExecutableDir()
106+
pass, err := utils.GenerateSecretString(5)
77107
if err != nil {
78-
fmt.Printf("Error: %v\n", err)
79-
return
108+
return nil, err
80109
}
81110

82-
templatePath := fmt.Sprintf("%s/pmcompose_templates/docker-compose.yaml.tmpl", dir)
111+
if *password == "" {
112+
*password = pass
113+
}
114+
115+
cfg = ComposeConfig{
116+
AppVersion: *appVersion,
117+
SecretKey: secretKey,
118+
WebAppPort: *appPort,
119+
UserLoginCredentials: ask.Credentials{
120+
Username: *username,
121+
Password: *password,
122+
},
123+
LoggingConfigs: *withLoggingConfigs,
124+
}
125+
126+
if *withS3Backend == true {
127+
cfg.S3StorageBackend.S3BucketName = "<your bucket name>"
128+
cfg.S3StorageBackend.AWSRegionName = "eu-central-1"
129+
cfg.S3StorageBackend.AWSAccessKeyID = "<your access key ID>"
130+
cfg.S3StorageBackend.AWSSecretAccessKey = "<your secret access key>"
131+
}
132+
133+
return &cfg, nil
134+
}
135+
136+
func InteractiveSession() (*ComposeConfig, error) {
137+
138+
cfg := ComposeConfig{}
139+
140+
fmt.Println(`
141+
###################################################
142+
### Papermerge DMS docker compose generator ###
143+
###################################################
144+
`)
145+
146+
version, err := ask.AppVersion("3.5")
83147

84-
tmpl, err := template.ParseFiles(templatePath)
85148
if err != nil {
86-
fmt.Println("Error loading template:", err)
87-
return
149+
return nil, err
88150
}
89151

90-
outputFile, err := os.Create("docker-compose.yml")
152+
cfg.AppVersion = version
153+
154+
appPort, err := ask.WebAppPort(12000)
155+
91156
if err != nil {
92-
fmt.Println("Error creating docker-compose.yml:", err)
93-
return
157+
return nil, err
94158
}
95-
defer outputFile.Close()
96159

97-
err = tmpl.Execute(outputFile, cfg)
160+
cfg.WebAppPort = appPort
161+
162+
creds, err := ask.LoginCredentials()
163+
98164
if err != nil {
99-
fmt.Println("Error executing template:", err)
100-
return
165+
return nil, err
101166
}
102167

103-
fmt.Println("✅ docker-compose.yml generated successfully.")
168+
cfg.UserLoginCredentials = *creds
169+
170+
s3StorageBackend, err := ask.StorageBackend()
104171

105-
err = os.WriteFile("webapp_logging.yaml", []byte(logs.WEBAPP_LOGGING), 0644)
106172
if err != nil {
107-
fmt.Println("Error writing webapp_logging.yaml file")
173+
return nil, err
108174
}
109175

110-
if cfg.S3StorageBackend.S3BucketName != "" {
111-
err = os.WriteFile("s3worker_logging.yaml", []byte(logs.S3WORKER_LOGGING), 0644)
112-
if err != nil {
113-
fmt.Println("Error writing s3worker_logging.yaml file")
114-
}
176+
if s3StorageBackend != nil {
177+
cfg.S3StorageBackend = *s3StorageBackend
115178
}
116179

180+
cfg.LoggingConfigs = ask.WithLogging()
181+
182+
secretKey, err := utils.GenerateSecretString(32)
183+
if err != nil {
184+
return nil, err
185+
}
186+
cfg.SecretKey = secretKey
187+
188+
return &cfg, nil
117189
}

pmcompose_templates/docker-compose.yaml.tmpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ services:
44
environment:
55
PAPERMERGE__SECURITY__SECRET_KEY: "{{ .SecretKey }}"
66
PAPERMERGE__AUTH__USERNAME: {{.UserLoginCredentials.Username }}
7-
PAPERMERGE__AUTH__PASSWORD: {{.UserLoginCredentials.Password }}
7+
PAPERMERGE__AUTH__PASSWORD: "{{.UserLoginCredentials.Password }}"
88
PAPERMERGE__DATABASE__URL: postgresql://coco:jumbo@db:5432/pmgdb
99
PAPERMERGE__REDIS__URL: redis://redis:6379/0
1010
PAPERMERGE__MAIN__MEDIA_ROOT: /var/media/pmg
11+
{{- if eq .LoggingConfigs true }}
1112
PAPERMERGE__MAIN__LOGGING_CFG: /etc/papermerge/logging.yaml
13+
{{- end }}
1214
ports:
1315
- "{{ .WebAppPort }}:80"
1416
depends_on:
1517
- db
1618
- redis
1719
volumes:
1820
- media_root:/var/media/pmg
21+
{{- if eq .LoggingConfigs true }}
1922
- ${PWD}/webapp_logging.yaml:/etc/papermerge/logging.yaml
23+
{{- end }}
2024
path_template_worker:
2125
image: papermerge/path-tmpl-worker:0.4
2226
command: worker
@@ -36,7 +40,9 @@ services:
3640
PAPERMERGE__REDIS__URL: redis://redis:6379/0
3741
PAPERMERGE__MAIN__MEDIA_ROOT: /var/media/pmg
3842
PAPERMERGE__S3__BUCKET_NAME: {{ .S3StorageBackend.S3BucketName }}
43+
{{- if eq .LoggingConfigs true }}
3944
PAPERMERGE__MAIN__LOGGING_CFG: /etc/papermerge/logging.yaml
45+
{{- end }}
4046
S3_WORKER_ARGS: "-Q s3 -c 2"
4147
AWS_REGION_NAME: {{ .S3StorageBackend.AWSRegionName }}
4248
AWS_ACCESS_KEY_ID: {{ .S3StorageBackend.AWSAccessKeyID }}
@@ -46,7 +52,9 @@ services:
4652
- redis
4753
volumes:
4854
- media_root:/var/media/pmg
55+
{{- if eq .LoggingConfigs true }}
4956
- ${PWD}/s3worker_logging.yaml:/etc/papermerge/logging.yaml
57+
{{- end }}
5058
{{- end }}
5159
db:
5260
image: postgres:16.1

0 commit comments

Comments
 (0)