Showing posts with label archlinux. Show all posts
Showing posts with label archlinux. Show all posts

Sunday, November 28, 2021

git-worktree

 


 Since 2015 git added a command git-worktree. This command could be used  when you have a repository  and each branch represent an old Versions of the code, one feature_branch or simple using the main branch.  This command generate a new tree, and you can avoid doing checkout on branches every  time you have to do an urgent bug fix or edit without doing changes on the main tree locally. 

Advantages.

  •     the Time spent by you IDE to reindex the switched branch, so you save a little of Time If you use some jetbrains IDE or eclipse.
  •     No need to fetch ,if you don't want to.
  •     By each tree you Will have a new project in the IDE .
  •     This command use a new directory un your file system, AND use the same .git/config file.
  •     You could compare the code by tree, branches, and directories.


In the old fashion way you Will have to clone each repo in diferents directories, and check or set manually  same config on .git/config,  each directory Will be a project in you IDE. In this way you only could compare by directory and by branch.

Another way was use git stash to save what are you working on, and do the checkout to the branch version  with the bug on production, this way you have a Lot of context switch (to make an hotfix) , cos you Will need to do the reindex on you IDE (in case you use eclipse or some jetbrains IDE ), and later checkout to your branch_feature, unstash, AND again reindex, and always don't forget to clean cache!

Some examples:


(base) ┌─[j3nnn1@caracas] - [~/git/tools] - [Sun Nov 28, 13:26]
└─[$] <git:(master)> git worktree add /home/j3nnn1/git/tools2
Preparing worktree (new branch 'tools2')
Updating files: 100% (2887/2887), done.
HEAD is now at db7534a Update paquetes.txt


When hotfix is ready and the fix it’s in production:


└─[$] <git:(master)> git worktree remove /home/j3nnn1/git/tools2


$ git worktree add <path> <branch>


With -d  throwaway working tree


git worktree prune 


To clean up any stale administrative files.


git worktree lock


To not delete, remove, or prune when gc.worktreePruneExpire passes.


List all trees


└─[$] <git:(master)> git worktree list
/home/j3nnn1/git/tools   db7534a [master]
/home/j3nnn1/git/tools2  db7534a [tools2]


 

Thursday, November 25, 2021

chezmoi saving your config files versioned


 



pacman -Syu chezmoi

chezmoi init (make a new dir  ~/.local/share/chezmoi)

chezmoi add ~/.bashrc

chezmoi [add|edit|update]  ~/.bashrc

chezmoi cd (~/.local/share/chezmoi)

git remote add origin https://github.com/j3nnn1/dotfiles.git

git branch -M main

git push -u origin main 

 

Additional config:

git config pull.ff true

git config pull.rebase true

and that's all.

Sunday, November 21, 2021

udev events, making an udev rule and debugging rules.



udev is a device manager for the linux kernel.

when you connect some device (pendrive, camera, microphone) on a linux machine, you have a daemon listen to events that are fired when the kernel register a new device. All devices are mapped to a file on /dev so you could check some info about the device with this command:

udevadm info -a -n  /dev/DEVICE_NAME

i.e.: udevadm info -a -n  /dev/ttyUSB2

udevadm info -a -n  /dev/ttyUSB2 | grep '{devpath}' | head -n 1
udevadm info -a -n  /dev/ttyUSB2 | grep '{idVendor}' | head -n 1
udevadm info -a -n  /dev/ttyUSB2 | grep '{idProduct}' | head -n 1
udevadm info -a -n  /dev/ttyUSB2 | grep '{serial}' | head -n 1


knowing the device info we could make a rule based in the information and execute a new action: make a file, make a symlink or execute a script. For example I want to create a rule that match with the attributes devpath, idVendor, idProduct, serial and generate a new symlink.

we need to create a file into:

/etc/udev/rules.d/99-some-rule.rules

- in this file we need to use "" in the values.
- each line is a rule.
- each line has many or one attribute and one action. When the attributes matched  the action is executed, that's all.
- if you edit or create a new rule, you need to load and trigger all rules again.

udevadm control --reload-rules && udevadm trigger


- so sometimes is tricky check why some rule is not executed. For debug you have:

    - udevadm test /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0/tty/ttyUSB0

      the devices path you could obtain with the comand udevadm info.

    - udevadm monitor

      show the events that are fired.
    
    - udevadm control --log-priority=debug

      set the environment on debug mode.

    - udevadm test $(udevadm info --query=path --name=ttyUSB0)
      
      test all rules associated to the device name ttyUSB0

- when you have an syntax error on some rules file, this will appear in the logs:
    - journalctl -f

- restart service:
    - systemctl restart systemd-udevd
 

- kernel messages
    - dmesg

- example: file called 99-new-rule.rules:

SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", ATTRS{devpath}=="1.2", ATTRS{bcdDevice}=="0264", SYMLINK+="FANCY_NAME"

The 99 number indicated the priority for execution. in this case would be the last one to execute.

It's important to put double quotes instead single quote.

Monday, September 6, 2021

4 tools in javascript

picture name: wichy asking for more coffee


01 NVM: 

