func (self *interaction) ReadPassword(text string) (result string, err error) { command, err := self.config.Eval("", "password", "command", os.Getenv) if err != nil { return } env := func(name string) string { switch name { case "TEXT": return text } return "" } arguments, err := self.config.Eval("", "password", "arguments", env) if err != nil { return } buffer := &bytes.Buffer{} type barrierData struct { n int64 err error } barrier := make(chan barrierData) prepare := func(cmd *exec.Cmd) (err error) { out, err := cmd.StdoutPipe() if err != nil { return errors.Decorated(err) } go func() { n, err := buffer.ReadFrom(out) barrier <- barrierData{n, err} }() return } resulter := make(chan string, 1) run := func(cmd *exec.Cmd) (err error) { data := <-barrier if data.err != nil { return errors.Decorated(err) } // the last character is a \n -- ignore it resulter <- string(buffer.Bytes()[:data.n-1]) return } err = exec.Command(prepare, run, "bash", "-c", fmt.Sprintf("%s %s", command, arguments)) if err != nil { return } result = <-resulter return }
func (self *interaction) Pager(text string) (err error) { pipe := make(chan io.WriteCloser, 1) prepare := func(cmd *exec.Cmd) (err error) { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr p, err := cmd.StdinPipe() if err != nil { return errors.Decorated(err) } pipe <- p return } run := func(cmd *exec.Cmd) (err error) { p := <-pipe p.Write([]byte(text)) err = p.Close() if err != nil { return errors.Decorated(err) } return } err = exec.Command(prepare, run, "less", "-R") return }
func xclip(name string, selection string) (err error) { pipe := make(chan io.WriteCloser, 1) prepare := func(cmd *exec.Cmd) (err error) { p, err := cmd.StdinPipe() if err != nil { return errors.Decorated(err) } pipe <- p return } run := func(cmd *exec.Cmd) (err error) { p := <-pipe p.Write([]byte(name)) err = p.Close() if err != nil { return errors.Decorated(err) } return } err = exec.Command(prepare, run, "xclip", "-selection", selection) return }
func displayMenu(config core.Config, srv server.Server, list []string) (err error) { command, err := config.Eval("", "menu", "command", os.Getenv) if err != nil { return } arguments, err := config.Eval("", "menu", "arguments", nil) if err != nil { return } barrier := make(chan error) pipe := make(chan io.WriteCloser, 1) prepare := func(cmd *exec.Cmd) (err error) { p, err := cmd.StdinPipe() if err != nil { return errors.Decorated(err) } out, err := cmd.StdoutPipe() if err != nil { return errors.Decorated(err) } mmi, err := ui.Ui(srv, config) if err != nil { return } go clipboard(mmi, out, barrier) pipe <- p return } run := func(cmd *exec.Cmd) (err error) { p := <-pipe for _, entry := range list { p.Write([]byte(entry + "\n")) } err = p.Close() if err != nil { return errors.Decorated(err) } e := <-barrier if e != io.EOF { err = errors.Decorated(e) } return } err = exec.Command(prepare, run, "bash", "-c", fmt.Sprintf("%s %s", command, arguments)) return }
func (self *vault) save(config core.Config) (err error) { outstream, err := self.out() if err != nil { return errors.Decorated(err) } defer outstream.Close() cipher, err := config.Eval("", "vault", "openssl.cipher", os.Getenv) if err != nil { return err } pipe := make(chan io.WriteCloser, 1) prepare := func(cmd *exec.Cmd) (err error) { cmd.Env = append(os.Environ(), fmt.Sprintf("VAULT_MASTER=%s", self.master)) cmd.Stdout = outstream p, err := cmd.StdinPipe() if err != nil { return errors.Decorated(err) } pipe <- p return } run := func(cmd *exec.Cmd) (err error) { p := <-pipe for _, k := range self.data { code := k.Encoded() n, err := p.Write([]byte(code)) if err != nil { return errors.Decorated(err) } if n < len(code) { return errors.Newf("Incomplete write") } } err = p.Close() if err != nil { return errors.Decorated(err) } return } err = exec.Command(prepare, run, "openssl", cipher, "-a", "-pass", "env:VAULT_MASTER") return }
func (self *vault) Open(master string, config core.Config) (err error) { instream, err := self.in() if err != nil { return errors.Decorated(err) } defer instream.Close() cipher, err := config.Eval("", "vault", "openssl.cipher", os.Getenv) if err != nil { return } barrier := make(chan error) prepare := func(cmd *exec.Cmd) (err error) { cmd.Env = append(os.Environ(), fmt.Sprintf("VAULT_MASTER=%s", master)) cmd.Stdin = instream out, err := cmd.StdoutPipe() if err != nil { return errors.Decorated(err) } go self.decode(self, out, barrier) return } run := func(cmd *exec.Cmd) (err error) { e := <-barrier if e != io.EOF { err = errors.Decorated(e) } return } err = exec.Command(prepare, run, "openssl", cipher, "-d", "-a", "-pass", "env:VAULT_MASTER") if err != nil { return } self.master = master self.open = true return }
func startServer() (err error) { pipe := make(chan io.WriteCloser, 1) prepare := func(cmd *exec.Cmd) (err error) { p, err := cmd.StdinPipe() if err != nil { return errors.Decorated(err) } pipe <- p return } run := func(cmd *exec.Cmd) (err error) { p := <-pipe dir := dirname() var exe string if dir == "" { exe, err = osexec.LookPath("server") if err != nil { return errors.Decorated(err) } } else { exe = fmt.Sprintf("%s/server", dir) } var rc string if len(os.Args) > 1 { rc = os.Args[1] } p.Write([]byte(fmt.Sprintf("#!/bin/bash\n%s \"%s\" > /tmp/server-%s.log 2>&1 & disown\n", exe, rc, time.Now().Format("20060102150405")))) err = p.Close() if err != nil { return errors.Decorated(err) } return } err = exec.Command(prepare, run, "at", "now") if err != nil { return } return }
func (self *scp) SaveVault(file string) (err error) { args, err := self.arguments() if err != nil { return } prepare := func(cmd *exec.Cmd) (err error) { if self.proxy != nil { self.proxy.Install(cmd) } cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return } err = exec.Command(prepare, nil, "scp", append([]string{file}, args...)...) return }
func (self *curl) doCurl(option, file, request string) (err error) { args, err := self.arguments(option, file, request) if err != nil { return } prepare := func(cmd *exec.Cmd) (err error) { if self.proxy != nil { self.proxy.Install(cmd) } cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return } err = exec.Command(prepare, nil, "curl", args...) return }
func (self *scp) LoadVault(file string) (err error) { args, err := self.arguments() if err != nil { return } prepare := func(cmd *exec.Cmd) (err error) { if self.proxy != nil { self.proxy.Install(cmd) } cmd.Env = append(os.Environ(), "SSH_ASKPASS=true") cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return } err = exec.Command(prepare, nil, "scp", append(args, file)...) return }