Not today...

Filed under Snippet...

comments

Snippet

Golang SSH, marshalling, unmarshalling

Golang is a wonderful language to deal with the SSH protocol. It’s mostly due to the SSH library which is pretty exhaustive. However, when I had to deal with external requirements like SSH Agent or OpenSSH I experienced a lack of example and struggled a bit interfacing. In this article we will see how to exchange keys between a program written in Go and those tools using files. This means to output our keys in a proper format (marshaling) and being able to read them (unmarshalling) from the external tool format. Read More...

Tagged golang , dev

comments

Snippet

Git amend history

This post is a simple copy of this answer, go there to see the original. As I am using git extensively in my day to day life/job, I encountered the issue to simply amend a commit which is already two or three commits before the current one. I could use git rebase -i with some stashing to apply the changes at the proper moment. This is most of the time a viable solution, however, it may be complicated because I have too much new code and stashing would be complicated. Read More...

Tagged cli , git

comments

Snippet

Simple HTTPs server

Sometimes we need to create a simple server saying hello through https. Here is a simple snippet to achieve this in a shell. # first we generate a self signed certificate for domain foo openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes \ -out foo.crt -keyout foo.key -subj "/CN=foo.com" # then we start a server using socat sudo socat "ssl-l:443,cert=foo.crt,key=foo.key,verify=0,fork,reuseaddr" \ SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; echo Hello World\! Read More...

Tagged cli , admin

comments

Snippet

Parallel runners with teardown in go

From time to time in go I have to start multiple small services acting in parallel. For example, a ssh server tied to an administration console (one is running on port 80 the other on port 22), or a kafka consumer pushing to a database and a website to serve those informations. You can decouple this in multiple programs, or run them through some kind of a manager and handle everything at the same place. Read More...

Tagged golang , dev

comments

Snippet

Filepath Walk bug?

During the development of a small project I noticed something strange with golang’s function filepath.Walk. If you are going over a directory, and return filepath.SkipDir error on a file entry. The walker will stop. Setup directory First, set up a simple directory to walk mkdir -p foo/baz touch foo/bar touch foo/baz/qux Run the script Now, let’s run this example script, with the previous directory as the entry argument. I will reuse the example from golang documentation website with small changes. Read More...

Tagged golang

comments

Snippet

Cancel copy of huge file in Go

I recently came across this video on Golang programming. I think this was the moment I finally fully understood the power of Go. It is smart, simple and elegant, I love it. Then, a few days later, I was coding on a toy project and I was doing some stuff around the io package to copy huge files. I wanted to achieve copy cancelation during the processing, basically, being able to interrupt. Read More...

Tagged dev , golang

comments

Snippet

direnv

I recently discovered the direnv project. Which helped me a lot for setting up my development environments. I will share a bit of things I use in my daily basis. Python projects This is well documented but here is what I use in python projects to set up a default virtualenv. layout python That’s it! It sets up a virtualenv in your .direnv directory, and load the updated PATH. Golang projects This one is a bit trickier. Read More...

Tagged bash , cli , dev

comments

Snippet

Golang err shadowing

A feature I like in golang is the hability to declare a variable at the assignation time. Something like this: foo := "bar" Here the variable foo will automatically set up as a string with the value “bar”. One more feature is to be able to allocate on same line as doing a comparison. Like this: if foo := "bar"; foo == "baz" { // do something } else { // do something else } This is really handy when it comes to catch errors from an other function: Read More...

Tagged golang , dev

comments

Snippet

Git clone inside a mounted volume with Docker

DISCLAIMER: This is now fixed in git new releases and does not need to be done anymore. I use an old version of alpine in order to have an unpached version of git. I ran into an interesting issue lastly. I wanted to mount a volume inside a container and clone a repo in it. I also wanted to avoid messing with the permissions and pass my user to the container as well. Read More...

Tagged dev

comments

snippet

Makefile

Recently, I had to build some project involving a lot of files. Two choices were possible: use a script to build use a Makefile I ended up with a Makefile, which is the best solution I think. As the script has to generate a bunch of files from “source” files. (I have to confess also, I have been a bit influenced by jessfraz with those tweets: here and here) This project also learned me some tricks which can be reused, here is a little list Read More...

Tagged dev