aboutsummaryrefslogtreecommitdiff
path: root/completions/zsh/_rest-run
blob: 0ed79ffbe1dc01fa1f0fd8bd9c98a381829c0c0e (plain)
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
#compdef _rest-run rest-run

# zsh completion for rest-run                             -*- shell-script -*-

function __debug {
  echo "$1" >> /tmp/_rest-run.log
}

function _rest-run_read_domain () {
  local ARG="${words[2]}"

  if [ ${#ARG} -lt 7 ]; then
    ARG="${words[3]}"
  fi

  echo "$ARG" | sed 's/[a-z]*:\/\///g' | sed 's/\/.*//g' | sed 's/\?.*//g'
}

function _rest-run {
  local line

  _arguments -C \
    '1:*:(POST GET PUT PATCH DELETE)' \
    '2:*:_rest-run_url_opts' \
    '*-h[HTTP header]' \
    + '(editopts)' {-ne,--no-edit}'[Skip request body edit]' \
    + '(paginateopts)' {-p,--paginate}'[Paginate request response]' \
    + '(coloropts)' \
      '--no-color[Disable ANSI color scape codes (default when output is being piped)]' \
      '--color[Enable ANSI color scape codes]' \
    + '(extractopts)' {-e,--extract}'[Extract request data]: :_rest-run_extract_opts' \
    + '(bodyresponseopts)' \
      {-b,--body}'[Body options]: :_rest-run_body_opts' \
      {-r,--response}'[Response options]: :_rest-run_response_opts' \
      {-rr,--read-response}'[Read response file]: :_rest-run_select_response_opts'
}

function _rest-run_url_opts {
  local -a _values

  _values=()

  tail -n -50 $HOME/.cache/rest-run/request-history 2> /dev/null | while read -r line; do
    _values+=("$(echo $line | sed 's/^[A-Z]*\s//g')")
  done

  compadd -a _values
}

function _rest-run_body_opts {
  local -a _descriptions _values

  _descriptions=(
    'default -- create a request body using the default editor'
    'last    -- use the last request'
    'history -- pick from the request history'
    'stdin   -- reads from piped input'
  )

  _values=(
    'default'
    'last'
    'history'
    'stdin'
  )

  compadd -d _descriptions -a _values
}

function _rest-run_response_opts {
  local -a _descriptions _values

  _descriptions=(
    'default -- store the request on cache'
    'last    -- read last request, skip request'
    'history -- pick from the response history, skip request'
  )

  _values=(
    'default'
    'last'
    'history'
  )

  compadd -d _descriptions -a _values
}

function _rest-run_extract_opts {
  local -a _descriptions _values

  _descriptions=(
    'default  -- whole request and response, headers and body'
    'body     -- only response body (default when output is piped to another process)'
    'headers  -- only response headers'
    'rbody    -- only request body'
    'rheaders -- only request headers'
  )

  _values=(
    'default'
    'body'
    'headers'
    'rbody'
    'rheaders'
  )

  compadd -d _descriptions -a _values
}

function _rest-run_select_response_opts {
  local domain="$(_rest-run_read_domain)"
  local -a _values

  for f in $(find $HOME/.cache/rest-run/$domain -type f -iname '*response*.log' 2> /dev/null | head -50); do
    _values+=("$(echo "$f" | sed 's/^.*\/\.cache\/rest-run\///g')")
  done

  compadd -a _values
}

if [ "$funcstack[1]" = "_rest-run" ]; then
  _rest-run
fi

compdef _rest-run rest-run