Skip to content

Commit e61890e

Browse files
committed
Add contrib/ini-to-shell
Signed-off-by: justusbunsi <[email protected]>
1 parent a1f56f8 commit e61890e

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ RUN if [ -n "${GITEA_VERSION}" ]; then git checkout "${GITEA_VERSION}"; fi \
2828
# Begin env-to-ini build
2929
RUN go build contrib/environment-to-ini/environment-to-ini.go
3030

31+
# Begin ini-to-shell build
32+
RUN go build contrib/ini-to-shell/ini-to-shell.go
33+
3134
# Copy local files
3235
COPY docker/root /tmp/local
3336

@@ -38,7 +41,8 @@ RUN chmod 755 /tmp/local/usr/bin/entrypoint \
3841
/tmp/local/etc/s6/openssh/* \
3942
/tmp/local/etc/s6/.s6-svscan/* \
4043
/go/src/code.gitea.io/gitea/gitea \
41-
/go/src/code.gitea.io/gitea/environment-to-ini
44+
/go/src/code.gitea.io/gitea/environment-to-ini \
45+
/go/src/code.gitea.io/gitea/ini-to-shell
4246
RUN chmod 644 /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete
4347

4448
FROM docker.io/library/alpine:3.20
@@ -83,4 +87,5 @@ CMD ["/bin/s6-svscan", "/etc/s6"]
8387
COPY --from=build-env /tmp/local /
8488
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
8589
COPY --from=build-env /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
90+
COPY --from=build-env /go/src/code.gitea.io/gitea/ini-to-shell /usr/local/bin/ini-to-shell
8691
COPY --from=build-env /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete /etc/profile.d/gitea_bash_autocomplete.sh

Dockerfile.rootless

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ RUN if [ -n "${GITEA_VERSION}" ]; then git checkout "${GITEA_VERSION}"; fi \
2828
# Begin env-to-ini build
2929
RUN go build contrib/environment-to-ini/environment-to-ini.go
3030

31+
# Begin ini-to-shell build
32+
RUN go build contrib/ini-to-shell/ini-to-shell.go
33+
3134
# Copy local files
3235
COPY docker/rootless /tmp/local
3336

@@ -36,7 +39,8 @@ RUN chmod 755 /tmp/local/usr/local/bin/docker-entrypoint.sh \
3639
/tmp/local/usr/local/bin/docker-setup.sh \
3740
/tmp/local/usr/local/bin/gitea \
3841
/go/src/code.gitea.io/gitea/gitea \
39-
/go/src/code.gitea.io/gitea/environment-to-ini
42+
/go/src/code.gitea.io/gitea/environment-to-ini \
43+
/go/src/code.gitea.io/gitea/ini-to-shell
4044
RUN chmod 644 /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete
4145

4246
FROM docker.io/library/alpine:3.20
@@ -71,6 +75,7 @@ RUN chown git:git /var/lib/gitea /etc/gitea
7175
COPY --from=build-env /tmp/local /
7276
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
7377
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
78+
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/ini-to-shell /usr/local/bin/ini-to-shell
7479
COPY --from=build-env /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete /etc/profile.d/gitea_bash_autocomplete.sh
7580

7681
# git:git

contrib/ini-to-shell/README

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Ini to Shell
2+
==================
3+
4+
This is the counterpart to environment-to-ini. It allows extracting
5+
settings from an existing ini file for further processing in e.g. a shell.
6+
7+
The original incentive comes from the Helm Chart repository, where
8+
the ini file must be recreated on changes while preserving some of them.
9+
10+
Since it is not possible to define an environment variable for the
11+
parent process, this script simply echoes the value.
12+
13+
To build locally, run:
14+
15+
go build contrib/ini-to-shell/ini-to-shell.go
16+
17+
To extract a value from an ini file, run:
18+
19+
reflogExpire=$(go run contrib/ini-to-shell/ini-to-shell.go -c app.ini -s 'git.config' -k 'gc.reflogExpire')
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
13+
"github.com/urfave/cli/v2"
14+
)
15+
16+
func main() {
17+
app := cli.NewApp()
18+
app.Name = "ini-to-shell"
19+
app.Usage = "Extract settings from an existing configuration ini"
20+
app.Description = `This is the counterpart to environment-to-ini.
21+
It allows extracting settings from an existing ini file for further
22+
processing in e.g. a shell.
23+
24+
Since it is not possible to define an environment variable for the
25+
parent process, this script simply echoes the value.
26+
27+
"""
28+
./ini-to-shell -c /path/to/app.ini -s '<section name>' -k '<key name>'
29+
"""
30+
31+
Section and key name are case sensitive and MUST match with the ini content.`
32+
app.Flags = []cli.Flag{
33+
&cli.StringFlag{
34+
Name: "custom-path",
35+
Aliases: []string{"C"},
36+
Value: setting.CustomPath,
37+
Usage: "Custom path file path",
38+
},
39+
&cli.StringFlag{
40+
Name: "config",
41+
Aliases: []string{"c"},
42+
Value: setting.CustomConf,
43+
Usage: "Custom configuration file path",
44+
},
45+
&cli.StringFlag{
46+
Name: "work-path",
47+
Aliases: []string{"w"},
48+
Value: setting.AppWorkPath,
49+
Usage: "Set the gitea working path",
50+
},
51+
&cli.StringFlag{
52+
Name: "section",
53+
Aliases: []string{"s"},
54+
Value: "",
55+
Usage: "Section name to search the given key (leave empty for default/root section)",
56+
},
57+
&cli.StringFlag{
58+
Name: "key",
59+
Aliases: []string{"k"},
60+
Required: true,
61+
Value: "",
62+
Usage: "Key name to extract the value from",
63+
},
64+
}
65+
app.Action = runIniToShell
66+
err := app.Run(os.Args)
67+
if err != nil {
68+
log.Fatal("Failed to run app with %s: %v", os.Args, err)
69+
}
70+
}
71+
72+
func runIniToShell(c *cli.Context) error {
73+
setting.InitWorkPathAndCfgProvider(os.Getenv, setting.ArgWorkPathAndCustomConf{
74+
WorkPath: c.String("work-path"),
75+
CustomPath: c.String("custom-path"),
76+
CustomConf: c.String("config"),
77+
})
78+
79+
cfg, err := setting.NewConfigProviderFromFile(setting.CustomConf)
80+
if err != nil {
81+
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
82+
}
83+
84+
sName := c.String("section")
85+
kName := c.String("key")
86+
87+
section, err := cfg.GetSection(sName)
88+
if err != nil {
89+
log.Fatal("Failed to load section '%s': %v", sName, err)
90+
}
91+
92+
if !section.HasKey(kName) {
93+
log.Fatal("Section '%s' does not have key '%s'", sName, kName)
94+
}
95+
96+
fmt.Printf("%s", section.Key(kName).Value())
97+
98+
return nil
99+
}

0 commit comments

Comments
 (0)