当前位置: 首页>>代码示例>>Golang>>正文


Golang Terminal.Write方法代码示例

本文整理汇总了Golang中golang.org/x/crypto/ssh/terminal.Terminal.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang Terminal.Write方法的具体用法?Golang Terminal.Write怎么用?Golang Terminal.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在golang.org/x/crypto/ssh/terminal.Terminal的用法示例。


在下文中一共展示了Terminal.Write方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: newStartedMongoState

// newStartedMongoState takes a term argument to have a context for printing
func (msh *mongoStateHolder) newStartedMongoState(replicaID, backupID, mongodPath, mongoPath, mountPath string,
	term *terminal.Terminal, driver *strata.Driver) (*mongoState, error) {

	mongoState := mongoState{}
	var err error
	mongoState.dbpath, err = ioutil.TempDir("", "mongoq_")
	if err != nil {
		return &mongoState, err
	}

	if err := driver.RestoreReadOnly(replicaID, backupID, mountPath, mongoState.dbpath); err != nil {
		return &mongoState, err
	}

	// Try to start mongod
	// Look for output text to determine success
	// If output text indicates that port is already in use, try another port
	for mongoState.mongod == nil {
		mongoState.mongod = exec.Command(mongodPath, "--port="+strconv.Itoa(msh.nextPort),
			"--dbpath="+mongoState.dbpath, "--storageEngine=rocksdb", "--rocksdbConfigString=max_open_files=10")

		mongodOut, err := mongoState.mongod.StdoutPipe()
		if err != nil {
			return &mongoState, err
		}
		defer mongodOut.Close()
		if err := mongoState.mongod.Start(); err != nil {
			return &mongoState, err
		}
		// Wait until mongod is ready to accept a connection
		for {
			buf := make([]byte, 10000)
			n, _ := mongodOut.Read(buf)
			term.Write(buf[:n]) // If there is a problem starting mongod, the user should see it and kill process
			rec := string(buf[:n])
			if strings.Contains(rec, "waiting for connections on port") {
				mongodOut.Close()
				break
			} else if strings.Contains(rec, "Address already in use for socket") {
				mongodOut.Close()
				if err := mongoState.mongod.Process.Kill(); err != nil {
					return &mongoState, err
				}
				mongoState.mongod = nil
				term.Write([]byte("MONGOQ Trying to start mongod again on another port\n"))
				msh.nextPort++
				break
			}
		}
	}

	mongoState.mongo = exec.Command(mongoPath, "--port="+strconv.Itoa(msh.nextPort))
	msh.nextPort++
	mongoState.mongoPty, err = pty.Start(mongoState.mongo)
	return &mongoState, err
}
开发者ID:facebookgo,项目名称:rocks-strata,代码行数:57,代码来源:mongoq.go

示例2: terminalMessage

func terminalMessage(term *terminal.Terminal, color []byte, msg string, critical bool) {
	line := make([]byte, 0, len(msg)+16)

	line = append(line, ' ')
	line = append(line, color...)
	line = append(line, '*')
	line = append(line, term.Escape.Reset...)
	line = append(line, []byte(fmt.Sprintf(" (%s) ", time.Now().Format(time.Kitchen)))...)
	if critical {
		line = append(line, term.Escape.Red...)
	}
	line = appendTerminalEscaped(line, []byte(msg))
	if critical {
		line = append(line, term.Escape.Reset...)
	}
	line = append(line, '\n')
	term.Write(line)
}
开发者ID:PMaynard,项目名称:coyim,代码行数:18,代码来源:term.go

示例3: promptForForm

