aboutsummaryrefslogtreecommitdiff
path: root/cp.go
blob: f2ee2b17eb749becf02ed08444e99454ad797863 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main

import (
	"context"
	"fmt"
	"os"
	"path"
	"path/filepath"
	"strings"

	"github.com/spf13/cobra"

	"git.onurgunduz.com/colabctl/internal/auth"
	"git.onurgunduz.com/colabctl/internal/colab"
)

// remoteSpec is a parsed copy endpoint. When isRemote is true the location
// lives on a runtime; endpoint may be empty to mean "the only active runtime".
type remoteSpec struct {
	isRemote bool
	endpoint string
	path     string
}

// parseSpec splits an scp-style argument. An argument is treated as remote when
// it contains a colon that comes before any slash, mirroring scp. The part
// before the colon is the runtime endpoint, or empty for the active runtime.
func parseSpec(arg string) remoteSpec {
	idx := strings.IndexByte(arg, ':')
	if idx < 0 || strings.IndexByte(arg[:idx], '/') >= 0 {
		return remoteSpec{isRemote: false, path: arg}
	}
	return remoteSpec{isRemote: true, endpoint: arg[:idx], path: arg[idx+1:]}
}

var cpCmd = &cobra.Command{
	Use:   "cp <src> <dst>",
	Short: "Copy files between your machine and a Colab runtime (scp-style)",
	Long: `Copy a file to or from an active Colab runtime, using scp-style paths.

A remote location is written as endpoint:path. Leave the endpoint empty to use
the only active runtime:

  # Download a trained model from the active runtime.
  colabctl cp :/content/model.pt ./model.pt

  # Upload a training script to a specific runtime.
  colabctl cp train.py gpu-t4-s-abc123:/content/train.py

Get the endpoint from 'colabctl sessions'. Directories are not copied directly;
archive them on the runtime first (tar) and copy the archive.`,
	Args: cobra.ExactArgs(2),
	RunE: func(cmd *cobra.Command, args []string) error {
		requireCredentials()
		ctx := cmd.Context()

		src := parseSpec(args[0])
		dst := parseSpec(args[1])

		if src.isRemote == dst.isRemote {
			if src.isRemote {
				return fmt.Errorf("copying between two runtimes is not supported")
			}
			return fmt.Errorf("at least one path must be remote (endpoint:path); use cp(1) for local copies")
		}

		httpClient, err := auth.Client(ctx, clientID, clientSecret)
		if err != nil {
			return err
		}
		client := colab.New(httpClient)

		if src.isRemote {
			return download(ctx, client, src, dst.path)
		}
		return upload(ctx, client, src.path, dst)
	},
}

// resolveRuntime finds an active runtime. With an explicit endpoint it looks
// that one up; otherwise it requires exactly one active runtime.
func resolveRuntime(ctx context.Context, client *colab.Client, endpoint string) (*colab.AssignmentInfo, error) {
	if endpoint != "" {
		return client.AssignmentFromEndpoint(ctx, endpoint)
	}

	runtimes, err := client.ListAssignments(ctx)
	if err != nil {
		return nil, err
	}
	switch len(runtimes) {
	case 0:
		return nil, fmt.Errorf("no active runtimes: start one with 'colabctl exec', or name one as endpoint:path")
	case 1:
		return client.AssignmentFromEndpoint(ctx, runtimes[0].Endpoint)
	default:
		var b strings.Builder
		fmt.Fprintf(&b, "%d active runtimes: name one in the path as endpoint:path:\n", len(runtimes))
		for _, r := range runtimes {
			fmt.Fprintf(&b, "  %s\n", r.Endpoint)
		}
		return nil, fmt.Errorf("%s", b.String())
	}
}

func download(ctx context.Context, client *colab.Client, src remoteSpec, localPath string) error {
	assignment, err := resolveRuntime(ctx, client, src.endpoint)
	if err != nil {
		return err
	}

	fc, err := client.FetchFile(ctx, assignment, src.path)
	if err != nil {
		return err
	}

	// If the local target is a directory (or ends with a separator), keep the
	// remote file name inside it.
	target := localPath
	if target == "" || strings.HasSuffix(target, string(os.PathSeparator)) {
		target = filepath.Join(target, fc.Name)
	} else if fi, statErr := os.Stat(target); statErr == nil && fi.IsDir() {
		target = filepath.Join(target, fc.Name)
	}

	if dir := filepath.Dir(target); dir != "" {
		if err := os.MkdirAll(dir, 0o755); err != nil {
			return err
		}
	}
	if err := os.WriteFile(target, fc.Data, 0o644); err != nil {
		return err
	}
	fmt.Fprintf(os.Stderr, "Downloaded %s (%d bytes) -> %s\n", src.path, len(fc.Data), target)
	return nil
}

func upload(ctx context.Context, client *colab.Client, localPath string, dst remoteSpec) error {
	data, err := os.ReadFile(localPath)
	if err != nil {
		return err
	}

	assignment, err := resolveRuntime(ctx, client, dst.endpoint)
	if err != nil {
		return err
	}

	// If the remote target names a directory, keep the local file name.
	remotePath := dst.path
	if remotePath == "" || strings.HasSuffix(remotePath, "/") {
		remotePath = path.Join(remotePath, filepath.Base(localPath))
	}

	if err := client.PutFile(ctx, assignment, remotePath, data); err != nil {
		return err
	}
	fmt.Fprintf(os.Stderr, "Uploaded %s (%d bytes) -> %s\n", localPath, len(data), remotePath)
	return nil
}

func init() {
	rootCmd.AddCommand(cpCmd)
}