Drop #418 (2024-02-09): Weekend Project Edition

Your Own Private Ephemeris CLI

We spent some time talking about, well, time, this week; specifically, times of various ephemeris. While each of the sections in Drop #416 had a CLI component, we don’t need to settle for someone else’s vision of how things should work!

So, today, we’ll use a starter Golang project to engage in a choose-your-own-adventure CLI builder for displaying ephemeris.

Ephemlite

Photo by Tim Graf on Unsplash

As noted, I am quite fond of the Go port of astral. So, I threw together a small CLI project that is designed to be super hackable even if Go is not your primary (or tertiary) language. In fact, if you only care about sunrise and sunset, you don’t even need to modify it. Here’s what it does out of the box:

$ go install codeberg.org/hrbrmstr/ephemlite@latest
$ ephemlite emit -- 43.266998932 -70.856996572 55
{
  "coordinates": {
    "Latitude": 43.266998932,
    "Longitude": -70.856996572,
    "Elevation": 55
  },
  "sunrise": "2024-02-09T06:48:45.373187092-05:00",
  "sunset": "2024-02-09T17:07:02.97010512-05:00"
}

Let’s peek under the covers (there’s only one Go file: main.go):

package main

import (
    "encoding/json"
    "fmt"
    "os"
    "strconv"
    "time"

    "github.com/sj14/astral/pkg/astral"
    "github.com/spf13/cobra"
)

There are only two “foreign” imports, one for astral and the other for cobra a lovely command-line argument parser on steroids (I do appreciate the nostalgic nod to GIJoe, too). Unless you want to go bonkers on your own, you only need to start focusing on or about line 30. This is the main handler for the CLI app and it takes three positional arguments for latitude, longitude, and elevation.

HACKABLE OPTION : modify the app to take in a custom date (it uses today as a default with those time.Now() calls)

Inside the Run function for that command, we compute two of many ephemeris:

sunrise, err := astral.Sunrise(observer, time.Now())
sunset, err := astral.Sunset(observer, time.Now())

We store them in a struct:

type Ephemeris struct {
  Coordiantes astral.Observer `json:"coordinates"`
  Sunrise     time.Time       `json:"sunrise"`
  Sunset      time.Time       `json:"sunset"`
}

so we can emit them as JSON, as you saw, above.

HACKABLE OPTION : add calls to other ephemeris computations and modify the struct accordingly.

fmt.Println(string(jsonData))

This is so you can use something like gum — which we used in a previous WPE — with jq to build your own CLI tool with just bash/powershell.

HACKABLE OPTION : Do all the CLI work right in ephemlite, relying on, say, lipgloss or one of the other Charm components.

FIN

We’ll turn our eyes to the web in the next WPE.

Remember, you can follow and interact with the full text of The Daily Drop’s free posts on Mastodon via @dailydrop.hrbrmstr.dev@dailydrop.hrbrmstr.dev ☮️

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.