Skip to content

Commit

Permalink
feat: adding option for skipping init-script (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunggun-yu authored Dec 30, 2024
1 parent b2c156f commit 4dd317f
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 61 deletions.
2 changes: 1 addition & 1 deletion cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var _ = Describe("Add Command", func() {
})
})

When("add profile that is already exisiting", func() {
When("add profile that is already existing", func() {
profileName := "lab.cluster1"
envs := []string{"env1=var1", "env2=var2"}
BeforeEach(func() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var _ = Describe("Edit Command", func() {
})

When("delete profile that is not existing", func() {
profileName := fmt.Sprintf("not-exisiting-profile-%v", GinkgoRandomSeed())
profileName := fmt.Sprintf("not-existing-profile-%v", GinkgoRandomSeed())
BeforeEach(func() {
args = append(args, profileName)
input = "y"
Expand Down
15 changes: 14 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,23 @@ func cmdExampleRoot() string {
# specify env var profile to use
envp profile-name -- kubectl get namespaces
# skip init-script
envp profile-name --skip-init -- kubectl get namespaces
`
}

// flags struct for start command
type rootFlags struct {
skipInitScript bool
}

// rootCommand sets environment variable and execute command line
func rootCommand(sh *shell.ShellCommand) *cobra.Command {

// add flags
var flags rootFlags

cmd := &cobra.Command{
Use: "envp profile-name [flags] -- [command line to execute, e.g. kubectl]",
Short: "ENVP is cli wrapper that sets environment variables by profile when you execute the command line",
Expand Down Expand Up @@ -111,12 +122,14 @@ func rootCommand(sh *shell.ShellCommand) *cobra.Command {
}

// Execute command
if err := sh.Execute(command, profile); err != nil {
if err := sh.Execute(command, profile, flags.skipInitScript); err != nil {
return err
}
return nil
},
}

cmd.Flags().BoolVarP(&flags.skipInitScript, "skip-init", "s", false, `Skip running initialization scripts from the profile's "init-script"`)

return cmd
}
72 changes: 33 additions & 39 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ var _ = Describe("Root Command", func() {
})
})

When("execute command with profile that is not exisiting", func() {
profileName := fmt.Sprintf("not-exisiting-profile-%v", GinkgoRandomSeed())
When("execute command with profile that is not existing", func() {
profileName := fmt.Sprintf("not-existing-profile-%v", GinkgoRandomSeed())
BeforeEach(func() {
args = append(args, profileName, "--", "echo", "hello")
})
Expand All @@ -212,7 +212,7 @@ var _ = Describe("Root Command", func() {
})
})

When("execute command but shell command that is not exisiting", func() {
When("execute command but shell command that is not existing", func() {
profileName := "lab.cluster2"
BeforeEach(func() {
args = append(args, profileName, "--", "1293471029384701298374019872498-aslkaslkjasdfjklasdfjklasdf-202020202")
Expand All @@ -223,40 +223,34 @@ var _ = Describe("Root Command", func() {
})
})

// When("execute start command with empty string of profile", func() {
// profileName := ""
// BeforeEach(func() {
// args = append(args, profileName)
// input = "echo hello"
// })

// It("should be error", func() {
// Expect(err).Should(HaveOccurred())
// })
// })

// When("execute start command with valid inputs but omit profile name", func() {
// BeforeEach(func() {
// args = []string{}
// input = "env"
// })

// It("should not be error", func() {
// Expect(err).ShouldNot(HaveOccurred())
// })

// It("should execute for default profile and include environment variable of default profile", func() {
// p, err := cfg.DefaultProfile()
// Expect(err).ShouldNot(HaveOccurred())
// for _, e := range p.Env {
// Expect(stdout.String()).Should(ContainSubstring(e.String()))
// }
// })

// It("should include ENVP_PROFILE environment variable that is match to default profile", func() {
// p, err := cfg.DefaultProfile()
// Expect(err).ShouldNot(HaveOccurred())
// Expect(stdout.String()).Should(ContainSubstring(fmt.Sprintf("ENVP_PROFILE=%s", p.Name)))
// })
// })
When("execute start command with profile that has init-script", func() {
// profile-with-init-script profile do "echo meow"
// so that, we should see "meow" in the stdout
BeforeEach(func() {
args = append(args, "profile-with-init-script", "--", "echo", "hello")
})

It("output should include result of init-script", func() {
fmt.Println(stdout.String(), err)
Expect(err).ShouldNot(HaveOccurred())
Expect(stdout.String()).Should(ContainSubstring("meow"))
Expect(stdout.String()).Should(ContainSubstring("hello"))
})
})

When("execute start command with skip-init flag", func() {
// profile-with-init-script profile do "echo meow"
// so that, we should see "meow" in the stdout
// but, "meow" should not seen because "skip-init" flag has passed
BeforeEach(func() {
args = append(args, "profile-with-init-script", "--skip-init", "--", "echo", "hello")
})

It("output should NOT include result of init-script", func() {
fmt.Println(stdout.String(), err)
Expect(err).ShouldNot(HaveOccurred())
Expect(stdout.String()).ShouldNot(ContainSubstring("meow"))
Expect(stdout.String()).Should(ContainSubstring("hello"))
})
})
})
16 changes: 15 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ func init() {
rootCmd.AddCommand(startCommand(shell.NewShellCommand()))
}

// flags struct for start command
type startFlags struct {
skipInitScript bool
}

// example of delete command
func cmdExampleStart() string {
return `
Expand All @@ -18,12 +23,18 @@ func cmdExampleStart() string {
# start new shell session with specific profile
envp start <profile-name>
# skip "init-script" of profile when start new shell session with specific profile
envp start <profile-name> --skip-init
`
}

// deleteCommand delete/remove environment variable profile and it's envionment variables from the config file
func startCommand(sh *shell.ShellCommand) *cobra.Command {

// add flags
var flags startFlags

cmd := &cobra.Command{
Use: "start profile-name",
Short: "Start new shell session with environment variable profile",
Expand All @@ -43,10 +54,13 @@ func startCommand(sh *shell.ShellCommand) *cobra.Command {
}

// ignore error message from shell. let shell print out the errors
sh.StartShell(profile)
sh.StartShell(profile, flags.skipInitScript)

return nil
},
}

cmd.Flags().BoolVarP(&flags.skipInitScript, "skip-init", "s", false, `Skip running initialization scripts from the profile's "init-script" during shell startup`)

return cmd
}
38 changes: 36 additions & 2 deletions cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ var _ = Describe("Start Command", func() {
})
})

When("execute start command with profile that is not exisiting", func() {
profileName := fmt.Sprintf("not-exisiting-profile-%v", GinkgoRandomSeed())
When("execute start command with profile that is not existing", func() {
profileName := fmt.Sprintf("not-existing-profile-%v", GinkgoRandomSeed())
BeforeEach(func() {
args = append(args, profileName)
input = "echo hello"
Expand Down Expand Up @@ -149,4 +149,38 @@ var _ = Describe("Start Command", func() {
Expect(stdout.String()).Should(ContainSubstring(fmt.Sprintf("ENVP_PROFILE=%s", p.Name)))
})
})

When("execute start command with profile that has init-script", func() {
// profile-with-init-script profile do "echo meow"
// so that, we should see "meow" in the stdout
profileName := "profile-with-init-script"
BeforeEach(func() {
args = append(args, profileName)
input = "exit"
})

It("output should include result of init-script", func() {
fmt.Println(stdout.String(), err)
Expect(err).ShouldNot(HaveOccurred())
Expect(stdout.String()).Should(ContainSubstring("meow"))
})
})

When("execute start command with skip-init flag", func() {
// profile-with-init-script profile do "echo meow"
// so that, we should see "meow" in the stdout
// but, "meow" should not seen because "skip-init" flag has passed
profileName := "profile-with-init-script"
BeforeEach(func() {
args = append(args, profileName)
args = append(args, "--skip-init")
input = "exit"
})

It("output should NOT include result of init-script", func() {
fmt.Println(stdout.String(), err)
Expect(err).ShouldNot(HaveOccurred())
Expect(stdout.String()).ShouldNot(ContainSubstring("meow"))
})
})
})
18 changes: 10 additions & 8 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func NewShellCommand() *ShellCommand {
}

// Execute executes given command
func (s *ShellCommand) Execute(cmd []string, profile *config.NamedProfile) error {
return s.execCommand(cmd[0], cmd, profile)
func (s *ShellCommand) Execute(cmd []string, profile *config.NamedProfile, skipInitScript bool) error {
return s.execCommand(cmd[0], cmd, profile, skipInitScript)
}

// StartShell runs default shell of user to create new shell session
func (s *ShellCommand) StartShell(profile *config.NamedProfile) error {
func (s *ShellCommand) StartShell(profile *config.NamedProfile, skipInitScript bool) error {
sh := os.Getenv("SHELL")

// use /bin/sh if SHELL is not set
Expand All @@ -54,7 +54,7 @@ func (s *ShellCommand) StartShell(profile *config.NamedProfile) error {
s.Stdout.Write([]byte(fmt.Sprintln("> press ctrl+d or type exit to close session")))

// execute the command
err := s.execCommand(sh, []string{sh, "-c", sh}, profile)
err := s.execCommand(sh, []string{sh, "-c", sh}, profile, skipInitScript)
if err != nil {
s.Stderr.Write([]byte(fmt.Sprintln(color.MagentaString(err.Error()))))
}
Expand All @@ -67,7 +67,7 @@ func (s *ShellCommand) StartShell(profile *config.NamedProfile) error {
}

// execCommand executes the os/exec Command with environment variables injection
func (s *ShellCommand) execCommand(argv0 string, argv []string, profile *config.NamedProfile) error {
func (s *ShellCommand) execCommand(argv0 string, argv []string, profile *config.NamedProfile, skipInitScript bool) error {
// first arg should be the command to execute
// check if command can be found in the PATH
binary, err := exec.LookPath(argv0)
Expand All @@ -88,9 +88,11 @@ func (s *ShellCommand) execCommand(argv0 string, argv []string, profile *config.
// set ENVP_PROFILE
cmd.Env = appendEnvpProfile(cmd.Env, profile.Name)

// run init-script
if err := s.executeInitScript(profile); err != nil {
return err
if !skipInitScript {
// run init-script
if err := s.executeInitScript(profile); err != nil {
return err
}
}

// run command
Expand Down
Loading

0 comments on commit 4dd317f

Please sign in to comment.