diff --git a/pkg/env/doc.go b/pkg/env/doc.go new file mode 100644 index 00000000..66d2e078 --- /dev/null +++ b/pkg/env/doc.go @@ -0,0 +1,18 @@ +/* +Copyright The CloudNativePG Contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package env contains useful functions to manage the system environment +package env diff --git a/pkg/env/env.go b/pkg/env/env.go new file mode 100644 index 00000000..f01b9c70 --- /dev/null +++ b/pkg/env/env.go @@ -0,0 +1,28 @@ +/* +Copyright The CloudNativePG Contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package env + +import "os" + +// GetOrDefault gets the value of an environment variable or the +// passed default value. +func GetOrDefault(env, def string) string { + if value, ok := os.LookupEnv(env); ok { + return value + } + return def +} diff --git a/pkg/postgres/pgconfig/doc.go b/pkg/postgres/pgconfig/doc.go new file mode 100644 index 00000000..89af761d --- /dev/null +++ b/pkg/postgres/pgconfig/doc.go @@ -0,0 +1,18 @@ +/* +Copyright The CloudNativePG Contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package pgconfig contains a Go interface for the "pg_config" utility +package pgconfig diff --git a/pkg/postgres/pgconfig/pgconfig.go b/pkg/postgres/pgconfig/pgconfig.go new file mode 100644 index 00000000..c43ef99d --- /dev/null +++ b/pkg/postgres/pgconfig/pgconfig.go @@ -0,0 +1,52 @@ +/* +Copyright The CloudNativePG Contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pgconfig + +import ( + "fmt" + "os/exec" + "strings" +) + +// ConfigurationParameter represents a PostgreSQL configuration parameter name +type ConfigurationParameter string + +const ( + // BinDir is the location user executables. Use this, for example, + // to find the psql program. This is normally also the location + // where the pg_config program resides. + BinDir ConfigurationParameter = "bindir" + + // PkgLibDir is the location of dynamically loadable modules, or + // where the server would search for them. (Other + // architecture-dependent data files might also be installed in + // this directory.) + PkgLibDir ConfigurationParameter = "pkglibdir" + + // ShareDir is the location of architecture-independent support + // files. + ShareDir ConfigurationParameter = "sharedir" +) + +// GetCondifurationParameter retrieves a PostgreSQL installation configuration parameter +func GetCondifurationParameter(pgConfigBinary string, parameter ConfigurationParameter) (string, error) { + out, err := exec.Command(pgConfigBinary, "--"+string(parameter)).Output() //nolint:gosec + if err != nil { + return "", fmt.Errorf("failed to get the %q parameter from pg_config: %w", parameter, err) + } + return strings.TrimSpace(string(out)), nil +}