From 348b292aabfb2f46524523948724d7daa5840491 Mon Sep 17 00:00:00 2001 From: Illyoung Choi Date: Mon, 21 Nov 2022 17:16:01 -0700 Subject: [PATCH] Implement env subcommand --- cmd/gocmd.go | 1 + cmd/subcmd/env.go | 49 ++++++++++++++++++++++++++++++++++++++++++ cmd/subcmd/init.go | 2 +- commons/commands.go | 52 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 cmd/subcmd/env.go diff --git a/cmd/gocmd.go b/cmd/gocmd.go index 4e5dcd2..414f78f 100644 --- a/cmd/gocmd.go +++ b/cmd/gocmd.go @@ -60,6 +60,7 @@ func main() { // add sub commands subcmd.AddInitCommand(rootCmd) + subcmd.AddEnvCommand(rootCmd) subcmd.AddPasswdCommand(rootCmd) subcmd.AddPwdCommand(rootCmd) subcmd.AddCdCommand(rootCmd) diff --git a/cmd/subcmd/env.go b/cmd/subcmd/env.go new file mode 100644 index 0000000..6757bd7 --- /dev/null +++ b/cmd/subcmd/env.go @@ -0,0 +1,49 @@ +package subcmd + +import ( + "fmt" + "os" + + "github.com/cyverse/gocommands/commons" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var envCmd = &cobra.Command{ + Use: "env", + Short: "Print current irods environment", + Long: `This prints out current irods environment.`, + RunE: processEnvCommand, +} + +func AddEnvCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(envCmd) + + rootCmd.AddCommand(envCmd) +} + +func processEnvCommand(command *cobra.Command, args []string) error { + logger := log.WithFields(log.Fields{ + "package": "main", + "function": "processEnvCommand", + }) + + cont, err := commons.ProcessCommonFlags(command) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + return nil + } + + if !cont { + return nil + } + + err = commons.PrintEnvironment() + if err != nil { + logger.Error(err) + fmt.Fprintln(os.Stderr, err.Error()) + return nil + } + return nil +} diff --git a/cmd/subcmd/init.go b/cmd/subcmd/init.go index f5e5e45..da27896 100644 --- a/cmd/subcmd/init.go +++ b/cmd/subcmd/init.go @@ -65,7 +65,7 @@ func processInitCommand(command *cobra.Command, args []string) error { if updated { // save - err := commons.GetEnvironmentManager().Save() + err := commons.GetEnvironmentManager().SaveEnvironment() if err != nil { logger.Error(err) fmt.Fprintln(os.Stderr, err.Error()) diff --git a/commons/commands.go b/commons/commands.go index c6bb4ee..607bf24 100644 --- a/commons/commands.go +++ b/commons/commands.go @@ -29,7 +29,6 @@ var ( account *irodsclient_types.IRODSAccount sessionID int - configPath string resourceServer string ) @@ -380,9 +379,19 @@ func loadConfigFile(configPath string) error { "function": "loadConfigFile", }) + configPath, err := ExpandHomeDir(configPath) + if err != nil { + return err + } + + configPath, err = filepath.Abs(configPath) + if err != nil { + return err + } + logger.Debugf("reading config file/dir - %s", configPath) // check if it is a file or a dir - _, err := os.Stat(configPath) + _, err = os.Stat(configPath) if err != nil { return err } @@ -557,6 +566,45 @@ func PrintAccount() error { return nil } +func PrintEnvironment() error { + envMgr := GetEnvironmentManager() + if envMgr == nil { + return errors.New("environment is not set") + } + + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + + t.AppendRows([]table.Row{ + { + "iRODS Session Environment File", + envMgr.GetSessionFilePath(os.Getppid()), + }, + { + "iRODS Environment File", + envMgr.GetEnvironmentFilePath(), + }, + { + "iRODS Host", + envMgr.Environment.Host, + }, + { + "iRODS Port", + envMgr.Environment.Port, + }, + { + "iRODS Zone", + envMgr.Environment.Zone, + }, + { + "iRODS Username", + envMgr.Environment.Username, + }, + }, table.RowConfig{}) + t.Render() + return nil +} + // InputYN inputs Y or N // true for Y, false for N func InputYN(msg string) bool {