Skip to content

Commit feed43e

Browse files
authored
Merge pull request #53 from SLH335/config-create
feat: form for interactive config file creation
2 parents ddecfba + 1a3629b commit feed43e

File tree

4 files changed

+212
-33
lines changed

4 files changed

+212
-33
lines changed

createConfig.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"path"
9+
10+
"github.com/charmbracelet/huh"
11+
)
12+
13+
type setupConfig struct {
14+
IdentityEndpoint string `json:"endpoint,omitempty"`
15+
ApplicationCredentialID string `json:"applicationCredentialId,omitempty"`
16+
ApplicationCredentialSecret string `json:"applicationCredentialSecret,omitempty"`
17+
Region string `json:"region,omitempty"`
18+
MountDir string `json:"mountDir,omitempty"`
19+
}
20+
21+
func createConfiguration(configPath string) error {
22+
var config setupConfig
23+
24+
credentialInstructions :=
25+
`To get an Application Credential ID you must create new Application Credentials
26+
via the web console (Horizon).
27+
Navigate to "Identity > Application Credentials > Create Application Credential"
28+
Enter a descriptive name and optionally a description and expiration date.
29+
Press 'Create Application Credential', copy the displayed ID and paste it here.
30+
31+
Do not close the window yet.`
32+
33+
mountDirInstructions := `Directory used for mounting cinder volumes
34+
Leave blank for default`
35+
36+
stat, err := os.Stat(configPath)
37+
if err == nil {
38+
if stat.IsDir() {
39+
return fmt.Errorf("The configuration file path already is a directory. Delete it or choose a different path to continue.")
40+
} else {
41+
overwriteFile := false
42+
43+
err = huh.NewForm(
44+
huh.NewGroup(
45+
huh.NewConfirm().Title("The config file already exists. Overwrite it?").Description(fmt.Sprintf("Path: '%s'", configPath)).Value(&overwriteFile),
46+
),
47+
).Run()
48+
if err != nil {
49+
return err
50+
}
51+
if !overwriteFile {
52+
return fmt.Errorf("user aborted")
53+
}
54+
}
55+
} else if errors.Is(err, os.ErrNotExist) {
56+
err = os.MkdirAll(path.Dir(configPath), 0775)
57+
}
58+
59+
err = huh.NewForm(
60+
huh.NewGroup(
61+
huh.NewInput().Title("Open Stack endpoint URL").Value(&config.IdentityEndpoint),
62+
huh.NewInput().Title("Open Stack Region Name").Value(&config.Region),
63+
),
64+
huh.NewGroup(
65+
huh.NewInput().Title("Application Credential ID").Value(&config.ApplicationCredentialID).Description(credentialInstructions),
66+
huh.NewInput().Title("Application Credential Secret").Value(&config.ApplicationCredentialSecret).Description("Copy the displayed Secret and paste it here").EchoMode(huh.EchoModePassword),
67+
),
68+
huh.NewGroup(
69+
huh.NewInput().Title("Mount Dir").Value(&config.MountDir).Description(mountDirInstructions),
70+
),
71+
).Run()
72+
if err != nil {
73+
return err
74+
}
75+
76+
return writeConfigurationFile(config, configPath)
77+
}
78+
79+
func writeConfigurationFile(config setupConfig, path string) error {
80+
configText, err := json.MarshalIndent(config, "", " ")
81+
if err != nil {
82+
return err
83+
}
84+
85+
err = os.WriteFile(path, configText, 0664)
86+
if err != nil {
87+
return err
88+
}
89+
return nil
90+
}

go.mod

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/jgraichen/docker-plugin-cinder
33
go 1.23
44

