ggev 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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/ggev +188 -0
  3. data/lib/config/default.rb +26 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd2b7b52bd1fcf147cc82c5a51a160ee023c7358b6ff3e4029dfe3d850b6c149
4
+ data.tar.gz: b4dbc431f9d16660aa790697458c89d523e4020f6f80139588dda8197ca2147d
5
+ SHA512:
6
+ metadata.gz: 5bbcd0cb63e91d38bcc72dce372b3a553acc4b2a96b2fc2e0fe5ef79c88b2b02eaed0cbc3f8633b4e6f8860c32859ec10aa7e430cdb5723889190290a1224ddb
7
+ data.tar.gz: 22a26795ae8f03b40a4a71036f8f953c252f96f33496198ca79b4791f7645118a63b21ffd6f423b42597eced456b73c578a01b71e70e2eb7bb844705a0186763
data/bin/ggev ADDED
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "yaml"
4
+ require "config/default.rb"
5
+ require "fileutils"
6
+
7
+ # Constants
8
+ DEFAULT_CONFIG_FILE=File::expand_path("~/.config/ggev/config.yaml")
9
+ DEFAULT_HOME_PATH=File::expand_path("~/.ggev")
10
+ REPO_PATH="#{DEFAULT_HOME_PATH}/repo"
11
+ DEFAULT_ENC_CIPHER="aes-128-cbc"
12
+
13
+ # Functions
14
+ def usage()
15
+ puts "Usage: #{$PROGRAM_NAME} [command]"
16
+ puts ""
17
+ puts "Commands:"
18
+ puts " init"
19
+ puts " push"
20
+ puts " pull [-f|--force]"
21
+ end
22
+
23
+ def run_cmd_with_exitstatus(cmd)
24
+ if not system(cmd)
25
+ return false, $?.exitstatus
26
+ end
27
+ return true, $?.exitstatus
28
+ end
29
+
30
+ def run_cmd(cmd)
31
+ ok, es = run_cmd_with_exitstatus(cmd)
32
+ return ok
33
+ end
34
+
35
+ def must_run_cmd(cmd)
36
+ run_cmd(cmd) or exit
37
+ end
38
+
39
+ # Main
40
+ if ARGV.length < 1
41
+ usage()
42
+ exit
43
+ end
44
+
45
+ # ARGV pop command
46
+ cmd = ARGV[0]
47
+ ARGV.slice!(0)
48
+
49
+ ## Process commands
50
+ if cmd=="init"
51
+ if File.exists?(DEFAULT_CONFIG_FILE)
52
+ puts "Initiated already"
53
+ puts "See #{DEFAULT_CONFIG_FILE}"
54
+ exit
55
+ end
56
+
57
+ ## Default config
58
+ FileUtils::mkdir_p(File::dirname(DEFAULT_CONFIG_FILE))
59
+
60
+ puts "Enter git repository to store/save your configs."
61
+ repo = ""
62
+ while repo.length==0
63
+ print "Repository: "
64
+ repo = STDIN.gets.strip
65
+ end
66
+
67
+ puts "Enter secret key to encrypt/descrypt your configs."
68
+ key = ""
69
+ while key.length==0
70
+ print "Key: "
71
+ key = STDIN.gets.strip
72
+ end
73
+
74
+ defaultCfg = GGEV::CONFIG.default
75
+ defaultCfg["repo"]["remote"] = repo
76
+ defaultCfg["encrypt"]["key"] = key
77
+ File::write(DEFAULT_CONFIG_FILE, defaultCfg.to_yaml)
78
+
79
+ ## Home dir
80
+ if not Dir::exists?(DEFAULT_HOME_PATH)
81
+ FileUtils.mkdir_p(DEFAULT_HOME_PATH)
82
+ end
83
+
84
+ if not Dir::exists?(REPO_PATH)
85
+ must_run_cmd("git clone #{repo} #{REPO_PATH}")
86
+ end
87
+
88
+ ## DONE
89
+ puts ""
90
+ puts "Congratulations! Init success!"
91
+ puts "The default config file has been created at #{DEFAULT_CONFIG_FILE}."
92
+ puts "You can modify it as your necessary."
93
+ end
94
+
95
+ # Other commands are not allowed before `init` command has been success.
96
+ if not File.exists?(DEFAULT_CONFIG_FILE)
97
+ puts "Please initiate ggev. "
98
+ puts "Run `ggev init`"
99
+ exit
100
+ end
101
+
102
+ if cmd=="push"
103
+ cfg = YAML::load_file(DEFAULT_CONFIG_FILE)
104
+
105
+ cfg["modules"].each { |mod|
106
+ puts "Processing module #{mod["name"]} ..."
107
+
108
+ mod_path = "#{REPO_PATH}/#{mod["name"]}"
109
+ if not Dir::exists?(mod_path)
110
+ FileUtils::mkdir_p(mod_path)
111
+ end
112
+
113
+ mod["files"].each { |origin_file_path|
114
+ origin_file_path = File::expand_path(origin_file_path)
115
+ file_name = File::basename(origin_file_path)
116
+ cache_file_path = "#{mod_path}/#{file_name}"
117
+
118
+ puts "#{origin_file_path} =(enc)=> #{cache_file_path} ..."
119
+ must_run_cmd("openssl enc -#{DEFAULT_ENC_CIPHER} -base64 -k #{cfg["encrypt"]["key"]} -in #{origin_file_path} -out #{cache_file_path}")
120
+ }
121
+ }
122
+
123
+ ### Cipher
124
+ File::write("#{REPO_PATH}/.cipher", "#{DEFAULT_ENC_CIPHER}")
125
+
126
+ ### Commit repo
127
+ Dir::chdir(REPO_PATH) {
128
+ must_run_cmd("git add -A")
129
+ must_run_cmd("git -P diff --cached --stat")
130
+ must_run_cmd("git commit -a -m 'auto-commit by ggev'")
131
+ must_run_cmd("git push origin master")
132
+ }
133
+ end
134
+
135
+ if cmd=="pull"
136
+ ifForce = false
137
+ cfg = YAML::load_file(DEFAULT_CONFIG_FILE)
138
+
139
+ # Process args
140
+ ARGV.each { |arg|
141
+ if arg=="-f" or arg=="--force"
142
+ ifForce = true
143
+ end
144
+ }
145
+
146
+ ### Pull resp
147
+ Dir::chdir(REPO_PATH) {
148
+ must_run_cmd("git fetch origin master")
149
+ ok, ec = run_cmd_with_exitstatus("git diff --exit-code --stat origin/master")
150
+
151
+ if ok
152
+ # Unchanged
153
+ puts "Unchanged"
154
+
155
+ if not ifForce
156
+ return
157
+ else
158
+ puts "Force pull"
159
+ end
160
+ else
161
+ # Error happen
162
+ if ec!=1
163
+ return
164
+ end
165
+
166
+ # ec==1
167
+ # Something changed
168
+ must_run_cmd("git merge origin/master ")
169
+ end
170
+ }
171
+
172
+ # Read cipher
173
+ cipher = File::read("#{REPO_PATH}/.cipher")
174
+
175
+ cfg["modules"].each { |mod|
176
+ puts "Processing module #{mod["name"]} ..."
177
+
178
+ mod_path = "#{REPO_PATH}/#{mod["name"]}"
179
+ mod["files"].each { |origin_file_path|
180
+ origin_file_path = File::expand_path(origin_file_path)
181
+ file_name = File::basename(origin_file_path)
182
+ cache_file_path = "#{mod_path}/#{file_name}"
183
+
184
+ puts "#{cache_file_path} =(enc)=> #{origin_file_path} ..."
185
+ must_run_cmd("openssl enc -#{cipher} -base64 -k #{cfg["encrypt"]["key"]} -d -in #{cache_file_path} -out #{origin_file_path}")
186
+ }
187
+ }
188
+ end
@@ -0,0 +1,26 @@
1
+
2
+ module GGEV
3
+ module CONFIG
4
+
5
+ @@default = {
6
+ "repo" => {
7
+ "remote" => "",
8
+ },
9
+ "encrypt" => {
10
+ "key" => ""
11
+ },
12
+ "modules" => [
13
+ {"name" => "vim", "files" => ["~/.vimrc"]},
14
+ {"name" => "git", "files" => ["~/.gitconfig"]},
15
+ {"name" => "zsh", "files" => ["~/.zshrc"]},
16
+ {"name" => "tmux", "files" => ["~/.tmux.conf"]},
17
+ {"name" => "autossh", "files" => ["~/.config/autossh/hosts"]}
18
+ ]
19
+ }
20
+
21
+ def self.default()
22
+ @@default
23
+ end
24
+
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ggev
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - supergui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-06-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Manage personal ENV
14
+ email: supergui@live.cn
15
+ executables:
16
+ - ggev
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/ggev
21
+ - lib/config/default.rb
22
+ homepage: https://rubygems.org/gems/ggev
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Guigui ENV!
45
+ test_files: []