本文整理汇总了Golang中golang.org/x/crypto/ssh.Session.Setenv方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.Setenv方法的具体用法?Golang Session.Setenv怎么用?Golang Session.Setenv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/crypto/ssh.Session
的用法示例。
在下文中一共展示了Session.Setenv方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: prepareCommand
func prepareCommand(session *ssh.Session, cmd *command) error {
for _, env := range cmd.Env {
variable := strings.Split(env, "=")
if len(variable) != 2 {
continue
}
if err := session.Setenv(variable[0], variable[1]); err != nil {
return err
}
}
if cmd.Stdout == nil {
session.Stdout = &cmd.stdout
} else {
session.Stdout = cmd.Stdout
}
if cmd.Stdin != nil {
session.Stdin = cmd.Stdin
}
if cmd.Stderr == nil {
session.Stderr = &cmd.stderr
} else {
session.Stderr = cmd.Stderr
}
return nil
}
示例2: configureSessionEnv
func configureSessionEnv(session *ssh.Session, env map[string]string) error {
fmt.Println("Setting environment variables")
for key, value := range env {
if err := session.Setenv(key, value); err != nil {
fmt.Printf("Unable to set environment %s = %s\n", key, value)
return err
}
}
fmt.Println("Done setting environment variables")
return nil
}
示例3: prepareCommand
func (client *clientSSH) prepareCommand(session *ssh.Session, cmd *sshCommand) error {
for _, env := range cmd.env {
variable := strings.Split(env, "=")
if len(variable) != 2 {
continue
}
if err := session.Setenv(variable[0], variable[1]); err != nil {
return err
}
}
return nil
}
示例4: prepareCommand
func (client *SSHClient) prepareCommand(session *ssh.Session, cmd *SSHCommand) error {
for _, env := range cmd.Env {
variable := strings.Split(env, "=")
if len(variable) != 2 {
continue
}
if err := session.Setenv(variable[0], variable[1]); err != nil {
return err
}
}
if cmd.Stdin != nil {
stdin, err := session.StdinPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdin for session: %v", err)
}
go io.Copy(stdin, cmd.Stdin)
}
if cmd.Stdout != nil {
stdout, err := session.StdoutPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdout for session: %v", err)
}
go io.Copy(cmd.Stdout, stdout)
}
if cmd.Stderr != nil {
stderr, err := session.StderrPipe()
if err != nil {
return fmt.Errorf("Unable to setup stderr for session: %v", err)
}
go io.Copy(cmd.Stderr, stderr)
}
return nil
}
示例5:
It("includes a default environment excluding PATH", func() {
result, err := session.Output("/usr/bin/env")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring(fmt.Sprintf("PATH=/bin:/usr/bin")))
Expect(result).To(ContainSubstring(fmt.Sprintf("LANG=en_US.UTF8")))
Expect(result).To(ContainSubstring(fmt.Sprintf("TEST=FOO")))
Expect(result).To(ContainSubstring(fmt.Sprintf("HOME=%s", os.Getenv("HOME"))))
Expect(result).To(ContainSubstring(fmt.Sprintf("USER=%s", os.Getenv("USER"))))
})
})
Context("when environment variables are requested", func() {
Context("before starting the command", func() {
It("runs the command with the specified environment", func() {
err := session.Setenv("ENV1", "value1")
Expect(err).NotTo(HaveOccurred())
err = session.Setenv("ENV2", "value2")
Expect(err).NotTo(HaveOccurred())
result, err := session.Output("/usr/bin/env")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("ENV1=value1"))
Expect(result).To(ContainSubstring("ENV2=value2"))
})
It("uses the value last specified", func() {
err := session.Setenv("ENV1", "original")
Expect(err).NotTo(HaveOccurred())