forked from binwiederhier/ntfy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for underscores in server.yml config options
- Loading branch information
Philipp Heckel
committed
May 16, 2022
1 parent
db613f8
commit 91594fa
Showing
9 changed files
with
177 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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 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 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,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/urfave/cli/v2" | ||
"github.com/urfave/cli/v2/altsrc" | ||
"gopkg.in/yaml.v2" | ||
"heckel.io/ntfy/util" | ||
"os" | ||
) | ||
|
||
// initConfigFileInputSourceFunc is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks | ||
// if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails. | ||
func initConfigFileInputSourceFunc(configFlag string, flags []cli.Flag) cli.BeforeFunc { | ||
return func(context *cli.Context) error { | ||
configFile := context.String(configFlag) | ||
if context.IsSet(configFlag) && !util.FileExists(configFile) { | ||
return fmt.Errorf("config file %s does not exist", configFile) | ||
} else if !context.IsSet(configFlag) && !util.FileExists(configFile) { | ||
return nil | ||
} | ||
inputSource, err := newYamlSourceFromFile(configFile, flags) | ||
if err != nil { | ||
return err | ||
} | ||
return altsrc.ApplyInputSourceValues(context, inputSource, flags) | ||
} | ||
} | ||
|
||
// newYamlSourceFromFile creates a new Yaml InputSourceContext from a filepath. | ||
// | ||
// This function also maps aliases, so a .yml file can contain short options, or options with underscores | ||
// instead of dashes. See https://github.com/binwiederhier/ntfy/issues/255. | ||
func newYamlSourceFromFile(file string, flags []cli.Flag) (altsrc.InputSourceContext, error) { | ||
var rawConfig map[interface{}]interface{} | ||
b, err := os.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := yaml.Unmarshal(b, &rawConfig); err != nil { | ||
return nil, err | ||
} | ||
for _, f := range flags { | ||
flagName := f.Names()[0] | ||
for _, flagAlias := range f.Names()[1:] { | ||
if _, ok := rawConfig[flagAlias]; ok { | ||
rawConfig[flagName] = rawConfig[flagAlias] | ||
} | ||
} | ||
} | ||
return altsrc.NewMapInputSource(file, rawConfig), nil | ||
} |
This file contains 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,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestNewYamlSourceFromFile(t *testing.T) { | ||
filename := filepath.Join(t.TempDir(), "server.yml") | ||
contents := ` | ||
# Normal options | ||
listen-https: ":10443" | ||
# Note the underscore! | ||
listen_http: ":1080" | ||
# OMG this is allowed now ... | ||
K: /some/file.pem | ||
` | ||
require.Nil(t, os.WriteFile(filename, []byte(contents), 0600)) | ||
|
||
ctx, err := newYamlSourceFromFile(filename, flagsServe) | ||
require.Nil(t, err) | ||
|
||
listenHTTPS, err := ctx.String("listen-https") | ||
require.Nil(t, err) | ||
require.Equal(t, ":10443", listenHTTPS) | ||
|
||
listenHTTP, err := ctx.String("listen-http") // No underscore! | ||
require.Nil(t, err) | ||
require.Equal(t, ":1080", listenHTTP) | ||
|
||
keyFile, err := ctx.String("key-file") // Long option! | ||
require.Nil(t, err) | ||
require.Equal(t, "/some/file.pem", keyFile) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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