// promptForForm runs an XEP-0004 form and collects responses from the user.
func promptForForm(term *terminal.Terminal, user, password, title, instructions string, fields []interface{}) error {
	info(term, "The server has requested the following information. Text that has come from the server will be shown in red.")

	// formStringForPrinting takes a string form the form and returns an
	// escaped version with codes to make it show as red.
	formStringForPrinting := func(s string) string {
		var line []byte

		line = append(line, term.Escape.Red...)
		line = appendTerminalEscaped(line, []byte(s))
		line = append(line, term.Escape.Reset...)
		return string(line)
	}

	write := func(s string) {
		term.Write([]byte(s))
	}

	var tmpDir string

	showMediaEntries := func(questionNumber int, medias [][]xmpp.Media) {
		if len(medias) == 0 {
			return
		}

		write("The following media blobs have been provided by the server with this question:\n")
		for i, media := range medias {
			for j, rep := range media {
				if j == 0 {
					write(fmt.Sprintf("  %d. ", i+1))
				} else {
					write("     ")
				}
				write(fmt.Sprintf("Data of type %s", formStringForPrinting(rep.MIMEType)))
				if len(rep.URI) > 0 {
					write(fmt.Sprintf(" at %s\n", formStringForPrinting(rep.URI)))
					continue
				}

				var fileExt string
				switch rep.MIMEType {
				case "image/png":
					fileExt = "png"
				case "image/jpeg":
					fileExt = "jpeg"
				}

				if len(tmpDir) == 0 {
					var err error
					if tmpDir, err = ioutil.TempDir("", "xmppclient"); err != nil {
						write(", but failed to create temporary directory in which to save it: " + err.Error() + "\n")
						continue
					}
				}

				filename := filepath.Join(tmpDir, fmt.Sprintf("%d-%d-%d", questionNumber, i, j))
				if len(fileExt) > 0 {
					filename = filename + "." + fileExt
				}
				out, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
				if err != nil {
					write(", but failed to create file in which to save it: " + err.Error() + "\n")
					continue
				}
				out.Write(rep.Data)
				out.Close()

				write(", saved in " + filename + "\n")
			}
		}

		write("\n")
	}

	var err error
	if len(title) > 0 {
		write(fmt.Sprintf("Title: %s\n", formStringForPrinting(title)))
	}
	if len(instructions) > 0 {
		write(fmt.Sprintf("Instructions: %s\n", formStringForPrinting(instructions)))
	}

	questionNumber := 0
	for _, field := range fields {
		questionNumber++
		write("\n")

		switch field := field.(type) {
		case *xmpp.FixedFormField:
			write(formStringForPrinting(field.Text))
			write("\n")
			questionNumber--

		case *xmpp.BooleanFormField:
			write(fmt.Sprintf("%d. %s\n\n", questionNumber, formStringForPrinting(field.Label)))
			showMediaEntries(questionNumber, field.Media)
			term.SetPrompt("Please enter yes, y, no or n: ")

		TryAgain:
//.........这里部分代码省略.........
开发者ID:PMaynard,项目名称:coyim,代码行数:101,代码来源:ui.go

示例4: main

func main() {
	var (
		filename string
		site     string
		username string
		password string
		line     string
		buf      []byte
		db       *keyrack.Database
		oldState *terminal.State
		term     *terminal.Terminal
		termio   TermIO
		group    *keyrack.Group
		//trail     []*keyrack.Group
		groupView GroupView
		matched   bool
		quit      bool
		err       error
	)

	if len(os.Args) != 2 {
		fmt.Printf("Syntax: %s <filename>\n", os.Args[0])
		os.Exit(1)
	}
	filename = os.Args[1]

	// setup terminal
	oldState, err = terminal.MakeRaw(0)
	if err != nil {
		panic(err)
	}
	defer terminal.Restore(0, oldState)

	termio.Input = os.Stdin
	termio.Output = os.Stdout
	term = terminal.NewTerminal(termio, "> ")

	if _, err = os.Stat(filename); os.IsNotExist(err) {
		db, err = keyrack.NewDatabase()
	} else {
		password, err = term.ReadPassword("Password: ")
		if err == nil {
			db, err = keyrack.LoadDatabase(filename, []byte(password))
		}
		password = ""
	}

	group = db.Top()
	for err == nil && !quit {
		groupView.Group = group
		buf, err = groupView.Render()
		if err != nil {
			break
		}

		_, err = term.Write(buf)
		if err != nil {
			break
		}

		line, err = term.ReadLine()
		if err != nil {
			break
		}

		switch line {
		case "q":
			password, err = term.ReadPassword("Password: ")
			if err == nil {
				err = db.Save(filename, []byte(password))
			}
			password = ""
			quit = true
		case "ng", "new group":
			term.SetPrompt("Group name: ")
			line, err = term.ReadLine()
			if err != nil {
				break
			}

			if line == "" {
				term.Write([]byte("Group creation cancelled.\n"))
			} else {
				err = group.AddGroup(line)
				if err != nil {
					if err == keyrack.ErrGroupExists {
						term.Write([]byte("Group already exists.\n"))
					} else {
						break
					}
				}
			}
			term.SetPrompt("> ")
		case "nl", "new login":
			term.SetPrompt("Site name: ")
			site, err = term.ReadLine()
			if err != nil {
				break
			}

//.........这里部分代码省略.........
开发者ID:viking,项目名称:go-keyrack,代码行数:101,代码来源:main.go


注:本文中的golang.org/x/crypto/ssh/terminal.Terminal.Write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。