neocities 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eed39701962669f623a0c0c5a765a47f2e3c675a5b6c58ff5783bd745aac1355
4
- data.tar.gz: a92dced052ec57bf28cd0375b236b2fddd8f21160b13f99cb9c6f53b3b5326f6
3
+ metadata.gz: c97aed183932a987e86e2fd59f8001620c800007f3365552642d8a43d8a24a6d
4
+ data.tar.gz: 2eeba231d12c9a8d87db79b7324141b3439c6b67d95b8257cba15fece01b3d1f
5
5
  SHA512:
6
- metadata.gz: 68cdc534f9d0a35f4c0c1fe41abb7110e69a282673b908cf5757c11e803a3f431d6ecf7dd58d551a66637b27b503c3b8a6dd96d8e76dadbe6cf9b774e216c4b4
7
- data.tar.gz: 44aa22f8c174a79b0e5b2caaa8da8cab2be56777610386c55da61456b5434bf3a6f9e016a84e35a1c7c559733d93537f142614e649c9cf6f6fad07e57f03a83c
6
+ metadata.gz: 3362281d36af925b2580a1b9c7dbc2ed1fbdef24287f9315c66bc3a45096388695a19a540d3a981d43199e3f427e6a30d1765372e34776cc836829d6ac038654
7
+ data.tar.gz: a3a969acf5a81ca16d3ae59184a361088831ce5d666a7a70f17a7219dbaaedfb5a5ab6d5fc1365f7516ddbc24bd136b43d7311dbdfbd9e3ba5c5a00a1cc58a16
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+ gem 'minitest', '>= 5', '< 7'
data/README.md CHANGED
@@ -22,6 +22,14 @@ Install ruby with [RubyInstaller](https://rubyinstaller.org/). Then you can inst
22
22
 
23
23
  After that, you are all set! Run `neocities` in a command line to see the options and get started.
24
24
 
25
+ ### Authentication
26
+
27
+ The CLI uses an API key and never asks for your Neocities password. Create or copy a key from your site's [settings](https://neocities.org/settings), then paste it into the prompt the first time you run a command. The key is stored in your local Neocities config for later commands.
28
+
29
+ For CI or other automation, set `NEOCITIES_API_KEY` instead. Keys supplied through the environment are not saved locally.
30
+
31
+ Run `neocities logout -y` to remove a saved key from the computer. Regenerate the key in your site settings if you need to revoke it everywhere.
32
+
25
33
  ## Neocities::Client
26
34
 
27
35
  This gem also ships with Neocities::Client, which you can use to write code that interfaces with the Neocities API.
data/lib/neocities/cli.rb CHANGED
@@ -41,6 +41,8 @@ module Neocities
41
41
  end
42
42
 
43
43
  def run
44
+ display_sign_in_status if @argv.empty?
45
+
44
46
  if @argv[0] == 'version'
45
47
  puts Neocities::VERSION
46
48
  exit
@@ -54,13 +56,15 @@ module Neocities
54
56
  send "display_#{@subcmd}_help_and_exit"
55
57
  end
56
58
 
59
+ return logout if @subcmd == 'logout'
60
+
57
61
  if !@api_key
58
62
  begin
59
63
  file = File.read @app_config_path
60
64
  data = JSON.load file
61
65
 
62
66
  if data
63
- @api_key = data["API_KEY"].strip # Remove any trailing whitespace causing HTTP requests to fail
67
+ @api_key = data["API_KEY"]&.strip # Remove any trailing whitespace causing HTTP requests to fail
64
68
  @sitename = data["SITENAME"] # Store the sitename to be able to reference it later
65
69
  @last_pull = data["LAST_PULL"] # Store the last time a pull was performed so that we only fetch from updated files
66
70
  end
@@ -69,38 +73,45 @@ module Neocities
69
73
  end
70
74
  end
71
75
 
72
- if @api_key.nil?
73
- puts "Please login to get your API key:"
76
+ prompted_for_api_key = false
77
+ if @api_key.nil? || @api_key.empty?
78
+ puts 'Please enter your Neocities API key.'
79
+ puts 'Create or copy one from https://neocities.org/settings'
80
+ @api_key = @prompt.mask('API key:').to_s.strip
81
+ prompted_for_api_key = true
82
+ end
83
+
84
+ @client = build_client @api_key
74
85
 
75
- if !@sitename && !@password
76
- @sitename = @prompt.ask('sitename:', default: ENV['NEOCITIES_SITENAME'])
77
- @password = @prompt.mask('password:', default: ENV['NEOCITIES_PASSWORD'])
86
+ if prompted_for_api_key || @sitename.nil?
87
+ resp = @client.info
88
+ if resp[:result] == 'error'
89
+ display_response resp
90
+ exit
78
91
  end
79
92
 
80
- @client = Neocities::Client.new sitename: @sitename, password: @password
93
+ @sitename = resp[:info][:sitename]
94
+ end
81
95
 
82
- resp = @client.key
83
- if resp[:api_key]
84
- conf = {
85
- "API_KEY": resp[:api_key],
86
- "SITENAME": @sitename,
87
- }
96
+ if prompted_for_api_key
97
+ conf = {
98
+ "API_KEY": @api_key,
99
+ "SITENAME": @sitename,
100
+ }
88
101
 
89
- FileUtils.mkdir_p Pathname(@app_config_path).dirname
90
- File.write @app_config_path, conf.to_json
102
+ FileUtils.mkdir_p Pathname(@app_config_path).dirname
103
+ File.write @app_config_path, conf.to_json
91
104
 
92
- puts "The api key for #{@pastel.bold @sitename} has been stored in #{@pastel.bold @app_config_path}."
93
- else
94
- display_response resp
95
- exit
96
- end
97
- else
98
- @client = Neocities::Client.new api_key: @api_key
105
+ puts "The API key for #{@pastel.bold @sitename} has been stored in #{@pastel.bold @app_config_path}."
99
106
  end
100
107
 
101
108
  send @subcmd
102
109
  end
103
110
 
111
+ def build_client(api_key)
112
+ Neocities::Client.new api_key: api_key
113
+ end
114
+
104
115
  def delete
105
116
  display_delete_help_and_exit if @subargs.empty?
106
117
  @subargs.each do |file|
@@ -121,8 +132,8 @@ module Neocities
121
132
  end
122
133
  end
123
134
  if confirmed
124
- FileUtils.rm @app_config_path
125
- puts @pastel.bold("Your api key has been removed.")
135
+ FileUtils.rm_f @app_config_path
136
+ puts @pastel.bold("Your API key has been removed.")
126
137
  else
127
138
  display_logout_help_and_exit
128
139
  end
@@ -561,6 +572,29 @@ HERE
561
572
  HERE
562
573
  end
563
574
 
575
+ def display_sign_in_status
576
+ config = {}
577
+ begin
578
+ config = JSON.parse File.read(@app_config_path)
579
+ rescue SystemCallError, JSON::ParserError
580
+ # Status should still be available without a readable config.
581
+ end
582
+ config = {} unless config.is_a?(Hash)
583
+ saved_key = config['API_KEY'].to_s.strip
584
+ environment_key = ENV['NEOCITIES_API_KEY']
585
+
586
+ status = if environment_key && environment_key != saved_key
587
+ environment_key.empty? ? 'Signed out' : 'Using NEOCITIES_API_KEY (username not loaded)'
588
+ elsif saved_key.empty?
589
+ 'Signed out'
590
+ elsif config['SITENAME'].to_s.empty?
591
+ 'API key saved (username not loaded)'
592
+ else
593
+ "Signed in as: #{config['SITENAME']}"
594
+ end
595
+ puts @pastel.cyan.bold(status)
596
+ end
597
+
564
598
  def display_help_and_exit
565
599
  display_banner
566
600
  puts <<HERE
@@ -575,6 +609,9 @@ HERE
575
609
  pull Get the most recent version of files from your site
576
610
  pizza Order a free pizza
577
611
 
612
+ The first command asks for an API key from your site settings.
613
+ Set NEOCITIES_API_KEY to use a key without saving it locally.
614
+
578
615
  HERE
579
616
  exit
580
617
  end
@@ -151,7 +151,9 @@ module Neocities
151
151
  post 'delete', 'filenames[]' => paths
152
152
  end
153
153
 
154
- def info(sitename)
154
+ def info(sitename=nil)
155
+ return get 'info' if sitename.nil?
156
+
155
157
  get 'info', sitename: sitename
156
158
  end
157
159
 
@@ -168,4 +170,4 @@ module Neocities
168
170
  JSON.parse resp.body, symbolize_names: true
169
171
  end
170
172
  end
171
- end
173
+ end
@@ -1,3 +1,3 @@
1
1
  module Neocities
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neocities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Drake