small pixel drawing of a pufferfish zoa

main.go

package main

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

	"j3s.sh/zoa/git"
	"j3s.sh/zoa/shell"
	"j3s.sh/zoa/utils"
)

func main() {
	// git vars
	var branch 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 {
		utils.SetZoaRoot("/var/lib/zoa/repo")
		err := os.MkdirAll(utils.ZoaRoot, 0755)
		if err != nil {
			log.Fatal(err)
		}
		// check the path & branch, make sure it's correct
		// then clone
		git.Clone(path, branch)
	} else {
		utils.SetZoaRoot(path)
	}

	// TODO: this writer is responsible for the random stdout
	// maybe save the stdout for debug mode somehow

	main := filepath.Join(utils.ZoaRoot, "main")
	shell.RunScript(main)
}

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)
}