explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!


Leave a comment

a better Quick Git Merge/Rebase

Back in Feb, I posted a quick tip about expediting the process of pulling the latest from develop and merging into your current branch. https://jwopitz.wordpress.com/2023/02/24/quick-git-merge-rebase/

Usually I just do this from origin’s develop branch, so why should I have to type it every time? I shouldn’t. So I made a git alias to make this even easier:

git fmerge

I could also specify a different branch (it defaults to develop):

git fmerge another-branch

… or you can even specify another origin (though I have never done this)

git fmerge another-branch another-origin

Here is the alias you can add to your ~/.gitconfig file:

[alias]
        fmerge = "!f() { \
                branch=${1:-develop}; \
                shift; \
                remote=${1:-origin}; \
                git fetch $remote $branch:$branch && git merge $branch; \
        }; f"


1 Comment

Quick Git Merge/Rebase

Used to, in order to get the latest from the develop branch into your working branch (assuming you’re using something like GitFlow or the like) you’d normally do this:

# git working-branch
git checkout develop
git pull
git checkout working-branch 
git merge/rebase develop

I’ve learned you can shave this down to just:

# git working-branch
git fetch origin develop:develop 
git merge/rebase develop


Leave a comment

Adventures in teaching an 8yo Math

Recently my 8yo, who LOVES math, was asking me about something they’d discovered on a school video about Math. Apparently they had gone Youtube Spelunking and discovered the concept of Squares and Square Roots. In trying to supplement their understanding of the topic I discovered this brilliantly succinct explanation on Squares and the magic therein.

https://betterexplained.com/articles/surprising-patterns-in-the-square-numbers-1-4-9-16/


Leave a comment

Array Quick Trick

I was working on some coding challenges just to keep myself fresh. I encountered a fairly easy one, dealing w. factorials – link

Now there are various approaches to this, some more complex than others. I specifically wanted to try to use built in array APIs to get this done, specifically using reduce. At first you might think, “well if you’re gonna us an array, you’ll need to fill it with the values you want to factorial-ize“. And if you are using reduce, then you do indeed need a non-empty array.

let a = [];
for( let i = 0; i < factorial; i++ ){ a.push(i + 1); }
return a.reduce(( acc, crnt ) => acc * crnt
   , 1 )

I specifically wanted to avoid this approach, partly because I wanted to challenge myself, partly because I know there is a simpler way. Well there is.

return new Array( factorial ).fill( true ).reduce(( acc, crnt, idx ) => acc * ( idx + 1 )
, 1 )

Nothing here is groundbreaking, I just wanted to challenge myself in trying to accomplish this in one line or specifically trying to avoid using a for-loop to fill an array.


Leave a comment

SSH’ing to Amazon Linux instance

After creating a .pem file and following instructions per their documents – I was running into this:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for './.dev/ec2.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "./.dev/ec2.pem": bad permissions
Permission denied (publickey).

Googling the issue led me to this post https://99robots.com/how-to-fix-permission-error-ssh-amazon-ec2-instance/. You need to change the key’s permissions by chmod’ing the file like so:

chmod 400 ./.dev/ec2.pem 

However this still resulted in the Permission denied (publickey) error. Down in the comments, one user said that the user I was using (jwopitz) was not the what AWS is looking for. I was using

ssh -i /path/my-key-pair.pem my-instance-user-name@my-instance-public-dns-name

Instead it should be (assuming you haven’t changed any defaults on your instance’s configuration):

ssh -i /path/my-key-pair.pem ec2-user@my-instance-public-dns-name


Leave a comment

Quickly Sync Mac OS’s Time via Script

I’ve noticed lately that my computer’s time is off by over a minute, sometimes several.  This happens as after I have put my computer in standby by closing the lid.  The problem it causes is that with the time out of sync, I cannot use my auth tokens to log into my work’s VPN.  So instead of opening the Time & Date preferences, I use this one line command to quickly update.

Open Terminal and type:

sudo ntpdate -u time.apple.com

You can even create an alias in your .bash_profile to make it simpler:

alias synctime="sudo ntpdate -u time.apple.com"

 


Leave a comment

Permissions-Based UXs in Angular Using Directives

the problem

A UI may want to present some content in a read-only state, only allowing the user to interact with said content if they are permitted to do so.  I won’t argue the merits/demerits of this particular UX, only how to solve this requirement.

the solution

.directive( 'permissions', [
     'popupService',
     'sessionService',
     function( popupService, sessionService ){

          return {
              restrict: 'A',
              priority: -1,
              link: function( $scope, elem, attrs ){

                   elem.on( 'click', function( evt ){
                        if ( !sessionService.user.isPremium )
                        {
                             evt.preventDefault()
                             evt.stopImmediatePropagation()

                             $scope.$apply( function(){
                                  popupService.show( ... )
                             })
                        }
                   }
              }
          }
     }
])

You can see the original question/answer here – http://stackoverflow.com/a/38439845/1121919


Leave a comment

nvm/node: an easy way to use different versions for different projects

problem

I need to have different versions of node/npm for different projects.  I’d prefer to have each project “know” which version of node/npm it needs to use.


solution

prerequisites

Before we get started, there are a few prerequisites:

  • understand that this is a Mac solution.  While I’m sure a similar solution exists for Windows, I am neither knowledgeable enough nor inclined to write a compatible shell script for Windows at this time.
  • have/install nvm.  I installed mine with Brew.
  • have at least one version of node installed via nvm
  • have a basic understanding of terminal.
  • have a willingness to edit your .bash_profile.

gameplan

leveraging .nvmrc

Much like Ruby has a shortcut for setting the current version of Ruby to use, nvm has a similar feature.  When you enter the following command…

nvm use

… then nvm will look for a .nvmrc file and get the correct version number to use from there.  So first navigate to a project folder that uses node and create a new file called .nvmrc.  Inside just write 4.2.  If you’re a command-line geek then do the following:

cd <project folder>
touch .nvmrc # creates the file
echo \4.2 >> .nvmrc # appends the version to the file
cat .nvmrc # prints the file's contents

editing your .bash_profile

So the easiest way to make nvm read your .nvmrc file is to do a check when you change directories.  We need to make a function we can call when we cd into a directory:


#!/bin/sh
enter_directory(){
if [ "$PWD" != "$PREV_PWD" ]; then
PREV_PWD="$PWD";
if [ -e ".nvmrc" ]; then
nvm use;
fi
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND enter_directory;"

view raw

check.sh

hosted with ❤ by GitHub

All this is doing is

  • checking to see if the current directory matches the previous directory
  • if it doesn’t , then it stores the current directory to the previous’ variable
  • then it checks to see if a .nvmrc file exists
  • if so, then tell nvm to use it
  • lastly we tell the OS to call this command any time we do something in Terminal/Bash