From 1e5c12b0b9e2081127dedd22597dcc54fd013d6c Mon Sep 17 00:00:00 2001 From: Illyoung Choi Date: Thu, 9 Jun 2022 13:13:20 -0700 Subject: [PATCH] Bugfix: could not locate session file due to ppid changes when running it with wrapper scripts --- Makefile | 16 +++++++++++++++- commons/commands.go | 24 ++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 ++-- install/prep-shortcut-script.sh | 2 +- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 751f340..32ccb61 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PKG=github.com/cyverse/gocommands -VERSION=v0.2.1 +VERSION=v0.2.2 GIT_COMMIT?=$(shell git rev-parse HEAD) BUILD_DATE?=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") LDFLAGS?="-X '${PKG}/commons.clientVersion=${VERSION}' -X '${PKG}/commons.gitCommit=${GIT_COMMIT}' -X '${PKG}/commons.buildDate=${BUILD_DATE}'" @@ -15,8 +15,22 @@ build: CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gocmd ./cmd/*.go +.PHONY: test-release +test-release: + rm -rf release + +# amd64_linux + mkdir -p release/amd64_linux + cd install && ./prep-install-script.sh ../release/amd64_linux && cd .. + cd install && ./prep-shortcut-script.sh ../release/amd64_linux && cd .. + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags=${LDFLAGS} -o release/amd64_linux/gocmd cmd/*.go + cd release/amd64_linux && tar cf gocommands_amd64_linux_${VERSION}.tar * && mv *.tar .. && cd ../.. + + .PHONY: build-release build-release: + rm -rf release + # i386_linux mkdir -p release/i386_linux cd install && ./prep-install-script.sh ../release/i386_linux && cd .. diff --git a/commons/commands.go b/commons/commands.go index 4891f2f..c077c05 100644 --- a/commons/commands.go +++ b/commons/commands.go @@ -22,6 +22,7 @@ var ( environmentMgr *irodsclient_icommands.ICommandsEnvironmentManager account *irodsclient_types.IRODSAccount + sessionID int resourceServer string ) @@ -74,7 +75,7 @@ func SetCWD(cwd string) { } session.CurrentWorkingDir = filepath.Clean(cwd) - environmentMgr.SaveSession() + environmentMgr.SaveSession(sessionID) } func SetCommonFlags(command *cobra.Command) { @@ -82,6 +83,7 @@ func SetCommonFlags(command *cobra.Command) { command.Flags().BoolP("version", "v", false, "Print version") command.Flags().BoolP("help", "h", false, "Print help") command.Flags().BoolP("debug", "d", false, "Enable debug mode") + command.Flags().Int32P("session", "s", -1, "Set session ID") command.Flags().StringP("resource", "", "", "Set resource server") } @@ -129,6 +131,24 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) { } } + sessionFlag := command.Flags().Lookup("session") + if sessionFlag != nil { + // load to global variable + sessionIDString := sessionFlag.Value.String() + sessionIDInt, err := strconv.ParseInt(sessionIDString, 10, 32) + if err != nil { + logger.Error(err) + return false, err // stop here + } + sessionID = int(sessionIDInt) + } + + if sessionID < 0 { + sessionID = os.Getppid() + } + + logger.Debugf("use sessionID - %d", sessionID) + configFlag := command.Flags().Lookup("config") if configFlag != nil { config := configFlag.Value.String() @@ -305,7 +325,7 @@ func loadConfigFile(command *cobra.Command, configFilePath string) error { return err } - err = iCommandsEnvMgr.Load() + err = iCommandsEnvMgr.Load(sessionID) if err != nil { return err } diff --git a/go.mod b/go.mod index ca28984..3b8cc88 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cyverse/gocommands go 1.17 require ( - github.com/cyverse/go-irodsclient v0.9.3 + github.com/cyverse/go-irodsclient v0.9.4-0.20220609195211-94e54487aea5 github.com/jedib0t/go-pretty/v6 v6.3.1 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index d8c9795..7309943 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/cyverse/go-irodsclient v0.9.3 h1:ERfEZJYOH8ZvAceKD68iCfesWyMyqGf/VBVUwxudD9Y= -github.com/cyverse/go-irodsclient v0.9.3/go.mod h1:vEllPYPlJhNUp/op6e4/zDKt/wOIeN8Y7Nyy4QpR/F0= +github.com/cyverse/go-irodsclient v0.9.4-0.20220609195211-94e54487aea5 h1:filixIT8Lj1sdMfSaieG3A0jwhU90/QaBOLCrrU04YQ= +github.com/cyverse/go-irodsclient v0.9.4-0.20220609195211-94e54487aea5/go.mod h1:vEllPYPlJhNUp/op6e4/zDKt/wOIeN8Y7Nyy4QpR/F0= 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= diff --git a/install/prep-shortcut-script.sh b/install/prep-shortcut-script.sh index 3a7620b..9e3fb3a 100755 --- a/install/prep-shortcut-script.sh +++ b/install/prep-shortcut-script.sh @@ -9,7 +9,7 @@ main() for i in "${SUBCOMMANDS[@]}" do - echo -e "#!/bin/bash\nbaseDir=\$(dirname \"\$0\")\n\$baseDir/gocmd $i \$@" > $1/go$i + echo -e "#!/bin/bash\nbaseDir=\$(dirname \"\$0\")\n\$baseDir/gocmd -s \$PPID $i \$@" > $1/go$i chmod 700 $1/go$i done }