Path: blob/main/components/gitpod-cli/cmd/gitpodRun.go
2498 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"encoding/base64"8"io"9"os"10"syscall"1112"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"13"github.com/spf13/cobra"14)1516// This command takes a base64 stream, saves it to disk and executes it. We use this in17// agent-smith to upload/run the sentinel.18// We don't want this explanation to end up in the binary.1920const delimiter = "@"2122// gitpodRunCmd represents the gitpodRun command23var gitpodRunCmd = &cobra.Command{24Use: "__gitpod_run [<pathToExecutable>]",25Short: "Used by Gitpod to ensure smooth operation",26Hidden: true,27Args: cobra.MaximumNArgs(1),28RunE: func(cmd *cobra.Command, args []string) error {29// ignore trace30utils.TrackCommandUsageEvent.Command = nil3132var tmpfile *os.File33var err error34if len(args) == 1 {35tmpfile, err = os.OpenFile(args[0], os.O_WRONLY|os.O_CREATE, 0700)36} else {37tmpfile, err = os.CreateTemp("", "gp")38}39if err != nil {40_ = os.Remove(tmpfile.Name())41return err42}4344decoder := base64.NewDecoder(base64.RawStdEncoding, &delimitingReader{os.Stdin, false})45_, err = io.Copy(tmpfile, decoder)46if err != nil {47_ = os.Remove(tmpfile.Name())48return err49}50tmpfile.Close()5152err = os.Chmod(tmpfile.Name(), 0700)53if err != nil {54_ = os.Remove(tmpfile.Name())55return err56}57err = syscall.Exec(tmpfile.Name(), []string{"gpr", "serve"}, []string{})58if err != nil {59_ = os.Remove(tmpfile.Name())60return err61}62return nil63},64}6566type delimitingReader struct {67io.Reader68EOF bool69}7071func (p *delimitingReader) Read(buf []byte) (int, error) {72if p.EOF {73return 0, io.EOF74}7576n, err := p.Reader.Read(buf)77if err != nil {78return n, err79}8081for i := 0; i < n; i++ {82if buf[i] == delimiter[0] {83p.EOF = true84return i, io.EOF85}86}8788return n, err89}9091func init() {92rootCmd.AddCommand(gitpodRunCmd)93}949596