55
require (
6+
github.com/charmbracelet/huh v0.6.0
67
github.com/coreos/go-systemd/v22 v22.5.0
78
github.com/docker/go-plugins-helpers v0.0.0-20240701071450-45e2431495c8
89
github.com/gophercloud/gophercloud/v2 v2.2.0
@@ -12,10 +13,32 @@ require (
1213

1314
require (
1415
github.com/Microsoft/go-winio v0.6.0 // indirect
16+
github.com/atotto/clipboard v0.1.4 // indirect
17+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
18+
github.com/catppuccin/go v0.2.0 // indirect
19+
github.com/charmbracelet/bubbles v0.20.0 // indirect
20+
github.com/charmbracelet/bubbletea v1.1.0 // indirect
21+
github.com/charmbracelet/lipgloss v0.13.0 // indirect
22+
github.com/charmbracelet/x/ansi v0.2.3 // indirect
23+
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
24+
github.com/charmbracelet/x/term v0.2.0 // indirect
1525
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
1626
github.com/docker/go-connections v0.4.0 // indirect
17-
golang.org/x/mod v0.8.0 // indirect
18-
golang.org/x/net v0.23.0 // indirect
19-
golang.org/x/sys v0.22.0 // indirect
20-
golang.org/x/tools v0.6.0 // indirect
27+
github.com/dustin/go-humanize v1.0.1 // indirect
28+
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
29+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
30+
github.com/mattn/go-isatty v0.0.20 // indirect
31+
github.com/mattn/go-localereader v0.0.1 // indirect
32+
github.com/mattn/go-runewidth v0.0.16 // indirect
33+
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
34+
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
35+
github.com/muesli/cancelreader v0.2.2 // indirect
36+
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect
37+
github.com/rivo/uniseg v0.4.7 // indirect
38+
golang.org/x/mod v0.17.0 // indirect
39+
golang.org/x/net v0.25.0 // indirect
40+
golang.org/x/sync v0.8.0 // indirect
41+
golang.org/x/sys v0.25.0 // indirect
42+
golang.org/x/text v0.18.0 // indirect
43+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
2144
)

go.sum

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
2+
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
13
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
24
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
5+
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
6+
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
7+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
8+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
9+
github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA=
10+
github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc=
11+
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
12+
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
13+
github.com/charmbracelet/bubbletea v1.1.0 h1:FjAl9eAL3HBCHenhz/ZPjkKdScmaS5SK69JAK2YJK9c=
14+
github.com/charmbracelet/bubbletea v1.1.0/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
15+
github.com/charmbracelet/huh v0.6.0 h1:mZM8VvZGuE0hoDXq6XLxRtgfWyTI3b2jZNKh0xWmax8=
16+
github.com/charmbracelet/huh v0.6.0/go.mod h1:GGNKeWCeNzKpEOh/OJD8WBwTQjV3prFAtQPpLv+AVwU=
17+
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
18+
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
19+
github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=
20+
github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
21+
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 h1:qko3AQ4gK1MTS/de7F5hPGx6/k1u0w4TeYmBFwzYVP4=
22+
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ=
23+
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
24+
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
325
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
426
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
527
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
@@ -11,29 +33,56 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
1133
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
1234
github.com/docker/go-plugins-helpers v0.0.0-20240701071450-45e2431495c8 h1:IMfrF5LCzP2Vhw7j4IIH3HxPsCLuZYjDqFAM/C88ulg=
1335
github.com/docker/go-plugins-helpers v0.0.0-20240701071450-45e2431495c8/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA=
36+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
37+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
38+
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
39+
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
1440
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
1541
github.com/gophercloud/gophercloud/v2 v2.2.0 h1:STqqnSXuhcg1OPBOZ14z6JDm8fKIN13H2bJg6bBuHp8=
1642
github.com/gophercloud/gophercloud/v2 v2.2.0/go.mod h1:f2hMRC7Kakbv5vM7wSGHrIPZh6JZR60GVHryJlF/K44=
43+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
44+
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
45+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
46+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
47+
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
48+
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
49+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
50+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
51+
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
52+
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
53+
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
54+
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
55+
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
56+
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
57+
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a h1:2MaM6YC3mGu54x+RKAA6JiFFHlHDY1UbkxqppT7wYOg=
58+
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a/go.mod h1:hxSnBBYLK21Vtq/PHd0S2FYCxBXzBua8ov5s1RobyRQ=
1759
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1860
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
61+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
62+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
63+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
1964
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
2065
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
2166
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
2267
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
2368
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2469
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2570
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
26-
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
27-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
28-
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
29-
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
30-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
31-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
71+
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
72+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
73+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
74+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
75+
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
76+
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
77+
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3278
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33-
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
34-
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
35-
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
36-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
79+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
80+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
81+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
82+
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
83+
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
84+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
85+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
3786
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3887
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3988
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

main.go

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
)
1818

1919
type tConfig struct {
20-
Debug bool
21-
Quiet bool
20+
Debug bool `json:"debug,omitempty"`
21+
Quiet bool `json:"quiet,omitempty"`
2222
Prefix string `json:"prefix,omitempty"`
2323
IdentityEndpoint string `json:"endpoint,omitempty"`
2424
Username string `json:"username,omitempty"`
@@ -46,20 +46,47 @@ func init() {
4646
func main() {
4747
var config tConfig
4848
var configFile string
49+
var createConfig bool
4950
flag.BoolVar(&config.Debug, "debug", false, "Enable debug logging")
5051
flag.BoolVar(&config.Quiet, "quiet", false, "Only report errors")
51-
flag.StringVar(&configFile, "config", "", "")
52+
flag.StringVar(&configFile, "config", "", "Path to config file")
5253
flag.StringVar(&config.Prefix, "prefix", "docker-volume", "")
5354
flag.StringVar(&config.MountDir, "mountDir", "", "")
55+
flag.BoolVar(&createConfig, "createConfig", false, "Create config file interactively")
5456
flag.Parse()
5557

56-
if len(configFile) == 0 {
57-
configFile = "cinder.json"
58-
}
59-
6058
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
6159
log.SetOutput(os.Stdout)
6260

61+
if config.Quiet {
62+
log.SetLevel(log.ErrorLevel)
63+
}
64+
65+
if config.Debug {
66+
log.SetLevel(log.DebugLevel)
67+
log.Debug("Debug logging enabled")
68+
}
69+
70+
if createConfig {
71+
if configFile == "" {
72+
configFile = "/etc/docker/cinder.json"
73+
}
74+
75+
err := createConfiguration(configFile)
76+
if err == nil {
77+
log.Info("Configuration file written successfully")
78+
} else if err.Error() == "user aborted" {
79+
log.Info("Configuration setup aborted")
80+
} else {
81+
log.Fatalf("Failed to write configuration file: %s", err)
82+
}
83+
os.Exit(0)
84+
}
85+
86+
if configFile == "" {
87+
configFile = "./cinder.json"
88+
}
89+
6390
content, err := os.ReadFile(configFile)
6491
if err != nil {
6592
log.Fatal(err.Error())
@@ -70,21 +97,11 @@ func main() {
7097
log.Fatal(err.Error())
7198
}
7299

73-
if len(config.MountDir) == 0 {
100+
if config.MountDir == "" {
74101
log.Fatal("No mountDir configured. Abort.")
75102
}
76103

77-
if config.Quiet {
78-
log.SetLevel(log.ErrorLevel)
79-
}
80-
81-
if config.Debug {
82-
log.SetLevel(log.DebugLevel)
83-
}
84-
85-
log.Debug("Debug logging enabled")
86-
87-
if len(config.IdentityEndpoint) == 0 {
104+
if config.IdentityEndpoint == "" {
88105
log.Fatal("Identity endpoint missing")
89106
}
90107

0 commit comments

Comments
 (0)