small pixel drawing of a pufferfish zoa

main.go

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"j3s.sh/zoa/env"
	"j3s.sh/zoa/git"
	"j3s.sh/zoa/shell"

	"mvdan.cc/sh/v3/interp"
)

const (
	zoaRoot = "/var/lib/zoa"
)

func main() {
	// git vars
	var branch string
	var entryPointDir string
	if os.Geteuid() != 0 {
		fmt.Println("! you are running zoa as a non-root user. not ideal tbh. !")
	}
	if len(os.Args) < 2 {
		printUsage()
	}
	if len(os.Args) > 2 {
		branch = os.Args[2]
	}

	path := os.Args[1]
	gitMode := git.GitMode(path)

	if gitMode {
		err := os.MkdirAll(zoaRoot, 0755)
		if err != nil {
			log.Fatal(err)
		}
		// check the path & branch, make sure it's correct
		// then clone
		git.Clone(path, branch)
		entryPointDir = filepath.Join(zoaRoot, "repo")
	} else {
		entryPointDir = path
	}

	// TODO: this writer is responsible for the random stdout
	// maybe save the stdout for debug mode somehow
	r, err := interp.New(interp.StdIO(nil, os.Stdout, os.Stderr))
	if err != nil {
		log.Fatal(err)
	}

	// set standard env vars for runtime
	r.Env, err = env.GenerateEnv()
	if err != nil {
		log.Fatal(err)
	}
	// for debuggin' fmt.Printf("%+v", r.Env)

	shell.RunCommands(entryPointDir, "main", r)
}

func printUsage() {
	// TODO: -v -h
	fmt.Println(`usage: zoa [git repo | fs path] [optional git branch]

details:
  zoa [git repo] [optional branch]
    zoa will clone the given repo+branch to /var/lib/zoa/repo and execute main.
    if the given repo/branch is already present, zoa will git fetch, then
    execute main.

  zoa [filesystem path]
    zoa will cd to the given path & execute the main script there
    (mostly used for development)

examples:
  zoa https://github.com/biox/zoa
  zoa https://github.com/biox/zoa dev
  zoa .`)
	os.Exit(1)
}