Skip to content

Commit

Permalink
Save cwd to session file even when reading config from env
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Oct 26, 2023
1 parent cba6b12 commit 861d069
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/flag/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) {
}

logger.Debugf("use sessionID - %d", myCommonFlagValues.SessionID)
commons.SetSessionID(myCommonFlagValues.SessionID)

readConfig := false

Expand Down
6 changes: 5 additions & 1 deletion cmd/subcmd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func changeWorkingDir(fs *irodsclient_fs.FileSystem, collectionPath string) erro
return xerrors.Errorf("failed to get collection %s: %w", collectionPath, err)
}

commons.SetCWD(collectionPath)
err = commons.SetCWD(collectionPath)
if err != nil {
return xerrors.Errorf("failed to set current working collection %s: %w", collectionPath, err)
}

return nil
}
90 changes: 82 additions & 8 deletions commons/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ var (
sessionID int
)

// GetEnvironmentManager returns environment manager
func GetEnvironmentManager() *irodsclient_icommands.ICommandsEnvironmentManager {
return environmentManager
}

// GetConfig returns config
func GetConfig() *Config {
return appConfig
}

// SetDefaultConfigIfEmpty sets default config if empty
func SetDefaultConfigIfEmpty() {
if environmentManager == nil {
iCommandsEnvMgr, _ := irodsclient_icommands.CreateIcommandsEnvironmentManager()
Expand All @@ -47,6 +50,12 @@ func SetDefaultConfigIfEmpty() {
}
}

// SetSessionID sets session id
func SetSessionID(id int) {
sessionID = id
}

// SyncAccount syncs irods account
func SyncAccount() error {
newAccount, err := environmentManager.ToIRODSAccount()
if err != nil {
Expand All @@ -69,6 +78,7 @@ func SyncAccount() error {
return nil
}

// GetAccount returns irods account
func GetAccount() *irodsclient_types.IRODSAccount {
return account
}
Expand All @@ -87,6 +97,7 @@ func getCWD(env *irodsclient_icommands.ICommandsEnvironment) string {
return path.Clean(currentWorkingDir)
}

// GetCWD returns current working directory
func GetCWD() string {
session := environmentManager.Session
sessionPath := getCWD(session)
Expand All @@ -105,22 +116,31 @@ func GetCWD() string {
return envPath
}

// GetZone returns zone
func GetZone() string {
env := environmentManager.Environment
return env.Zone
}

// GetUsername returns username
func GetUsername() string {
env := environmentManager.Environment
return env.Username
}

// GetHomeDir returns home dir
func GetHomeDir() string {
env := environmentManager.Environment
return fmt.Sprintf("/%s/home/%s", env.Zone, env.Username)
}

func SetCWD(cwd string) {
// SetCWD sets current workding directory
func SetCWD(cwd string) error {
logger := log.WithFields(log.Fields{
"package": "commons",
"function": "SetCWD",
})

env := environmentManager.Environment
session := environmentManager.Session
if !strings.HasPrefix(cwd, "/") {
Expand All @@ -129,7 +149,13 @@ func SetCWD(cwd string) {
}

session.CurrentWorkingDir = path.Clean(cwd)
environmentManager.SaveSession(sessionID)

logger.Debugf("save session to file - id %d", sessionID)
err := environmentManager.SaveSession(sessionID)
if err != nil {
return xerrors.Errorf("failed to save session: %w", err)
}
return nil
}

// InputMissingFields inputs missing fields
Expand Down Expand Up @@ -337,18 +363,24 @@ func isICommandsEnvDir(filePath string) bool {
return false
}

envFilePath := filepath.Join(filePath, "irods_environment.json")

stEnv, err := os.Stat(envFilePath)
entries, err := os.ReadDir(filePath)
if err != nil {
return false
}

if stEnv.IsDir() {
return false
for _, entry := range entries {
if !entry.IsDir() {
if strings.HasPrefix(entry.Name(), "irods_environment.json.") {
return true
} else if entry.Name() == "irods_environment.json" {
return true
} else if entry.Name() == ".irodsA" {
return true
}
}
}

return true
return false
}

func isYAMLFile(filePath string) bool {
Expand Down Expand Up @@ -577,6 +609,48 @@ func LoadConfigFromEnv() error {
log.SetLevel(logLevel)
}

// read session from ~/.irods
configPath, err := ExpandHomeDir("~/.irods")
if err != nil {
return xerrors.Errorf("failed to expand home dir for %s: %w", "~/.irods", err)
}

configPath, err = filepath.Abs(configPath)
if err != nil {
return xerrors.Errorf("failed to compute absolute path for %s: %w", configPath, err)
}

logger.Debugf("reading config file/dir - %s", configPath)
// check if it is a file or a dir
_, err = os.Stat(configPath)
if err != nil {
if !os.IsNotExist(err) {
return xerrors.Errorf("failed to stat %s: %w", configPath, err)
}
}

configFilePath := configPath
if isICommandsEnvDir(configPath) {
configFilePath = filepath.Join(configPath, "irods_environment.json")
}

err = iCommandsEnvMgr.SetEnvironmentFilePath(configFilePath)
if err != nil {
return xerrors.Errorf("failed to set iCommands Environment file %s: %w", configFilePath, err)
}

err = iCommandsEnvMgr.Load(sessionID)
if err != nil {
return xerrors.Errorf("failed to read iCommands Environment: %w", err)
}

setICommandsEnvMgrToConfig(config, iCommandsEnvMgr)

if iCommandsEnvMgr.Environment.LogLevel > 0 {
logLevel := getLogrusLogLevel(iCommandsEnvMgr.Environment.LogLevel)
log.SetLevel(logLevel)
}

environmentManager = iCommandsEnvMgr
appConfig = config

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/creativeprojects/go-selfupdate v1.0.1
github.com/cyverse/go-irodsclient v0.12.16
github.com/cyverse/go-irodsclient v0.12.17-0.20231026230840-0280081e7d07
github.com/dustin/go-humanize v1.0.1
github.com/gliderlabs/ssh v0.3.5
github.com/jedib0t/go-pretty/v6 v6.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creativeprojects/go-selfupdate v1.0.1 h1:5Un4MTv4puCR5GBgkDLC14J72fljGuMC60E63cFGq1o=
github.com/creativeprojects/go-selfupdate v1.0.1/go.mod h1:nm7AWUJfrfYt/SB97NAcMhR0KEpPqlrVHXkWFti+ezw=
github.com/cyverse/go-irodsclient v0.12.16 h1:V1NWXvWioaZ511ZFHDkj7MuRLrx5DZqhaeATjanhl+w=
github.com/cyverse/go-irodsclient v0.12.16/go.mod h1:9douI0OllPASzv9wwlOwbpnfI8xw0/zFCaAA8D3gOjI=
github.com/cyverse/go-irodsclient v0.12.17-0.20231026230840-0280081e7d07 h1:aek8ulcjIhmbpGygzFgMT4LIBlBHZz5H9F0Pzuxrv7c=
github.com/cyverse/go-irodsclient v0.12.17-0.20231026230840-0280081e7d07/go.mod h1:9douI0OllPASzv9wwlOwbpnfI8xw0/zFCaAA8D3gOjI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit 861d069

Please sign in to comment.