Uncategorized

Misusing C# 8 `using` to get Golang `defer` in DotNet Core 3

I started out with C# since then I’ve learned other languages and one of my favorites is Golang.

When I was reading the release notes from C# 8 I saw the new using declaration and through it was awesome… I also realized it could be misused to give C# the defer keyword from Golang.

Whats defer in Golang do?

defer in Golang lets you define a function that will get run when the code block exits.

The example below will print:

hello
world

Code: Try it out here


package main

import "fmt"

func main() {
    defer fmt.Println("world")
    fmt.Println("hello")
}

I’m a big fan of this approach as I think it’s clearer to read than the standard try finally pattern as it lets you put the cleanup code directly below the code which is making the mess like:

    f := createTempFile("/tmp/defer.txt")defer deleteTempFile(f)writeFile(f)        // here the `deleteTempFile` method gets called

How can we do this in C# 8 with the new using syntax?

Well now because using var x = new thing()exists you can write a simple class called defer which runs a function when the current method exits, just like in golang

The interesting thing is that as a using statement generates a try finally under the covers these defer functions will still run if an exception is thrown.

The example below will print:

Hello World!
Defer 2
Defer 1
Exception thrown: It's all one wrong

Code:

Should you do this? Well that’s kinda up to you, I don’t think it’s super nasty but I’ve not used this in anger so probably worth testing out a bit before going all out.

Standard
Coding, How to, vscode

VSCode Container dev a Remote Docker Daemon [Brain dump]

Note: This is more a stream of consciousness than a blog post. It’s not detailed and mainly for my own memory on how to set this up. Be cautious.

So I’ve recently built a new home server/lab and wanted to use some of it’s power to run my daily dev stuff.

I’ve been playing a bit with VSCode Remote for Containers which lets you define you dev environment as a container and commit it along with your code… it’s super nice.

So how about hooking it up to a remote docker daemon so that I can use the power of the new server from my laptop when I’m at home but switch back to my local docker deamon when I’m traveling… turns out the team have thought of that!

There are some great docs here on how to do this in detail: https://code.visualstudio.com/docs/remote/containers-advanced#_developing-inside-a-container-on-a-remote-docker-host

What will follow is only useful if your like me, running Ubuntu on your laptop and also Ubuntu/Linux on the remote Daemon machine.

First up one of the key problems with this setup is with the file system access as a remote docker daemon on the server can’t see files on the Laptop. To get around this I’ve setup two alias which use rsync to push and pull data to and from the server from my laptop.

alias docker-remote-push="rsync -rlptv --progress \${PWD} \"lawrence@ubuntudev:\${PWD}/../\""

alias docker-remote-pull="rsync -rlptv --progress \"lawrence@ubuntudev:\${PWD}\" \${PWD}/../"

Then I can use docker context to setup a context for local and my ubuntudev remote box to easy switch between them.

Have a look here on how to create a context. https://docs.docker.com/engine/reference/commandline/context_create/

The last alias are to foward the docker for the remote deamon to my local box and switch to the context I’ve created for it.

alias docker-remote-start="ssh -fNL localhost:23750:/var/run/docker.sock lawrence@ubuntudev && docker context use remote"

alias docker-remote-stop="docker context use local"

Then all you need to do is add

 "docker.host":"tcp://localhost:23750"

To your settings.json in VSCode and now you are set to go.

Open up a folder in VSCode with a `.devcontainer` then run

docker-remote-push
docker-remote-start

Then use the `Reopen in container` option in VSCode and it’s done.

Standard
Coding, How to

Easily Debugging Terraform Provider for Azure in VSCode

So you’re making a change to the provider to add a feature, it’s going great and your ready to test it out…. but then you realize things get a bit ropey… ideally you want a visual debugger to step through the code.

Well here is how to set that up in VSCode.

First make sure you have VSCode setup for golang debugging (delve configured etc). Then it’s easy, say you want to debug a new provider you’ve written and it has a test called TestAccDataSourceAzureRMFunction_basic in the file data_source_function_test.go then you can setup your launch.json file in VSCode to look like:

(Makes sure you replace the details in the private.env with your service principal and subscription details)

 

Standard