diff --git a/commons/commands.go b/commons/commands.go index c53c5aa..c6bb4ee 100644 --- a/commons/commands.go +++ b/commons/commands.go @@ -20,6 +20,10 @@ import ( "github.com/jedib0t/go-pretty/v6/table" ) +const ( + irodsEnvironmentFileEnvKey string = "IRODS_ENVIRONMENT_FILE" +) + var ( environmentMgr *irodsclient_icommands.ICommandsEnvironmentManager account *irodsclient_types.IRODSAccount @@ -92,7 +96,7 @@ func SetCWD(cwd string) { } func SetCommonFlags(command *cobra.Command) { - command.Flags().StringP("config", "c", "", "Set config file (default is $HOME/.irods)") + command.Flags().StringP("config", "c", "", "Set config file or dir (default is $HOME/.irods)") command.Flags().BoolP("envconfig", "e", false, "Read config from environmental variables") command.Flags().BoolP("version", "v", false, "Print version") command.Flags().BoolP("help", "h", false, "Print help") @@ -145,14 +149,6 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) { } } - // read env config - /* - envConfig, err := NewEnvConfigFromENV() - if err != nil { - return false, err // stop here - } - */ - sessionFlag := command.Flags().Lookup("session") if sessionFlag != nil { // load to global variable @@ -187,57 +183,50 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) { } } - // need to modify gocommands to support change of irods env file path - /* - if len(envConfig.ConfigPath) > 0 { - if strings.HasPrefix(envConfig.ConfigPath, "~/") || strings.HasPrefix(envConfig.ConfigPath, "./") { - homePath, err := os.UserHomeDir() - if err == nil { - irodsPath := filepath.Join(homePath, envConfig.ConfigPath[2:]) - loadConfigFile(irodsPath) - } - } - err := loadConfigFile(config) + if !readConfig { + envConfigFlag := command.Flags().Lookup("envconfig") + if envConfigFlag != nil { + envConfig, err := strconv.ParseBool(envConfigFlag.Value.String()) if err != nil { logger.Error(err) return false, err // stop here } - readConfig = true - } - */ + if envConfig { + err := loadConfigEnv() + if err != nil { + logger.Error(err) + return false, err // stop here + } - envConfigFlag := command.Flags().Lookup("envconfig") - if envConfigFlag != nil { - envConfig, err := strconv.ParseBool(envConfigFlag.Value.String()) - if err != nil { - logger.Error(err) - return false, err // stop here + readConfig = true + } } + } - if envConfig { - err := loadConfigEnv() - if err != nil { - logger.Error(err) - return false, err // stop here - } + // read env config + if !readConfig { + if irodsEnvironmentFileEnvVal, ok := os.LookupEnv(irodsEnvironmentFileEnvKey); ok { + if len(irodsEnvironmentFileEnvVal) > 0 { + err := loadConfigFile(irodsEnvironmentFileEnvVal) + if err != nil { + logger.Error(err) + return false, err // stop here + } - readConfig = true + readConfig = true + } } } // default config if !readConfig { // auto detect - homePath, err := os.UserHomeDir() - if err == nil { - irodsPath := filepath.Join(homePath, ".irods") - loadConfigFile(irodsPath) - //if err != nil { - //logger.Error(err) - // ignore error - //} - } + loadConfigFile("~/.irods") + //if err != nil { + //logger.Error(err) + // ignore error + //} } resourceFlag := command.Flags().Lookup("resource") @@ -252,7 +241,7 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) { // InputMissingFields inputs missing fields func InputMissingFields() (bool, error) { if environmentMgr == nil { - envMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManagerWithDefault() + envMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager() if err != nil { return false, err } @@ -385,91 +374,38 @@ func isYAMLFile(filePath string) bool { return ext == ".yaml" || ext == ".yml" } -func isJSONFile(filePath string) bool { - st, err := os.Stat(filePath) - if err != nil { - return false - } - - if st.IsDir() { - return false - } - - ext := filepath.Ext(filePath) - return ext == ".json" -} - -func loadConfigFile(configFilePath string) error { +func loadConfigFile(configPath string) error { logger := log.WithFields(log.Fields{ "package": "commons", "function": "loadConfigFile", }) - logger.Debugf("reading config file - %s", configFilePath) + logger.Debugf("reading config file/dir - %s", configPath) // check if it is a file or a dir - _, err := os.Stat(configFilePath) + _, err := os.Stat(configPath) if err != nil { return err } - if isICommandsEnvDir(configFilePath) { - logger.Debugf("reading icommands environment file - %s", configFilePath) - iCommandsEnvMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager(configFilePath, 0) - if err != nil { - return err - } - - err = iCommandsEnvMgr.Load(sessionID) - if err != nil { - return err - } - - if iCommandsEnvMgr.Environment.LogLevel > 0 { - logLevel := log.Level(iCommandsEnvMgr.Environment.LogLevel / 2) - log.SetLevel(logLevel) - } - - loadedAccount, err := iCommandsEnvMgr.ToIRODSAccount() - if err != nil { - return err - } - - environmentMgr = iCommandsEnvMgr - account = loadedAccount - return nil - } else if isYAMLFile(configFilePath) || isJSONFile(configFilePath) { - logger.Debugf("reading gocommands YAML/JSON config file - %s", configFilePath) + if isYAMLFile(configPath) { + logger.Debugf("reading gocommands YAML config file - %s", configPath) - homePath, err := os.UserHomeDir() + iCommandsEnvMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager() if err != nil { return err } - irodsPath := filepath.Join(homePath, ".irods") - iCommandsEnvMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager(irodsPath, 0) + // load from YAML + yjBytes, err := ioutil.ReadFile(configPath) if err != nil { return err } - // load from YAML/JSON - yjBytes, err := ioutil.ReadFile(configFilePath) + config, err := NewConfigFromYAML(yjBytes) if err != nil { return err } - var config *Config - if isYAMLFile(configFilePath) { - config, err = NewConfigFromYAML(yjBytes) - if err != nil { - return err - } - } else { - config, err = NewConfigFromJSON(yjBytes) - if err != nil { - return err - } - } - iCommandsEnvMgr.Environment.CurrentWorkingDir = config.CurrentWorkingDir iCommandsEnvMgr.Environment.Host = config.Host iCommandsEnvMgr.Environment.Port = config.Port @@ -495,7 +431,42 @@ func loadConfigFile(configFilePath string) error { return nil } - return fmt.Errorf("unhandled configuration file - %s", configFilePath) + // icommands compatible + configFilePath := configPath + if isICommandsEnvDir(configPath) { + configFilePath = filepath.Join(configPath, "irods_environment.json") + } + + logger.Debugf("reading icommands environment file - %s", configFilePath) + + iCommandsEnvMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager() + if err != nil { + return err + } + + err = iCommandsEnvMgr.SetEnvironmentFilePath(configFilePath) + if err != nil { + return err + } + + err = iCommandsEnvMgr.Load(sessionID) + if err != nil { + return err + } + + if iCommandsEnvMgr.Environment.LogLevel > 0 { + logLevel := log.Level(iCommandsEnvMgr.Environment.LogLevel / 2) + log.SetLevel(logLevel) + } + + loadedAccount, err := iCommandsEnvMgr.ToIRODSAccount() + if err != nil { + return err + } + + environmentMgr = iCommandsEnvMgr + account = loadedAccount + return nil } func loadConfigEnv() error { @@ -505,13 +476,8 @@ func loadConfigEnv() error { }) logger.Debug("reading config from environment variables") - homePath, err := os.UserHomeDir() - if err != nil { - return err - } - irodsPath := filepath.Join(homePath, ".irods") - iCommandsEnvMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager(irodsPath, 0) + iCommandsEnvMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager() if err != nil { return err } diff --git a/commons/config.go b/commons/config.go index 309f525..30d437a 100644 --- a/commons/config.go +++ b/commons/config.go @@ -1,7 +1,6 @@ package commons import ( - "encoding/json" "fmt" "github.com/kelseyhightower/envconfig" @@ -9,34 +8,15 @@ import ( ) type Config struct { - CurrentWorkingDir string `yaml:"irods_cwd,omitempty" json:"irods_cwd,omitempty" envconfig:"IRODS_CWD"` - Host string `yaml:"irods_host,omitempty" json:"irods_host,omitempty" envconfig:"IRODS_HOST"` - Port int `yaml:"irods_port,omitempty" json:"irods_port,omitempty" envconfig:"IRODS_PORT"` - Username string `yaml:"irods_user_name,omitempty" json:"irods_user_name,omitempty" envconfig:"IRODS_USER_NAME"` - Zone string `yaml:"irods_zone_name,omitempty" json:"irods_zone_name,omitempty" envconfig:"IRODS_ZONE_NAME"` - DefaultResource string `yaml:"irods_default_resource,omitempty" json:"irods_default_resource,omitempty" envconfig:"IRODS_DEFAULT_RESOURCE"` - LogLevel int `yaml:"irods_log_level,omitempty" json:"irods_log_level,omitempty" envconfig:"IRODS_LOG_LEVEL"` - - Password string `yaml:"irods_user_password,omitempty" json:"irods_user_password,omitempty" envconfig:"IRODS_USER_PASSWORD"` -} - -type EnvConfig struct { - ConfigPath string `envconfig:"GOCMD_CONFIG_PATH"` - IrodsEnvironmentFile string `envconfig:"IRODS_ENVIRONMENT_FILE"` -} - -// NewConfigFromJSON creates Config from JSON -func NewConfigFromJSON(jsonBytes []byte) (*Config, error) { - config := Config{ - Port: 1247, - } - - err := json.Unmarshal(jsonBytes, &config) - if err != nil { - return nil, fmt.Errorf("failed to unmarshal JSON - %v", err) - } - - return &config, nil + CurrentWorkingDir string `yaml:"irods_cwd,omitempty" envconfig:"IRODS_CWD"` + Host string `yaml:"irods_host,omitempty" envconfig:"IRODS_HOST"` + Port int `yaml:"irods_port,omitempty" envconfig:"IRODS_PORT"` + Username string `yaml:"irods_user_name,omitempty" envconfig:"IRODS_USER_NAME"` + Zone string `yaml:"irods_zone_name,omitempty" envconfig:"IRODS_ZONE_NAME"` + DefaultResource string `yaml:"irods_default_resource,omitempty" envconfig:"IRODS_DEFAULT_RESOURCE"` + LogLevel int `yaml:"irods_log_level,omitempty" envconfig:"IRODS_LOG_LEVEL"` + + Password string `yaml:"irods_user_password,omitempty" envconfig:"IRODS_USER_PASSWORD"` } // NewConfigFromYAML creates Config from YAML @@ -66,18 +46,3 @@ func NewConfigFromENV() (*Config, error) { return &config, nil } - -// NewEnvConfigFromENV creates EnvConfig from Environmental variables -func NewEnvConfigFromENV() (*EnvConfig, error) { - envConfig := EnvConfig{ - ConfigPath: "", - IrodsEnvironmentFile: "~/.irods/irods_environment.json", - } - - err := envconfig.Process("", &envConfig) - if err != nil { - return nil, fmt.Errorf("failed to read config from environmental variables - %v", err) - } - - return &envConfig, nil -} diff --git a/commons/path.go b/commons/path.go index aa1a34f..ac4dde7 100644 --- a/commons/path.go +++ b/commons/path.go @@ -174,3 +174,37 @@ func GetCommonRootLocalDirPath(paths []string) (string, error) { return commonRootPath, nil } + +func ExpandHomeDir(path string) (string, error) { + // resolve "~/" + if path == "~" { + homedir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + return homedir, nil + } else if strings.HasPrefix(path, "~/") { + homedir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + path = filepath.Join(homedir, path[2:]) + return filepath.Clean(path), nil + } + + return path, nil +} + +func ExistFile(path string) bool { + st, err := os.Stat(path) + if err != nil { + return false + } + + if !st.IsDir() { + return true + } + return false +}