|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/qubesome/cli/internal/clipboard" |
| 8 | + "github.com/qubesome/cli/internal/command" |
| 9 | + "github.com/urfave/cli/v3" |
| 10 | +) |
| 11 | + |
| 12 | +func clipboardCommand() *cli.Command { |
| 13 | + clipType := &cli.StringFlag{ |
| 14 | + Name: "type", |
| 15 | + Aliases: []string{"t"}, |
| 16 | + Validator: func(s string) error { |
| 17 | + switch s { |
| 18 | + case "image/png": |
| 19 | + return nil |
| 20 | + } |
| 21 | + return fmt.Errorf("unsupported type %q", s) |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + cmd := &cli.Command{ |
| 26 | + Name: "clipboard", |
| 27 | + Aliases: []string{"clip"}, |
| 28 | + Usage: "enable sharing of clipboard across profiles and the host", |
| 29 | + Commands: []*cli.Command{ |
| 30 | + { |
| 31 | + Name: "from-host", |
| 32 | + Usage: "copies the clipboard contents from the host to a profile", |
| 33 | + Arguments: []cli.Argument{ |
| 34 | + &cli.StringArg{ |
| 35 | + Name: "target_profile", |
| 36 | + Min: 1, |
| 37 | + Max: 1, |
| 38 | + Destination: &targetProfile, |
| 39 | + }, |
| 40 | + }, |
| 41 | + Flags: []cli.Flag{ |
| 42 | + clipType, |
| 43 | + }, |
| 44 | + Action: func(ctx context.Context, c *cli.Command) error { |
| 45 | + cfg := profileConfigOrDefault(targetProfile) |
| 46 | + |
| 47 | + target, ok := cfg.Profiles[targetProfile] |
| 48 | + if !ok { |
| 49 | + return fmt.Errorf("no active profile %q found", targetProfile) |
| 50 | + } |
| 51 | + |
| 52 | + opts := []command.Option[clipboard.Options]{ |
| 53 | + clipboard.WithFromHost(), |
| 54 | + clipboard.WithTargetProfile(target), |
| 55 | + } |
| 56 | + |
| 57 | + if typ := c.String("type"); typ != "" { |
| 58 | + fmt.Println(typ) |
| 59 | + opts = append(opts, clipboard.WithContentType(typ)) |
| 60 | + } |
| 61 | + |
| 62 | + return clipboard.Run( |
| 63 | + opts..., |
| 64 | + ) |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + Name: "from-profile", |
| 69 | + Usage: "copies the clipboard contents between profiles", |
| 70 | + Arguments: []cli.Argument{ |
| 71 | + &cli.StringArg{ |
| 72 | + Name: "source_profile", |
| 73 | + Min: 1, |
| 74 | + Max: 1, |
| 75 | + Destination: &sourceProfile, |
| 76 | + }, |
| 77 | + &cli.StringArg{ |
| 78 | + Name: "target_profile", |
| 79 | + Min: 1, |
| 80 | + Max: 1, |
| 81 | + Destination: &targetProfile, |
| 82 | + }, |
| 83 | + }, |
| 84 | + Flags: []cli.Flag{ |
| 85 | + clipType, |
| 86 | + }, |
| 87 | + Action: func(ctx context.Context, c *cli.Command) error { |
| 88 | + cfg := profileConfigOrDefault(targetProfile) |
| 89 | + |
| 90 | + source, ok := cfg.Profiles[sourceProfile] |
| 91 | + if !ok { |
| 92 | + return fmt.Errorf("no active profile %q found", sourceProfile) |
| 93 | + } |
| 94 | + |
| 95 | + target, ok := cfg.Profiles[targetProfile] |
| 96 | + if !ok { |
| 97 | + return fmt.Errorf("no active profile %q found", targetProfile) |
| 98 | + } |
| 99 | + |
| 100 | + opts := []command.Option[clipboard.Options]{ |
| 101 | + clipboard.WithSourceProfile(source), |
| 102 | + clipboard.WithTargetProfile(target), |
| 103 | + } |
| 104 | + |
| 105 | + if typ := c.String("type"); typ != "" { |
| 106 | + fmt.Println(typ) |
| 107 | + opts = append(opts, clipboard.WithContentType(typ)) |
| 108 | + } |
| 109 | + |
| 110 | + return clipboard.Run( |
| 111 | + opts..., |
| 112 | + ) |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + Name: "to-host", |
| 117 | + Usage: "copies the clipboard contents from a profile to the host", |
| 118 | + Arguments: []cli.Argument{ |
| 119 | + &cli.StringArg{ |
| 120 | + Name: "source_profile", |
| 121 | + Min: 1, |
| 122 | + Max: 1, |
| 123 | + Destination: &sourceProfile, |
| 124 | + }, |
| 125 | + }, |
| 126 | + Flags: []cli.Flag{ |
| 127 | + clipType, |
| 128 | + }, |
| 129 | + Action: func(ctx context.Context, c *cli.Command) error { |
| 130 | + cfg := profileConfigOrDefault(sourceProfile) |
| 131 | + |
| 132 | + target, ok := cfg.Profiles[sourceProfile] |
| 133 | + if !ok { |
| 134 | + return fmt.Errorf("no active profile %q found", sourceProfile) |
| 135 | + } |
| 136 | + |
| 137 | + opts := []command.Option[clipboard.Options]{ |
| 138 | + clipboard.WithSourceProfile(target), |
| 139 | + clipboard.WithTargetHost(), |
| 140 | + } |
| 141 | + |
| 142 | + if typ := c.String("type"); typ != "" { |
| 143 | + fmt.Println(typ) |
| 144 | + opts = append(opts, clipboard.WithContentType(typ)) |
| 145 | + } |
| 146 | + |
| 147 | + return clipboard.Run( |
| 148 | + opts..., |
| 149 | + ) |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + return cmd |
| 155 | +} |
0 commit comments