NVM is the acronym for Node Version Manager, which is a command-line tool that lets you install and use many Node.js versions and NPM on your computer. It is a dev tool, so I don't recommend using it in production environments. NVM works fine in Linux and Mac. You can also check the documentation here: https://github.com/nvm-sh/nvm.


When is it useful to use NVM? 

*   In teams where we have different operating systems.

*   When we have to test new things, for example, the testing version for Node.js.

*  When we have many projects with different versions, and you cannot update Node.js and you have to maintain two or more node js versions in many projects, so is a way to tidy up the dev environment, switching between Node.js versions.


NVM: Installation

Mac:

You can use homebrew. If you don’t know how to install it, you could check here it (https://brew.sh/) and that’s all.

 

brew install nvm
source $(brew --prefix nvm)/nvm.sh
echo 'source $(brew --prefix nvm)/nvm.sh' >> ~/.profile


Linux:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash


The line before will have an output such as this:

 

=> Downloading nvm from git to '/home/jennifer/.nvm'
=> Cloning into '/home/jennifer/.nvm'...
remote: Enumerating objects: 278, done.
remote: Counting objects: 100% (278/278), done.
remote: Compressing objects: 100% (249/249), done.
remote: Total 278 (delta 33), reused 88 (delta 16), pack-reused 0
Receiving objects: 100% (278/278), 142.36 KiB | 0 bytes/s, done.
Resolving deltas: 100% (33/33), done.
Checking connectivity... done.
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/jennifer/.bashrc
=> Appending bash_completion source string to /home/jennifer/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion


Make sure that these lines below are in your ~/.bashrc or ~/.zshrc:

 

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm



 

My personal desktop runs over Arch Linux,  so I used an aur package, for example https://aur.archlinux.org/packages/nvm/. I downloaded the snapshot file and uncompressed the tar.gz file.  Also, you could use the same installation explained before or these lines for installing nvm:

$ cd nvm
$ makepkg  -s  #this will generate an Arch Linux package (nvm.tar.xz).
$ ls -l nvm.tar.xz

$ pacman -U nvm.tar.xz


NVM Usage:

nvm list : List all versions installed on your machine

➜  ~ nvm list
->       system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)


NVM install: Install a local version on your machine from the remote list. The remote list could be filtered by another command like: nvm ls-remote <versionLike>  for example “nvm ls-remote v11” lists all versions that have v11 as prefix.


➜  ~  nvm install v10.15.3
Downloading and installing node v10.15.3...
Downloading https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz...
######################################################################## 100,0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.15.3 (npm v6.4.1)
Creating default alias: default -> v10.15.3


nvm use: set in the current terminal the environments variables so all command executed related to Node.js will use the version defined by NVM.

➜  ~ nvm use v10.15.3
Now using node v10.15.3 (npm v6.4.1)


nvm which: show the path to the Node.js bin file.

➜  ~ nvm which v10.15.3
/home/jennifer/.nvm/versions/node/v10.15.3/bin/node




02 Docker container with node js/ angular-cli:



Hub docker contains a thousand images that you could use in your daily tasks at work, in a simple search you could see (https://hub.docker.com/search?q=angular-cli&type=image)

About 1 - 25 of 492 results for angular-cli. 

Here is my recommendation to use it in a local environment for a development process: https://hub.docker.com/r/trion/ng-cli/


docker pull trion/ng-cli

When node_modules directory already was generated.

docker run -u $(id -u) --rm -p 4200:4200 -v "$PWD":/app trion/ng-cli  ng serve --host 0.0.0.0 --disableHostCheck


In this way, you need node_modules directory inside the directory defined in $PWD variable.

When a node_modules directory is not generated. And that’s all.

first

docker run -u $(id -u) --rm -p 4200:4200 -v "$PWD":/app trion/ng-cli npm install


later:

docker run -u $(id -u) --rm -p 4200:4200 -v "$PWD":/app trion/ng-cli  ng serve --host 0.0.0.0 --disableHostCheck




03 Dotenv using a .env file with and avoid versioning



Dotenv is useful to keep tidy up the code, avoid magic number, avoid push any secret key to the repo and it is a relief for many environments. With this, you can run your app anywhere by modifying the environment variables without changing your code and without rebuilding it!

npm install dotenv


Later, create the .env file in the root of your app and add your variables and values to it. Don’t forget to add this file to .gitignore to avoid committing it.


.env
PORT=55555



Server.js
const dotenv = require('dotenv');
dotenv.config();
console.log(`Your port is ${process.env.PORT}`);


4 Cloud solutions:

For each project, you could use one of these, sometimes I have to share code with a friend or when I have a doubt, and I need to ask something with a context code, it’s the best way to show when you are remote, for example for an angular-cli project there is no doubt that the best is  https://stackblitz.com/ has a better integration with angular and let you download the project in a zip file, also is recommended by @angular documentation. 

Also for Js, HTML, and CSS, and tiny projects you could use https://jsfiddle.net/, https://codepen.io/.  In the last one, there are many web artists and you could make you own portfolio to share it.  And From Red Hat, we have https://codenvy.com/ has a free subscription with limitations, but it’s ok for prototyping things.