Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/env/doc.go
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions pkg/postgres/pgconfig/doc.go
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions pkg/postgres/pgconfig/pgconfig.go
Original file line number Diff line number Diff line change
@@ -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
}