blob: 79156cea215431e22a735c2f7fb5eca7cde43b21 [file]
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 cmd
import (
"fmt"
"github.com/apache/cloudberry-backup/gpbackman/gpbckpconfig"
"github.com/apache/cloudberry-backup/gpbackman/textmsg"
"github.com/apache/cloudberry-go-libs/gplog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var version string
// Flags for the gpbackman command (rootCmd)
var (
rootHistoryDB string
rootAutoLoadHistoryDB bool
rootLogFile string
rootLogLevelConsole string
rootLogLevelFile string
)
var rootCmd = &cobra.Command{
Use: commandName,
Short: "gpBackMan - utility for managing backups created by gpbackup",
Args: cobra.NoArgs,
}
func init() {
rootCmd.PersistentFlags().StringVar(
&rootHistoryDB,
historyDBFlagName,
"",
"full path to the gpbackup_history.db file",
)
rootCmd.PersistentFlags().BoolVar(
&rootAutoLoadHistoryDB,
autoLoadHistoryDBFlagName,
false,
"resolve gpbackup_history.db from $COORDINATOR_DATA_DIRECTORY when --history-db is unset",
)
rootCmd.PersistentFlags().StringVar(
&rootLogFile,
logFileFlagName,
"",
"full path to log file directory, if not specified, the log file will be created in the $HOME/gpAdminLogs directory",
)
rootCmd.PersistentFlags().StringVar(
&rootLogLevelConsole,
logLevelConsoleFlagName,
"info",
"level for console logging (error, info, debug, verbose)",
)
rootCmd.PersistentFlags().StringVar(
&rootLogLevelFile,
logLevelFileFlagName,
"info",
"level for file logging (error, info, debug, verbose)",
)
}
func doInit() {
rootCmd.Version = version
// If log-file flag is specified the log file will be created in the specified directory
gplog.InitializeLogging(commandName, rootLogFile)
}
func getVersion() string {
return rootCmd.Version
}
// These flag checks are applied for all commands:
func doRootFlagValidation(flags *pflag.FlagSet, checkFileExists bool) {
var err error
// If history-db flag is specified and full path.
// The existence of the file is checked by condition from each specific command.
// Not all commands require a history db file to exist.
if flags.Changed(historyDBFlagName) {
err = gpbckpconfig.CheckFullPath(rootHistoryDB, checkFileExists)
if err != nil {
gplog.Error("%s", textmsg.ErrorTextUnableValidateFlag(rootHistoryDB, historyDBFlagName, err))
execOSExit(exitErrorCode)
}
}
// Check, that the log level is correct.
err = setLogLevelConsole(rootLogLevelConsole)
if err != nil {
gplog.Error("%s", textmsg.ErrorTextUnableValidateFlag(rootLogLevelConsole, logLevelConsoleFlagName, err))
execOSExit(exitErrorCode)
}
err = setLogLevelFile(rootLogLevelFile)
if err != nil {
gplog.Error("%s", textmsg.ErrorTextUnableValidateFlag(rootLogLevelFile, logLevelFileFlagName, err))
execOSExit(exitErrorCode)
}
}
func Execute() {
doInit()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
execOSExit(exitErrorCode)
}
}