diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..8802dd7 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,49 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/go . +{ + "name": "Go", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/go:2-1.25-trixie", + + // Features to add to the dev container. More info: https://containers.dev/features. + "features": { + "ghcr.io/devcontainers/features/go:1": {}, + "ghcr.io/guiyomh/features/golangci-lint:0": {}, + "ghcr.io/guiyomh/features/goreleaser:0": {} + }, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Configure tool-specific properties. + "customizations": { + "vscode": { + "extensions": [ + "openai.chatgpt", + "golang.Go" + ], + "settings": { + // Slightly different dark theme to highlight the DevContainer + "workbench.colorTheme": "Default Dark Modern", + "workbench.colorCustomizations": { + "statusBar.background": "#005f73", + "statusBar.noFolderBackground": "#005f73", + "statusBar.debuggingBackground": "#9b2226", + "statusBar.foreground": "#ffffff" + } + } + } + }, + + // Preserve the packages configurations within the container on a host volume: git, codex-extension. + "mounts": [ + "source=codex-session,target=/home/vscode/.codex,type=volume", + "source=gitconfig,target=/home/vscode/.gitconfig-volume,type=volume" + ], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "./.devcontainer/setup.sh", + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + "remoteUser": "vscode" +} diff --git a/.devcontainer/setup.sh b/.devcontainer/setup.sh new file mode 100644 index 0000000..5f54b56 --- /dev/null +++ b/.devcontainer/setup.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -euo pipefail + +if ! command -v task >/dev/null; then + echo "Installing Taskfile CLI helper..." >&2 + curl -1sLf 'https://dl.cloudsmith.io/public/task/task/setup.deb.sh' | sudo -E bash + sudo apt install -y task +fi + +sudo chown -R ${UID}:${UID} ${HOME}/.codex + +# Fix project file permissions to standard privileges +sudo chown -R ${UID}:${UID} ${PWD} +sudo find ${PWD} -type d -exec chmod 755 {} + +sudo find ${PWD} -type f ! -name "*.sh" -exec chmod 644 {} + +sudo find ${PWD} -type f -name "*.sh" -exec chmod 755 {} + + +# Setup git files configurations +sudo chown -R ${UID}:${UID} ${HOME}/.gitconfig-volume + +touch ${HOME}/.gitconfig-volume/config +ln -fs ${HOME}/.gitconfig-volume/config ${HOME}/.gitconfig +sudo chown -R ${UID}:${UID} ${HOME}/.gitconfig + +touch ${HOME}/.gitconfig-volume/.git-credentials +ln -fs ${HOME}/.gitconfig-volume/.git-credentials ${HOME}/.git-credentials +sudo chown -R ${UID}:${UID} ${HOME}/.git-credentials + +git config --global --add safe.directory ${PWD} diff --git a/.gitignore b/.gitignore index 7cbe6c2..e35a94a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ -hapi \ No newline at end of file +hapi +bin/ +dist/ \ No newline at end of file diff --git a/README.md b/README.md index 8520cc6..01be951 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,32 @@ hapi vps vm list See the [full reference documentation](https://github.com/hostinger/api-cli/blob/main/docs/hapi.md) for information about each available command. +# Building from source + +If you need to compile `hapi` yourself, the repository ships with a `Taskfile.yml` that automates downloading dependencies, cross-building every supported platform and packaging the binaries. + +Inside the repository, install [`Task CLI`](https://taskfile.dev) or use the [devcontainer](https://containers.dev) terminal, then run: + +```bash +task clean # remove previous artifacts +task release # builds linux/darwin/windows binaries +``` + +Each tarball is created under `dist/` with the pattern `hapi---.tar.gz`, and the native host binary lands at `dist/host/-/hapi`. + +You can also build a single target manually if you prefer: +```bash +# a binary for your device +go build -ldflags="-s -w" -o hapi main.go +``` + +```bash +# or for specific arch +GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o hapi main.go +``` + +Finally, copy the resulting binary into your `$PATH` or package it for the target device. + # Enabling shell auto-completion (optional) `hapi` has auto-complete support. This makes it easier to use the CLI and improves user experience by completing command @@ -59,4 +85,4 @@ Auto-completion can be generated for multiple shells. The currently supported sh After adding shell auto-completion, remember to refresh your shell profile by logging out from the shell and log back in. -Read more on [how to enable auto-completion](https://github.com/hostinger/api-cli/blob/main/AUTOCOMPLETE.md). \ No newline at end of file +Read more on [how to enable auto-completion](https://github.com/hostinger/api-cli/blob/main/AUTOCOMPLETE.md). diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..9dd963a --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,157 @@ +version: '3' + +env: + DIST_DIR: dist + BINARY: hapi + LDFLAGS: -s -w + +tasks: + default: + desc: List the available tasks without executing any of them. + silent: true + cmds: + - task --list + + clean: + desc: Remove previously generated artifacts. + cmds: + - rm -rf ${DIST_DIR} + + ensure: + desc: Download Go modules before building. + cmds: + - go mod download + + build: + desc: Build the CLI for the host platform. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/host + - go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/host/${BINARY} main.go + + build.linux-amd64: + desc: Build Linux amd64 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/linux-amd64 + - GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/linux-amd64/${BINARY} main.go + + build.linux-arm64: + desc: Build Linux arm64 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/linux-arm64 + - GOOS=linux GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/linux-arm64/${BINARY} main.go + + build.darwin-amd64: + desc: Build macOS amd64 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/darwin-amd64 + - GOOS=darwin GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/darwin-amd64/${BINARY} main.go + + build.darwin-arm64: + desc: Build macOS arm64 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/darwin-arm64 + - GOOS=darwin GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/darwin-arm64/${BINARY} main.go + + build.linux-386: + desc: Build Linux 386 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/linux-386 + - GOOS=linux GOARCH=386 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/linux-386/${BINARY} main.go + + build.windows-386: + desc: Build Windows 386 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/windows-386 + - GOOS=windows GOARCH=386 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/windows-386/${BINARY}.exe main.go + + build.windows-amd64: + desc: Build Windows amd64 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/windows-amd64 + - GOOS=windows GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/windows-amd64/${BINARY}.exe main.go + + build.windows-arm64: + desc: Build Windows arm64 binary. + deps: [ensure] + cmds: + - mkdir -p ${DIST_DIR}/windows-arm64 + - GOOS=windows GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o ${DIST_DIR}/windows-arm64/${BINARY}.exe main.go + + package.linux-amd64: + desc: Package the Linux amd64 binary into a tarball. + deps: [build.linux-amd64] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-linux-amd64.tar.gz -C ${DIST_DIR}/linux-amd64 ${BINARY}' + + package.linux-arm64: + desc: Package the Linux arm64 binary into a tarball. + deps: [build.linux-arm64] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-linux-arm64.tar.gz -C ${DIST_DIR}/linux-arm64 ${BINARY}' + + package.darwin-arm64: + desc: Package the macOS arm64 binary into a tarball. + deps: [build.darwin-arm64] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-darwin-arm64.tar.gz -C ${DIST_DIR}/darwin-arm64 ${BINARY}' + + package.darwin-amd64: + desc: Package the macOS amd64 binary into a tarball. + deps: [build.darwin-amd64] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-darwin-amd64.tar.gz -C ${DIST_DIR}/darwin-amd64 ${BINARY}' + + package.linux-386: + desc: Package the Linux 386 binary into a tarball. + deps: [build.linux-386] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-linux-386.tar.gz -C ${DIST_DIR}/linux-386 ${BINARY}' + + package.windows-386: + desc: Package the Windows 386 binary into a tarball. + deps: [build.windows-386] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-windows-386.tar.gz -C ${DIST_DIR}/windows-386 ${BINARY}.exe' + + package.windows-amd64: + desc: Package the Windows amd64 binary into a tarball. + deps: [build.windows-amd64] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-windows-amd64.tar.gz -C ${DIST_DIR}/windows-amd64 ${BINARY}.exe' + + package.windows-arm64: + desc: Package the Windows arm64 binary into a tarball. + deps: [build.windows-arm64] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); tar czf ${DIST_DIR}/${BINARY}-$version-windows-arm64.tar.gz -C ${DIST_DIR}/windows-arm64 ${BINARY}.exe' + + packages: + desc: Package every platform bundle. + deps: + - package.darwin-amd64 + - package.darwin-arm64 + - package.linux-386 + - package.linux-amd64 + - package.linux-arm64 + - package.windows-386 + - package.windows-amd64 + - package.windows-arm64 + + package.checksums: + desc: Create SHA256 checksums for every release tarball. + deps: [packages] + cmds: + - sh -c 'version=$(git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD); cd ${DIST_DIR}; sha256sum ${BINARY}-$version-*.tar.gz > ${BINARY}-$version-checksums.sha256' + + release: + desc: Build and package every target used for releases. + deps: + - package.checksums