Пример #1
0
func do(appEnvObj interface{}) error {
	appEnv := appEnvObj.(*appEnv)

	clientConn, err := grpc.Dial(appEnv.Address)
	if err != nil {
		return err
	}
	apiClient := pps.NewApiClient(clientConn)

	var protoFlag bool

	inspectCmd := cobramainutil.Command{
		Use:        "inspect github.com/user/repository [path/to/specDir]",
		Long:       "Inspect a pipeline specification.",
		MinNumArgs: 1,
		MaxNumArgs: 2,
		Run: func(cmd *cobra.Command, args []string) error {
			pipelineArgs, err := getPipelineArgs(args)
			if err != nil {
				return err
			}
			getPipelineResponse, err := ppsutil.GetPipelineGithub(
				apiClient,
				pipelineArgs.contextDir,
				pipelineArgs.user,
				pipelineArgs.repository,
				pipelineArgs.branch,
				pipelineArgs.accessToken,
			)
			if err != nil {
				return err
			}
			if protoFlag {
				fmt.Printf("%v\n", getPipelineResponse.Pipeline)
			} else {
				data, err := json.MarshalIndent(getPipelineResponse.Pipeline, "", "\t ")
				if err != nil {
					return err
				}
				fmt.Println(string(data))
			}
			return nil
		},
	}.ToCobraCommand()
	inspectCmd.Flags().BoolVar(&protoFlag, "proto", false, "Print in proto format instead of JSON.")

	startCmd := cobramainutil.Command{
		Use:        "start github.com/user/repository [path/to/specDir]",
		Long:       "Start a pipeline specification run.",
		MinNumArgs: 1,
		MaxNumArgs: 2,
		Run: func(cmd *cobra.Command, args []string) error {
			pipelineArgs, err := getPipelineArgs(args)
			if err != nil {
				return err
			}
			startPipelineRunResponse, err := ppsutil.StartPipelineRunGithub(
				apiClient,
				pipelineArgs.contextDir,
				pipelineArgs.user,
				pipelineArgs.repository,
				pipelineArgs.branch,
				pipelineArgs.accessToken,
			)
			if err != nil {
				return err
			}
			fmt.Println(startPipelineRunResponse.PipelineRunId)
			return nil
		},
	}.ToCobraCommand()

	statusCmd := cobramainutil.Command{
		Use:     "status pipelineRunID",
		Long:    "Get the status of a pipeline run.",
		NumArgs: 1,
		Run: func(cmd *cobra.Command, args []string) error {
			getPipelineRunStatusResponse, err := ppsutil.GetPipelineRunStatus(
				apiClient,
				args[0],
			)
			if err != nil {
				return err
			}
			name, ok := pps.PipelineRunStatusType_name[int32(getPipelineRunStatusResponse.PipelineRunStatus.PipelineRunStatusType)]
			if !ok {
				return fmt.Errorf("unknown run status")
			}
			fmt.Printf("%s %s\n", args[0], strings.Replace(name, "PIPELINE_RUN_STATUS_TYPE_", "", -1))
			return nil
		},
	}.ToCobraCommand()

	logsCmd := cobramainutil.Command{
		Use:        "logs pipelineRunID [node]",
		Long:       "Get the logs from a pipeline run.",
		MinNumArgs: 1,
		MaxNumArgs: 2,
		Run: func(cmd *cobra.Command, args []string) error {
			node := ""
			if len(args) == 2 {
				node = args[1]
			}
			getPipelineRunLogsResponse, err := ppsutil.GetPipelineRunLogs(
				apiClient,
				args[0],
				node,
			)
			if err != nil {
				return err
			}
			for _, pipelineRunLog := range getPipelineRunLogsResponse.PipelineRunLog {
				name, ok := pps.OutputStream_name[int32(pipelineRunLog.OutputStream)]
				if !ok {
					return fmt.Errorf("unknown pps.OutputStream")
				}
				name = strings.Replace(name, "OUTPUT_STREAM_", "", -1)
				containerID := pipelineRunLog.ContainerId
				if len(containerID) > 8 {
					containerID = containerID[:8]
				}
				logInfo := &logInfo{
					Node:         pipelineRunLog.Node,
					ContainerID:  containerID,
					OutputStream: name,
					Time:         protoutil.TimestampToTime(pipelineRunLog.Timestamp),
				}
				logInfoData, err := json.Marshal(logInfo)
				if err != nil {
					return err
				}
				s := fmt.Sprintf("%s %s", string(logInfoData), string(pipelineRunLog.Data))
				fmt.Println(strings.TrimSpace(s))
			}
			return nil
		},
	}.ToCobraCommand()

	rootCmd := &cobra.Command{
		Use: "pps",
		Long: `Access the PPS API.

Note that this CLI is experimental and does not even check for common errors.
The environment variable PPS_ADDRESS controls what server the CLI connects to, the default is 0.0.0.0:651.`,
	}
	rootCmd.AddCommand(cobramainutil.NewVersionCommand(clientConn, pachyderm.Version))
	rootCmd.AddCommand(inspectCmd)
	rootCmd.AddCommand(startCmd)
	rootCmd.AddCommand(statusCmd)
	rootCmd.AddCommand(logsCmd)
	return rootCmd.Execute()
}
Пример #2
0
func do(appEnvObj interface{}) error {
	appEnv := appEnvObj.(*appEnv)

	clientConn, err := grpc.Dial(appEnv.Address)
	if err != nil {
		return err
	}
	apiClient := pfs.NewApiClient(clientConn)

	var shard int
	var modulus int

	initCmd := cobramainutil.Command{
		Use:     "init repository-name",
		Long:    "Initalize a repository.",
		NumArgs: 1,
		Run: func(cmd *cobra.Command, args []string) error {
			return pfsutil.InitRepository(apiClient, args[0], false)
		},
	}.ToCobraCommand()

	mkdirCmd := cobramainutil.Command{
		Use:     "mkdir repository-name commit-id path/to/dir",
		Long:    "Make a directory. Sub directories must already exist.",
		NumArgs: 3,
		Run: func(cmd *cobra.Command, args []string) error {
			return pfsutil.MakeDirectory(apiClient, args[0], args[1], args[2])
		},
	}.ToCobraCommand()

	putCmd := cobramainutil.Command{
		Use:     "put repository-name branch-id path/to/file",
		Long:    "Put a file from stdin. Directories must exist. branch-id must be a writeable commit.",
		NumArgs: 3,
		Run: func(cmd *cobra.Command, args []string) error {
			_, err := pfsutil.PutFile(apiClient, args[0], args[1], args[2], 0, os.Stdin)
			return err
		},
	}.ToCobraCommand()

	getCmd := cobramainutil.Command{
		Use:     "get repository-name commit-id path/to/file",
		Long:    "Get a file from stdout. commit-id must be a readable commit.",
		NumArgs: 3,
		Run: func(cmd *cobra.Command, args []string) error {
			return pfsutil.GetFile(apiClient, args[0], args[1], args[2], 0, pfsutil.GetAll, os.Stdout)
		},
	}.ToCobraCommand()

	lsCmd := cobramainutil.Command{
		Use:     "ls repository-name branch-id path/to/dir",
		Long:    "List a directory. Directory must exist.",
		NumArgs: 3,
		Run: func(cmd *cobra.Command, args []string) error {
			listFilesResponse, err := pfsutil.ListFiles(apiClient, args[0], args[1], args[2], uint64(shard), uint64(modulus))
			if err != nil {
				return err
			}
			for _, fileInfo := range listFilesResponse.FileInfo {
				fmt.Printf("%+v\n", fileInfo)
			}
			return nil
		},
	}.ToCobraCommand()
	lsCmd.Flags().IntVarP(&shard, "shard", "s", 0, "shard to read from")
	lsCmd.Flags().IntVarP(&modulus, "modulus", "m", 1, "modulus of the shards")

	branchCmd := cobramainutil.Command{
		Use:     "branch repository-name commit-id",
		Long:    "Branch a commit. commit-id must be a readable commit.",
		NumArgs: 2,
		Run: func(cmd *cobra.Command, args []string) error {
			branchResponse, err := pfsutil.Branch(apiClient, args[0], args[1])
			if err != nil {
				return err
			}
			fmt.Println(branchResponse.Commit.Id)
			return nil
		},
	}.ToCobraCommand()

	commitCmd := cobramainutil.Command{
		Use:     "commit repository-name branch-id",
		Long:    "Commit a branch. branch-id must be a writeable commit.",
		NumArgs: 2,
		Run: func(cmd *cobra.Command, args []string) error {
			return pfsutil.Commit(apiClient, args[0], args[1])
		},
	}.ToCobraCommand()

	commitInfoCmd := cobramainutil.Command{
		Use:     "commit-info repository-name commit-id",
		Long:    "Get info for a commit.",
		NumArgs: 2,
		Run: func(cmd *cobra.Command, args []string) error {
			commitInfoResponse, err := pfsutil.GetCommitInfo(apiClient, args[0], args[1])
			if err != nil {
				return err
			}
			fmt.Printf("%+v\n", commitInfoResponse.CommitInfo)
			return nil
		},
	}.ToCobraCommand()

	listCommitsCmd := cobramainutil.Command{
		Use:     "list-commits repository-name",
		Long:    "List commits on the repository.",
		NumArgs: 1,
		Run: func(cmd *cobra.Command, args []string) error {
			listCommitsResponse, err := pfsutil.ListCommits(apiClient, args[0])
			if err != nil {
				return err
			}
			for _, commitInfo := range listCommitsResponse.CommitInfo {
				fmt.Printf("%+v\n", commitInfo)
			}
			return nil
		},
	}.ToCobraCommand()

	mountCmd := cobramainutil.Command{
		Use:     "mount repository-name",
		Long:    "Mount a repository as a local file system.",
		NumArgs: 1,
		Run: func(cmd *cobra.Command, args []string) error {
			return fuse.NewMounter().Mount(apiClient, args[0], args[0], uint64(shard), uint64(modulus))
		},
	}.ToCobraCommand()
	mountCmd.Flags().IntVarP(&shard, "shard", "s", 0, "shard to read from")
	mountCmd.Flags().IntVarP(&modulus, "modulus", "m", 1, "modulus of the shards")

	rootCmd := &cobra.Command{
		Use: "pfs",
		Long: `Access the PFS API.

Note that this CLI is experimental and does not even check for common errors.
The environment variable PFS_ADDRESS controls what server the CLI connects to, the default is 0.0.0.0:650.`,
	}

	rootCmd.AddCommand(cobramainutil.NewVersionCommand(clientConn, pachyderm.Version))
	rootCmd.AddCommand(initCmd)
	rootCmd.AddCommand(mkdirCmd)
	rootCmd.AddCommand(putCmd)
	rootCmd.AddCommand(getCmd)
	rootCmd.AddCommand(lsCmd)
	rootCmd.AddCommand(branchCmd)
	rootCmd.AddCommand(commitCmd)
	rootCmd.AddCommand(commitInfoCmd)
	rootCmd.AddCommand(listCommitsCmd)
	rootCmd.AddCommand(mountCmd)
	return rootCmd.Execute()
}