add new os pkg on ocis-pkg

This commit is contained in:
A.Unger
2021-06-08 13:08:32 +02:00
parent 24b94dd2f1
commit 83f628052d
5 changed files with 62 additions and 36 deletions

View File

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

View File

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

17
ocis-pkg/os/os.go Normal file
View File

@@ -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)
}

36
ocis-pkg/os/os_test.go Normal file
View File

@@ -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)
}
})
}
}

View File

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