-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add contrib/ini-to-shell #32669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
justusbunsi
wants to merge
3
commits into
go-gitea:main
Choose a base branch
from
justusbunsi:feat/ini-to-shell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add contrib/ini-to-shell #32669
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Ini to Shell | ||
================== | ||
|
||
This is the counterpart to environment-to-ini. It allows extracting | ||
settings from an existing ini file for further processing in e.g. a shell. | ||
|
||
The original incentive comes from the Helm Chart repository, where | ||
the ini file must be recreated on changes while preserving some of them. | ||
|
||
Since it is not possible to define an environment variable for the | ||
parent process, this script simply echoes the value. | ||
|
||
To build locally, run: | ||
|
||
go build contrib/ini-to-shell/ini-to-shell.go | ||
|
||
To extract a value from an ini file, run: | ||
|
||
reflogExpire=$(go run contrib/ini-to-shell/ini-to-shell.go -c app.ini -s 'git.config' -k 'gc.reflogExpire') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package main | ||
|
||
import ( | ||
golog "log" | ||
"os" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Name = "ini-to-shell" | ||
app.Usage = "Extract settings from an existing configuration ini" | ||
app.Description = `This is the counterpart to environment-to-ini. | ||
It allows extracting settings from an existing ini file for further | ||
processing in e.g. a shell. | ||
|
||
Since it is not possible to define an environment variable for the | ||
parent process, this script simply echoes the value. | ||
|
||
""" | ||
./ini-to-shell -c /path/to/app.ini -s '<section name>' -k '<key name>' | ||
""" | ||
|
||
Section and key name are case sensitive and MUST match with the ini content.` | ||
app.Flags = []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "custom-path", | ||
Aliases: []string{"C"}, | ||
Value: setting.CustomPath, | ||
Usage: "Custom path file path", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "config", | ||
Aliases: []string{"c"}, | ||
Value: setting.CustomConf, | ||
Usage: "Custom configuration file path", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "work-path", | ||
Aliases: []string{"w"}, | ||
Value: setting.AppWorkPath, | ||
Usage: "Set the gitea working path", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "section", | ||
Aliases: []string{"s"}, | ||
Value: "", | ||
Usage: "Section name to search the given key (leave empty for default/root section)", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "key", | ||
Aliases: []string{"k"}, | ||
Required: true, | ||
Value: "", | ||
Usage: "Key name to extract the value from", | ||
}, | ||
} | ||
app.Action = runIniToShell | ||
err := app.Run(os.Args) | ||
if err != nil { | ||
log.Fatal("Failed to run app with %s: %v", os.Args, err) | ||
} | ||
} | ||
|
||
func runIniToShell(c *cli.Context) error { | ||
setting.InitWorkPathAndCfgProvider(os.Getenv, setting.ArgWorkPathAndCustomConf{ | ||
WorkPath: c.String("work-path"), | ||
CustomPath: c.String("custom-path"), | ||
CustomConf: c.String("config"), | ||
}) | ||
|
||
cfg, err := setting.NewConfigProviderFromFile(setting.CustomConf) | ||
if err != nil { | ||
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err) | ||
} | ||
|
||
sName := c.String("section") | ||
kName := c.String("key") | ||
|
||
section, err := cfg.GetSection(sName) | ||
if err != nil { | ||
log.Fatal("Failed to load section '%s': %v", sName, err) | ||
} | ||
|
||
if !section.HasKey(kName) { | ||
log.Fatal("Section '%s' does not have key '%s'", sName, kName) | ||
} | ||
|
||
golog.SetOutput(os.Stdout) | ||
golog.SetFlags(golog.Flags() &^ (golog.Ldate | golog.Ltime)) | ||
golog.Println(section.Key(kName).Value()) | ||
Comment on lines
+96
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think it should use |
||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Necessary?
I think we only need to pass the "config file" to the program, without
InitWorkPathAndCfgProvider
, and justNewConfigProviderFromFile