1
0
Fork 0
mirror of https://github.com/on4kjm/FLEcli.git synced 2025-02-07 16:32:46 +01:00
FLEcli/flecmd/root.go
Nic Heath 5560c5a68a
Add check to verify output file directory exists. (#131)
* Add check to verify output file directory exists.
* Change (and test) the error handling of the CheckDir function
* Add ADIF arguments tests 
* Add CSV arguments tests

---------

Co-authored-by: Jean-Marc MEESSEN <jean-marc@meessen-web.org>
2024-03-20 22:10:04 +01:00

98 lines
2.9 KiB
Go

package flecmd
/*
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var cfgFile string
var inputFilename string
var isInterpolateTime bool
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "FLEcli",
Short: "A Command Line \"Fast Log Entry\" (FLE) processor",
TraverseChildren: true,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.FLEcli.yaml)")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".FLEcli" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".FLEcli")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
// CheckDir verifies a given path/file string actually exists. If it does not
// then exit with an error.
func CheckDir(file string) error {
path := filepath.Dir(file)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("The directory of specified output file (%s) does not exist.", path)
}
}
return nil
}