~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to src/workflow/ArcanistPasteWorkflow.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-11-01 23:20:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20141101232006-mvlnp0cil67tsboe
Tags: upstream-0~git20141101/arcanist
Import upstream version 0~git20141101, component arcanist

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Upload a chunk of text to the Paste application, or download one.
 
5
 */
 
6
final class ArcanistPasteWorkflow extends ArcanistWorkflow {
 
7
 
 
8
  private $id;
 
9
  private $language;
 
10
  private $title;
 
11
  private $json;
 
12
 
 
13
  public function getWorkflowName() {
 
14
    return 'paste';
 
15
  }
 
16
 
 
17
  public function getCommandSynopses() {
 
18
    return phutil_console_format(<<<EOTEXT
 
19
      **paste** [--title __title__] [--lang __language__] [--json]
 
20
      **paste** __id__ [--json]
 
21
EOTEXT
 
22
      );
 
23
  }
 
24
 
 
25
  public function getCommandHelp() {
 
26
    return phutil_console_format(<<<EOTEXT
 
27
          Supports: text
 
28
          Share and grab text using the Paste application. To create a paste,
 
29
          use stdin to provide the text:
 
30
 
 
31
            $ cat list_of_ducks.txt | arc paste
 
32
 
 
33
          To retrieve a paste, specify the paste ID:
 
34
 
 
35
            $ arc paste P123
 
36
EOTEXT
 
37
      );
 
38
  }
 
39
 
 
40
  public function getArguments() {
 
41
    return array(
 
42
      'title' => array(
 
43
        'param' => 'title',
 
44
        'help' => 'Title for the paste.',
 
45
      ),
 
46
      'lang' => array(
 
47
        'param' => 'language',
 
48
        'help' => 'Language for syntax highlighting.',
 
49
      ),
 
50
      'json' => array(
 
51
        'help' => 'Output in JSON format.',
 
52
      ),
 
53
      '*' => 'argv',
 
54
    );
 
55
  }
 
56
 
 
57
  public function requiresAuthentication() {
 
58
    return true;
 
59
  }
 
60
 
 
61
  protected function didParseArguments() {
 
62
    $this->language = $this->getArgument('lang');
 
63
    $this->title    = $this->getArgument('title');
 
64
    $this->json     = $this->getArgument('json');
 
65
 
 
66
    $argv = $this->getArgument('argv');
 
67
    if (count($argv) > 1) {
 
68
      throw new ArcanistUsageException('Specify only one paste to retrieve.');
 
69
    } else if (count($argv) == 1) {
 
70
      $id = $argv[0];
 
71
      if (!preg_match('/^P?\d+/', $id)) {
 
72
        throw new ArcanistUsageException('Specify a paste ID, like P123.');
 
73
      }
 
74
      $this->id = (int)ltrim($id, 'P');
 
75
 
 
76
      if ($this->language || $this->title) {
 
77
        throw new ArcanistUsageException(
 
78
          'Use options --lang and --title only when creating pastes.');
 
79
      }
 
80
    }
 
81
  }
 
82
 
 
83
  private function getTitle() {
 
84
    return $this->title;
 
85
  }
 
86
 
 
87
  private function getLanguage() {
 
88
    return $this->language;
 
89
  }
 
90
 
 
91
  private function getJSON() {
 
92
    return $this->json;
 
93
  }
 
94
 
 
95
  public function run() {
 
96
 
 
97
    if ($this->id) {
 
98
      return $this->getPaste();
 
99
    } else {
 
100
      return $this->createPaste();
 
101
    }
 
102
  }
 
103
 
 
104
  private function getPaste() {
 
105
    $conduit = $this->getConduit();
 
106
 
 
107
    $info = $conduit->callMethodSynchronous(
 
108
      'paste.query',
 
109
      array(
 
110
        'ids' => array($this->id),
 
111
      ));
 
112
    $info = head($info);
 
113
 
 
114
    if ($this->getJSON()) {
 
115
      echo json_encode($info)."\n";
 
116
    } else {
 
117
      echo $info['content'];
 
118
      if (!preg_match('/\\n$/', $info['content'])) {
 
119
        // If there's no newline, add one, since it looks stupid otherwise. If
 
120
        // you want byte-for-byte equivalence you can use --json.
 
121
        echo "\n";
 
122
      }
 
123
    }
 
124
 
 
125
    return 0;
 
126
  }
 
127
 
 
128
  private function createPaste() {
 
129
    $conduit = $this->getConduit();
 
130
 
 
131
    // Avoid confusion when people type "arc paste" with nothing else.
 
132
    $this->writeStatusMessage("Reading paste from stdin...\n");
 
133
 
 
134
    $info = $conduit->callMethodSynchronous(
 
135
      'paste.create',
 
136
      array(
 
137
        'content'   => file_get_contents('php://stdin'),
 
138
        'title'     => $this->getTitle(),
 
139
        'language'  => $this->getLanguage(),
 
140
      ));
 
141
 
 
142
    if ($this->getArgument('json')) {
 
143
      echo json_encode($info)."\n";
 
144
    } else {
 
145
      echo $info['objectName'].': '.$info['uri']."\n";
 
146
    }
 
147
 
 
148
    return 0;
 
149
  }
 
150
 
 
151
}