|
| 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