Skip to content

Commit

Permalink
Implement env subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Nov 22, 2022
1 parent 34822f1 commit 348b292
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {

// add sub commands
subcmd.AddInitCommand(rootCmd)
subcmd.AddEnvCommand(rootCmd)
subcmd.AddPasswdCommand(rootCmd)
subcmd.AddPwdCommand(rootCmd)
subcmd.AddCdCommand(rootCmd)
Expand Down
49 changes: 49 additions & 0 deletions cmd/subcmd/env.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/subcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
52 changes: 50 additions & 2 deletions commons/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var (
account *irodsclient_types.IRODSAccount

sessionID int
configPath string
resourceServer string
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 348b292

Please sign in to comment.