forked from helix-editor/helix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
copilot.fish
114 lines (99 loc) · 3.63 KB
/
copilot.fish
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
# Downloading Copilot lsp
function clone_copilot_lsp
echo "*** Cloning the copilot lsp into /usr/local/bin ***"
set -l url "https://github.com/github/copilot.vim"
set -l folder_name "dist/"
set -l temp_dir (mktemp -d)
if test -d "/usr/local/bin/$folder_name"
echo "Folder '$folder_name' already exists in /usr/local/bin. Delete this to re-install. Aborting."
return 1
end
echo "Cloning repository from $url.."
if not git clone $url $temp_dir
echo "Failed to clone repository."
else if not sudo mv $temp_dir/$folder_name /usr/local/bin/$folder_name
echo "Failed to move $folder_name to /usr/local/bin."
end
echo "Moving $folder_name to /usr/local/bin.."
echo "Cleaning up..."
rm -rf $temp_dir
end
function create_copilot_exe
set -l target_binary "/usr/local/bin/copilot"
echo "*** Creating an executable in /usr/local/bin to call the copilot lsp ***"
if test -x $target_binary
echo "The file '$target_binary' already exists. Delete this to re-install. Aborting."
return
end
echo '#! /usr/bin/env bash' > $target_binary
echo 'node /usr/local/bin/dist/language-server.js' >>$target_binary
sudo chmod +x $target_binary
echo "The executable 'copilot' has been created at $target_binary"
end
# Auth
function get_value
set -l pairs (string split '&' $argv[1])
set -l kv_line (string match -r "$argv[2]=.*" $pairs)
echo (string split "=" $kv_line)[2]
end
function get_device_params
set -l payload '{ "scope": "read:user", "client_id": "Iv1.b507a08c87ecfe98" }'
set -l resp (curl -s -X POST "https://github.com/login/device/code" \
-H "Content-Type: application/json" \
-d $payload)
echo $resp
end
function oath
set -l payload "{\"client_id\": \"Iv1.b507a08c87ecfe98\", \"device_code\": \"$argv[1]\", \"grant_type\": \"urn:ietf:params:oauth:grant-type:device_code\"}"
set -l resp (curl -s -X POST "https://github.com/login/oauth/access_token" \
-H "Content-Type: application/json" \
-d $payload)
echo $resp
end
function auth
echo "*** Creating a github access-key file for copilot in ~/.config/github-copilot/hosts.json ***"
set file_path ~/.config/github-copilot/hosts.json
if test -e $file_path
echo "Copilot access-key file $file_path already exists. Delete this to re-install/re-authenticate .Aborting."
return 1
end
set device_params (get_device_params)
set device_code (get_value $device_params device_code)
set uri (get_value $device_params uri)
set uri (echo $uri | sed 's/%/\\\\x/g')
set user_code (get_value $device_params user_code)
echo -e "Go to $uri and enter the code $user_code"
while true
set resp (oath $device_code)
set access_token (get_value $resp access_token)
set error (get_value $resp error)
if not test -z $access_token
mkdir -p ~/.config/github-copilot
echo "{\"github.com\":{\"oauth_token\":\"$access_token\"}}" > ~/.config/github-copilot/hosts.json
echo "Copilot access_token written to ~/.config/github-copilot/hosts.json"
return 0
else if not test -z error
echo $error
else
echo "Bad response."
return 1
end
sleep 5
end
end
# Main
if not set -q argv[1]
echo "No argument provided. Use --create-lsp or --auth."
exit 1
end
switch $argv[1]
case --create-lsp
clone_copilot_lsp
echo ""
create_copilot_exe
case --auth
auth
case '*'
echo "Unknown option: $argv[1]"
exit 1
end