Ejemplo n.º 1
0
// Write implements a io.Writer that expects messages from users.
func (h *Hal) Write(cmd []byte) (n int, err error) {
	l := len(cmd)

	cmd = bytes.Replace(cmd, []byte("<"), []byte(""), -1)
	cmd = bytes.Replace(cmd, []byte(">"), []byte(""), -1)

	chunks := bytes.Split(cmd, space)

	if len(chunks) < 1 {
		h.out.Write([]byte("Say what?!"))
		return l, errMissingCommand
	}

	s := string(chunks[0])

	switch s {
	case "help":
		h.out.Write([]byte(`set-repo [repo-url]`))
		h.out.Write([]byte(`[network] [target]`))
		return l, nil
	case "wipe":
		h.reset()
		h.out.Write([]byte(`Now I don't have a memory.`))
		return l, nil
	case "set-repo":
		if len(chunks) > 1 {
			repo := string(chunks[1])
			if repo != "" {
				// TODO: check this is an actual repo.
				h.repo = repo
				h.save()
				h.out.Write([]byte(fmt.Sprintf("You current repo is %q", h.repo)))
				return l, nil
			}
		}
		h.out.Write([]byte(fmt.Sprintf("Try `set-repo [repo-url]`")))
		return l, errMissingCommand
	default:
		if h.repo != "" {

			h.out.Write([]byte(fmt.Sprintf("Hang in there, I'm cloning %q...", h.repo)))

			// TODO: grab branch name from URL, if any.
			repo, err := git.Clone(h.repo)
			if err != nil {
				h.out.Write([]byte(fmt.Sprintf("I'm sorry Dave, %v", err)))
				h.out.Write([]byte("Clone failed."))
				return l, err
			}

			if err := repo.Checkout("master"); err != nil {
				h.out.Write([]byte(fmt.Sprintf("I'm sorry Dave, %v", err)))
				h.out.Write([]byte("Check out failed."))
				return l, err
			}

			h.out.Write([]byte(fmt.Sprintf("Running sup...")))

			var outbuf bytes.Buffer
			// TODO: check error
			cmd, _ := sup.New(&outbuf, repo.Dir())
			defer func() {
				log.Printf("Cleaning %v", repo.Dir())
				os.RemoveAll(repo.Dir())
			}()

			if len(chunks) > 0 {
				cmd.SetNetwork(string(chunks[0]))
			}
			if len(chunks) > 1 {
				cmd.SetTarget(string(chunks[1]))
			}

			err = cmd.Exec()
			if err != nil {
				h.out.Write([]byte(fmt.Sprintf("I'm sorry Dave, %v", err)))
				return l, err
			}

			h.out.Write(outbuf.Bytes())
			return l, nil
		}
		h.out.Write([]byte(fmt.Sprintf("Missing repo, try `set-repo [repo-url]`")))
		return l, errMissingCommand
	}

	// Catch-all, this should never run
	return l, errUnexpectedIssue
}
Ejemplo n.º 2
0
func (h *Hal) Write(cmd []byte) (n int, err error) {
	l := len(cmd)

	cmd = bytes.Replace(cmd, []byte("<"), []byte(""), -1)
	cmd = bytes.Replace(cmd, []byte(">"), []byte(""), -1)

	chunks := bytes.Split(cmd, space)

	if len(chunks) < 1 {
		h.out.Write([]byte(msgMissingCommand))
		return l, errMissingCommand
	}

	s := string(chunks[0])

	switch s {
	case "help":
		h.out.Write([]byte(`[repository]/[branch] [network] [target]`))
		return l, nil
	case "wipe":
		h.reset()
		h.out.Write([]byte(`Now I don't have a memory.`))
		return l, nil
	default:
		if len(chunks) > 0 {
			switch string(chunks[0]) {
			case "set-repo":
				if len(chunks) > 1 {
					repo := string(chunks[1])
					if repo != "" {
						// TODO: check this is an actual repo.
						h.repo = repo
						h.save()
						h.out.Write([]byte(fmt.Sprintf("You current repo is %q", h.repo)))
						return l, nil
					}
				}
				h.out.Write([]byte(fmt.Sprintf("Try `set-repo [repo-url]`")))
				return l, errMissingCommand
			}
			if h.repo != "" {

				h.out.Write([]byte(fmt.Sprintf("Hang in there, I'm cloning %q...", h.repo)))

				// TODO: grab branch name from URL, if any.
				repo, err := git.Clone(h.repo)
				if err != nil {
					return l, err
				}

				if err := repo.Checkout("master"); err != nil {
					return l, err
				}

				h.out.Write([]byte(fmt.Sprintf("Running sup...")))

				// TODO: insert sup magic here.
				var outbuf bytes.Buffer
				cmd := sup.NewSup(&outbuf).Setwd(repo.Dir())
				defer func() {
					log.Printf("Cleaning %v", repo.Dir())
					os.RemoveAll(repo.Dir())
				}()

				if len(chunks) > 0 {
					cmd.Network(string(chunks[0]))
				}
				if len(chunks) > 1 {
					cmd.Target(string(chunks[1]))
				}

				err = cmd.Exec()

				h.out.Write(outbuf.Bytes())
				return l, err
			} else {
				h.out.Write([]byte(fmt.Sprintf("Missing repo, try `set-repo [repo-url]`")))
			}
			return l, errMissingCommand
		}
	}

	return l, nil
}