Example #1
0
func streamBuild(app, build string) error {
	host, password, err := currentLogin()

	if err != nil {
		stdcli.Error(err)
		return err
	}

	origin := fmt.Sprintf("https://%s", host)
	url := fmt.Sprintf("wss://%s/apps/%s/builds/%s/logs", host, app, build)

	config, err := websocket.NewConfig(url, origin)

	if err != nil {
		stdcli.Error(err)
		return err
	}

	userpass := fmt.Sprintf("convox:%s", password)
	userpass_encoded := base64.StdEncoding.EncodeToString([]byte(userpass))

	config.Header.Add("Authorization", fmt.Sprintf("Basic %s", userpass_encoded))

	config.TlsConfig = &tls.Config{
		InsecureSkipVerify: true,
	}

	ws, err := websocket.DialConfig(config)

	if err != nil {
		stdcli.Error(err)
		return err
	}

	defer ws.Close()

	var message []byte

	for {
		err := websocket.Message.Receive(ws, &message)

		if err != nil {
			fmt.Fprintf(os.Stderr, "ws %s, retrying...\n", err.Error())
			streamBuild(app, build)
		}

		fmt.Print(string(message))
	}

	return nil
}
Example #2
0
func cmdLogsStream(c *cli.Context) {
	for {
		_, app, err := stdcli.DirApp(c, ".")

		if err != nil {
			stdcli.Error(err)
			return
		}

		host, password, err := currentLogin()

		if err != nil {
			stdcli.Error(err)
			return
		}

		origin := fmt.Sprintf("https://%s", host)
		url := fmt.Sprintf("wss://%s/apps/%s/logs/stream", host, app)

		config, err := websocket.NewConfig(url, origin)

		if err != nil {
			stdcli.Error(err)
			return
		}

		userpass := fmt.Sprintf("convox:%s", password)
		userpass_encoded := b64.StdEncoding.EncodeToString([]byte(userpass))

		config.Header.Add("Authorization", fmt.Sprintf("Basic %s", userpass_encoded))

		config.TlsConfig = &tls.Config{
			InsecureSkipVerify: true,
		}

		ws, err := websocket.DialConfig(config)

		if err != nil {
			stdcli.Error(err)
			return
		}

		defer ws.Close()

		var message []byte

		for {
			err := websocket.Message.Receive(ws, &message)

			if err == io.EOF {
				return
			}

			if err != nil {
				break
			}

			fmt.Print(string(message))
		}

		fmt.Println("out")
	}
}
Example #3
0
File: run.go Project: noqcks/cli
func cmdRunAttached(c *cli.Context) {
	fd := os.Stdin.Fd()
	oldState, err := term.SetRawTerminal(fd)
	if err != nil {
		stdcli.Error(err)
		return
	}
	defer term.RestoreTerminal(fd, oldState)

	_, app, err := stdcli.DirApp(c, ".")

	if err != nil {
		stdcli.Error(err)
		return
	}

	if len(c.Args()) < 2 {
		stdcli.Usage(c, "run")
		return
	}

	ps := c.Args()[0]

	host, password, err := currentLogin()

	if err != nil {
		stdcli.Error(err)
		return
	}

	origin := fmt.Sprintf("https://%s", host)
	url := fmt.Sprintf("wss://%s/apps/%s/processes/%s/run", host, app, ps)

	config, err := websocket.NewConfig(url, origin)

	if err != nil {
		stdcli.Error(err)
		return
	}

	command := strings.Join(c.Args()[1:], " ")

	config.Header.Set("Command", command)

	userpass := fmt.Sprintf("convox:%s", password)
	userpass_encoded := b64.StdEncoding.EncodeToString([]byte(userpass))

	config.Header.Add("Authorization", fmt.Sprintf("Basic %s", userpass_encoded))

	config.TlsConfig = &tls.Config{
		InsecureSkipVerify: true,
	}

	ws, err := websocket.DialConfig(config)

	if err != nil {
		stdcli.Error(err)
		return
	}

	defer ws.Close()

	ch := make(chan int)
	go io.Copy(ws, os.Stdin)
	go messageReceive(ws, os.Stdout, ch)

	code := <-ch

	term.RestoreTerminal(os.Stdin.Fd(), oldState)
	os.Exit(code)
}