From 83f628052df6d5af0b80e05bd987ebe84c2d3d40 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 8 Jun 2021 13:08:32 +0200 Subject: [PATCH] add new os pkg on ocis-pkg --- glauth/pkg/flagset/flagset.go | 15 +++------------ idp/pkg/flagset/flagset.go | 15 +++------------ ocis-pkg/os/os.go | 17 +++++++++++++++++ ocis-pkg/os/os_test.go | 36 +++++++++++++++++++++++++++++++++++ proxy/pkg/flagset/flagset.go | 15 +++------------ 5 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 ocis-pkg/os/os.go create mode 100644 ocis-pkg/os/os_test.go diff --git a/glauth/pkg/flagset/flagset.go b/glauth/pkg/flagset/flagset.go index 7bf555799..d3bc91a00 100644 --- a/glauth/pkg/flagset/flagset.go +++ b/glauth/pkg/flagset/flagset.go @@ -1,13 +1,12 @@ package flagset import ( - "os" "path" - "path/filepath" "github.com/micro/cli/v2" "github.com/owncloud/ocis/glauth/pkg/config" "github.com/owncloud/ocis/ocis-pkg/flags" + pkgos "github.com/owncloud/ocis/ocis-pkg/os" ) // RootWithConfig applies cfg to the root flagset @@ -47,14 +46,6 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag { } } -func mustUserConfigDir() string { - dir, err := os.UserConfigDir() - if err != nil { - panic(err) - } - return filepath.Join(dir, "ocis", "ldap") -} - // ServerWithConfig applies cfg to the root flagset func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ @@ -170,14 +161,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "ldaps-cert", - Value: flags.OverrideDefaultString(cfg.Ldaps.Cert, path.Join(mustUserConfigDir(), "ldap.crt")), + Value: flags.OverrideDefaultString(cfg.Ldaps.Cert, path.Join(pkgos.MustUserConfigDir("ocis", "ldap"), "ldap.crt")), Usage: "path to ldaps certificate in PEM format", EnvVars: []string{"GLAUTH_LDAPS_CERT"}, Destination: &cfg.Ldaps.Cert, }, &cli.StringFlag{ Name: "ldaps-key", - Value: flags.OverrideDefaultString(cfg.Ldaps.Key, path.Join(mustUserConfigDir(), "ldap.key")), + Value: flags.OverrideDefaultString(cfg.Ldaps.Key, path.Join(pkgos.MustUserConfigDir("ocis", "ldap"), "ldap.key")), Usage: "path to ldaps key in PEM format", EnvVars: []string{"GLAUTH_LDAPS_KEY"}, Destination: &cfg.Ldaps.Key, diff --git a/idp/pkg/flagset/flagset.go b/idp/pkg/flagset/flagset.go index 9fb57a492..9b4c15437 100644 --- a/idp/pkg/flagset/flagset.go +++ b/idp/pkg/flagset/flagset.go @@ -1,13 +1,12 @@ package flagset import ( - "os" "path" - "path/filepath" "github.com/micro/cli/v2" "github.com/owncloud/ocis/idp/pkg/config" "github.com/owncloud/ocis/ocis-pkg/flags" + pkgos "github.com/owncloud/ocis/ocis-pkg/os" ) // RootWithConfig applies cfg to the root flagset @@ -47,14 +46,6 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag { } } -func mustUserConfigDir() string { - dir, err := os.UserConfigDir() - if err != nil { - panic(err) - } - return filepath.Join(dir, "ocis", "idp") -} - // ServerWithConfig applies cfg to the root flagset func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ @@ -245,14 +236,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "transport-tls-cert", - Value: flags.OverrideDefaultString(cfg.HTTP.TLSCert, path.Join(mustUserConfigDir(), "server.crt")), + Value: flags.OverrideDefaultString(cfg.HTTP.TLSCert, path.Join(pkgos.MustUserConfigDir("ocis", "idp"), "server.crt")), Usage: "Certificate file for transport encryption", EnvVars: []string{"IDP_TRANSPORT_TLS_CERT"}, Destination: &cfg.HTTP.TLSCert, }, &cli.StringFlag{ Name: "transport-tls-key", - Value: flags.OverrideDefaultString(cfg.HTTP.TLSKey, path.Join(mustUserConfigDir(), "server.key")), + Value: flags.OverrideDefaultString(cfg.HTTP.TLSKey, path.Join(pkgos.MustUserConfigDir("ocis", "idp"), "server.key")), Usage: "Secret file for transport encryption", EnvVars: []string{"IDP_TRANSPORT_TLS_KEY"}, Destination: &cfg.HTTP.TLSKey, diff --git a/ocis-pkg/os/os.go b/ocis-pkg/os/os.go new file mode 100644 index 000000000..3c3942aba --- /dev/null +++ b/ocis-pkg/os/os.go @@ -0,0 +1,17 @@ +package os + +import ( + "os" + "path/filepath" +) + +// MustUserConfigDir generates a default config location for a user based on their OS. This location can be used to store +// any artefacts the app needs for its functioning. It is a pure function. Its only side effect is that results vary +// depending on which operative system we're in. +func MustUserConfigDir(prefix, extension string) string { + dir, err := os.UserConfigDir() + if err != nil { + panic(err) + } + return filepath.Join(dir, prefix, extension) +} diff --git a/ocis-pkg/os/os_test.go b/ocis-pkg/os/os_test.go new file mode 100644 index 000000000..29f627b66 --- /dev/null +++ b/ocis-pkg/os/os_test.go @@ -0,0 +1,36 @@ +package os + +import ( + "os" + "path/filepath" + "testing" +) + +func Test_mustUserConfigDir(t *testing.T) { + configDir, _ := os.UserConfigDir() + type args struct { + prefix string + extension string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "fetch the default config location for the current user", + args: args{ + prefix: "ocis", + extension: "testing", + }, + want: filepath.Join(configDir, "ocis", "testing"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MustUserConfigDir(tt.args.prefix, tt.args.extension); got != tt.want { + t.Errorf("MustUserConfigDir() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/proxy/pkg/flagset/flagset.go b/proxy/pkg/flagset/flagset.go index 996eb996e..87ad472d4 100644 --- a/proxy/pkg/flagset/flagset.go +++ b/proxy/pkg/flagset/flagset.go @@ -1,12 +1,11 @@ package flagset import ( - "os" "path" - "path/filepath" "github.com/micro/cli/v2" "github.com/owncloud/ocis/ocis-pkg/flags" + pkgos "github.com/owncloud/ocis/ocis-pkg/os" "github.com/owncloud/ocis/proxy/pkg/config" ) @@ -47,14 +46,6 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag { } } -func mustUserConfigDir() string { - dir, err := os.UserConfigDir() - if err != nil { - panic(err) - } - return filepath.Join(dir, "ocis", "proxy") -} - // ServerWithConfig applies cfg to the root flagset func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ @@ -168,14 +159,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "transport-tls-cert", - Value: flags.OverrideDefaultString(cfg.HTTP.TLSCert, path.Join(mustUserConfigDir(), "server.crt")), + Value: flags.OverrideDefaultString(cfg.HTTP.TLSCert, path.Join(pkgos.MustUserConfigDir("ocis", "proxy"), "server.crt")), Usage: "Certificate file for transport encryption", EnvVars: []string{"PROXY_TRANSPORT_TLS_CERT"}, Destination: &cfg.HTTP.TLSCert, }, &cli.StringFlag{ Name: "transport-tls-key", - Value: flags.OverrideDefaultString(cfg.HTTP.TLSKey, path.Join(mustUserConfigDir(), "server.key")), + Value: flags.OverrideDefaultString(cfg.HTTP.TLSKey, path.Join(pkgos.MustUserConfigDir("ocis", "proxy"), "server.key")), Usage: "Secret file for transport encryption", EnvVars: []string{"PROXY_TRANSPORT_TLS_KEY"}, Destination: &cfg.HTTP.TLSKey,