Skip to content

Instantly share code, notes, and snippets.

View euccas's full-sized avatar

Euccas Chen euccas

View GitHub Profile
@euccas
euccas / Jenkinsfile.groovy
Last active June 8, 2020 02:05 — forked from Faheetah/Jenkinsfile.groovy
Jenkinsfile idiosynchrasies with escaping and quotes
node {
echo 'Results included as an inline comment exactly how they are returned as of Jenkins 2.121, with $BUILD_NUMBER = 1'
echo 'No quotes, pipeline command in single quotes'
sh 'echo $BUILD_NUMBER' // 1
echo 'Double quotes are silently dropped'
sh 'echo "$BUILD_NUMBER"' // 1
echo 'Even escaped with a single backslash they are dropped'
sh 'echo \"$BUILD_NUMBER\"' // 1
echo 'Using two backslashes, the quotes are preserved'
sh 'echo \\"$BUILD_NUMBER\\"' // "1"
@euccas
euccas / react_setState.js
Last active June 8, 2020 02:05
react setState
constructor(props) {
super(props)
this.state = {
userName: 'Azat Mardan',
userEmail: 'hi@azat.co',
userId: 3967
}
}
updateValues() {
this.setState({userName: 'Azat'})
import React, { Component } from "react";
import ReactDOM from "react-dom";
class App extends Component {
constructor() {
super();
this.state = { data: [] };
}
componentDidMount() {
fetch(`https://api.coinmarketcap.com/v1/ticker/?limit=10`)
.then(res => res.json())
@euccas
euccas / yamldiff.py
Last active March 5, 2022 02:46
compare yaml files, report diff in tags, recursively
def rdiff(yamla, yamlb, path='', key=getkey):
if isinstance(yamla, list) and isinstance(yamlb, list):
for d in listdiff(yamla, yamlb, path=path, key=key):
yield d
elif isinstance(yamla, dict) and isinstance(yamlb, dict):
for d in dictdiff(yamla, yamlb, path=path, key=key):
yield d
else:
if yamla != yamlb:
yield (path, 'value_difference', yamla, '-->', yamlb)
@euccas
euccas / remove_empty_hash_element.rb
Last active June 8, 2020 02:06
remove empty hash element
def remove_empty_hash_element(h)
if h and h.length>0
h.each do |k,v|
h.delete(k) if v.respond_to?('empty?') and v.empty?
end
end
end
@euccas
euccas / zlib_bi_reverse.cpp
Last active June 8, 2020 02:07
Zlib function: bits reverse
/* ===========================================================================
* Reverse the first len bits of a code, using straightforward code (a faster
* method would use a table)
* IN assertion: 1 <= len <= 15
*/
local unsigned bi_reverse(code, len)
unsigned code; /* the value to invert */
int len; /* its bit length */
{
register unsigned res = 0;
@euccas
euccas / read_mem_to_buffer.cpp
Last active June 8, 2020 02:07
Read memory and store to a 16KB buffer, then output to a file. Pseudo code.
const size_t buffSize = 16 * 1024; // 16KB
size_t currReadSize = buffSize; // default
size_t dumpedSize = 0;
uint8 currBuff[buffSize];
while (currAddr < endAddr)
{
currReadSize = (currAddr + buffSize <= endAddr) ? buffSize : (size_t)(endAddr - currAddr);
read_memory(currBuff, currAddr, sizeof(uint8) * currReadSize, memory_type);
dumpedSize += currReadSize;
currAddr += currReadSize;
@euccas
euccas / connect_list_items.py
Last active June 8, 2020 02:07
Generate a string connecting items of a given list. The string should look like: a and b a, b and c Being strict on the separator and "and".
item_names = mydict["items"].split()
if len(item_names) > 1:
item_names[-1] = "and " + dv_tiers[-1]
sep = ", " if len(item_names) > 2 else " "
namstr = sep.join(item_names)
print namestr # a, b and c
@euccas
euccas / jenkins_copy_jobs_in_view.groovy
Last active June 8, 2020 02:08
Copy all jobs in a view
import hudson.model.*
def str_view = "AIR_Project_A630"
def str_search = "air_a630"
def str_replace = "air_a630v1"
def view = Hudson.instance.getView(str_view)
//copy all projects of a view
for(item in view.getItems())
@euccas
euccas / test_jsonb.rb
Last active June 8, 2020 02:08
postgresql JSONB
require 'pg'
require 'json'
def db_connect(host, dbname, user, password)
begin
conn = PGconn.connect(host, 5436, '', '', dbname, user, password)
rescue
puts "Failed to connect to database #{dbname} on #{host}"
end
return conn