Skip to content

Commit

Permalink
cmd: add additional CLI entrypoint and build separately
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Nov 23, 2024
1 parent 02ed4a3 commit ed8a66f
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 39 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ data/
Dockerfile
internal/resources/blocky
internal/resources/macoui
bin/**
25 changes: 25 additions & 0 deletions .github/workflows/build-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
build:
name: Build
runs-on: ubuntu-latest
outputs:
release_upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
Expand All @@ -37,7 +39,10 @@ jobs:
build-args: |
BUILD=${{ steps.vars.outputs.build }}
GIT_BUILD_HASH=${{ steps.vars.outputs.sha }}
- run: |
make bin/gravity-cli
- uses: actions/[email protected]
id: create_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand All @@ -63,3 +68,23 @@ jobs:
```
go get beryju.io/gravity@v${{ steps.vars.outputs.version }}
```
build-cli:
name: Build CLI
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
arch: [arm64, amd64]
os: [darwin, linux]
steps:
- uses: actions/checkout@v4
- run: |
GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} make bin/gravity-cli
- uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.build.outputs.release_upload_url }}
asset_path: ./bin/gravity-cli
asset_name: gravity-cli_${{ matrix.os }}_${{ matrix.arch }}
asset_content_type: application/octet-stream
11 changes: 11 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ jobs:
BUILD=${{ steps.vars.outputs.build }}
GIT_BUILD_HASH=${{ steps.vars.outputs.sha }}
CC_GH_COMMIT_SHA=${{ github.event.pull_request.head.sha }}
build-cli:
name: Build CLI
runs-on: ubuntu-latest
strategy:
matrix:
arch: [arm64, amd64]
os: [darwin, linux]
steps:
- uses: actions/checkout@v4
- run: |
GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} make bin/gravity-cli
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage.html
*.sock
tests.json
test-output
bin/**
37 changes: 22 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ ci--env:
docker-build: internal/resources/macoui internal/resources/blocky internal/resources/tftp
go build \
-ldflags "${LD_FLAGS} -X beryju.io/gravity/pkg/extconfig.BuildHash=${GIT_BUILD_HASH}" \
-v -a -o gravity .
-v -a -o gravity ${PWD}

clean:
rm -rf ${PWD}/data/
rm -rf ${PWD}/bin/

run: internal/resources/macoui internal/resources/blocky internal/resources/tftp
export INSTANCE_LISTEN=0.0.0.0
export DEBUG=true
export LISTEN_ONLY=true
$(eval LD_FLAGS := -X beryju.io/gravity/pkg/extconfig.Version=${VERSION} -X beryju.io/gravity/pkg/extconfig.BuildHash=dev-$(shell git rev-parse HEAD))
go run ${GO_FLAGS} . server
go run ${GO_FLAGS} ${PWD} server

# Web
web-install:
Expand All @@ -51,33 +52,39 @@ web-lint:
npm run lint
npm run lit-analyse

# CLI
bin/gravity-cli:
$(eval LD_FLAGS := -X beryju.io/gravity/pkg/extconfig.Version=${VERSION} -X beryju.io/gravity/pkg/extconfig.BuildHash=dev-$(shell git rev-parse HEAD))
mkdir -p ${PWD}/bin/
go build ${GO_FLAGS} -o ${PWD}/bin/gravity-cli ${PWD}/cmd/cli/main/

# Website
website-watch:
cd ${PWD}/docs
open http://localhost:1313/ && hugo server --noBuildLock

internal/resources/macoui:
mkdir -p internal/resources/macoui
curl -L https://raw.githubusercontent.com/wireshark/wireshark/6885d787fda5f74a2d1f9eeea443fecf8dd58528/manuf -o ./internal/resources/macoui/db.txt
curl -L https://raw.githubusercontent.com/wireshark/wireshark/6885d787fda5f74a2d1f9eeea443fecf8dd58528/manuf -o ${PWD}/internal/resources/macoui/db.txt

internal/resources/blocky:
mkdir -p internal/resources/blocky
curl -L https://adaway.org/hosts.txt -o ./internal/resources/blocky/adaway.org.txt
curl -L https://big.oisd.nl/domainswild -o ./internal/resources/blocky/big.oisd.nl.txt
curl -L https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -o ./internal/resources/blocky/StevenBlack.hosts.txt
curl -L https://v.firebog.net/hosts/AdguardDNS.txt -o ./internal/resources/blocky/AdguardDNS.txt
curl -L https://v.firebog.net/hosts/Easylist.txt -o ./internal/resources/blocky/Easylist.txt
curl -L https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt -o ./internal/resources/blocky/AdGuardSDNSFilter.txt
curl -L https://adaway.org/hosts.txt -o ${PWD}/internal/resources/blocky/adaway.org.txt
curl -L https://big.oisd.nl/domainswild -o ${PWD}/internal/resources/blocky/big.oisd.nl.txt
curl -L https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -o ${PWD}/internal/resources/blocky/StevenBlack.hosts.txt
curl -L https://v.firebog.net/hosts/AdguardDNS.txt -o ${PWD}/internal/resources/blocky/AdguardDNS.txt
curl -L https://v.firebog.net/hosts/Easylist.txt -o ${PWD}/internal/resources/blocky/Easylist.txt
curl -L https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt -o ${PWD}/internal/resources/blocky/AdGuardSDNSFilter.txt

internal/resources/tftp:
mkdir -p internal/resources/tftp
curl -L http://boot.ipxe.org/undionly.kpxe -o ./internal/resources/tftp/ipxe.undionly.kpxe
curl -L https://boot.netboot.xyz/ipxe/netboot.xyz.kpxe -o ./internal/resources/tftp/netboot.xyz.kpxe
curl -L https://boot.netboot.xyz/ipxe/netboot.xyz-undionly.kpxe -o ./internal/resources/tftp/netboot.xyz-undionly.kpxe
curl -L https://boot.netboot.xyz/ipxe/netboot.xyz.efi -o ./internal/resources/tftp/netboot.xyz.efi
curl -L http://boot.ipxe.org/undionly.kpxe -o ${PWD}/internal/resources/tftp/ipxe.undionly.kpxe
curl -L https://boot.netboot.xyz/ipxe/netboot.xyz.kpxe -o ${PWD}/internal/resources/tftp/netboot.xyz.kpxe
curl -L https://boot.netboot.xyz/ipxe/netboot.xyz-undionly.kpxe -o ${PWD}/internal/resources/tftp/netboot.xyz-undionly.kpxe
curl -L https://boot.netboot.xyz/ipxe/netboot.xyz.efi -o ${PWD}/internal/resources/tftp/netboot.xyz.efi

gen-build:
DEBUG=true go run ${GO_FLAGS} . generateSchema ${SCHEMA_FILE}
DEBUG=true go run ${GO_FLAGS} ${PWD} generateSchema ${SCHEMA_FILE}
git add ${SCHEMA_FILE}

gen-clean:
Expand Down Expand Up @@ -162,7 +169,7 @@ test: internal/resources/macoui internal/resources/blocky internal/resources/tft
export ETCD_ENDPOINT="localhost:2385"
export DEBUG="true"
export LISTEN_ONLY="true"
go run -v . cli etcdctl del --prefix /
go run -v ${PWD} cli etcdctl del --prefix /
go test \
-p 1 \
-coverprofile=coverage.txt \
Expand Down
9 changes: 4 additions & 5 deletions cmd/cli.go → cmd/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"context"
Expand All @@ -25,7 +25,7 @@ var (
logger *zap.Logger
)

var cliCmd = &cobra.Command{
var CLICmd = &cobra.Command{
Use: "cli",
Short: "Interact with a running Gravity server",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -66,9 +66,8 @@ func init() {
defUrl = fmt.Sprintf("unix://%s/gravity.sock", cwd)
}
logger = extconfig.Get().Logger().Named("cli")
cliCmd.PersistentFlags().StringVarP(&apiUrl, "host", "s", defUrl, "API Host")
cliCmd.PersistentFlags().StringVarP(&apiToken, "token", "t", "", "API Token")
rootCmd.AddCommand(cliCmd)
CLICmd.PersistentFlags().StringVarP(&apiUrl, "host", "s", defUrl, "API Host")
CLICmd.PersistentFlags().StringVarP(&apiToken, "token", "t", "", "API Token")
}

func checkApiError(hr *http.Response, err error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli_convert.go → cmd/cli/cli_convert.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import "github.com/spf13/cobra"

Expand All @@ -8,5 +8,5 @@ var cliConvertCmd = &cobra.Command{
}

func init() {
cliCmd.AddCommand(cliConvertCmd)
CLICmd.AddCommand(cliConvertCmd)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli_etcdctl.go → cmd/cli/cli_etcdctl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
_ "unsafe"
Expand All @@ -17,5 +17,5 @@ var globalFlags command.GlobalFlags

func init() {
globalFlags.Endpoints = []string{extconfig.Get().Etcd.Endpoint}
cliCmd.AddCommand(etcdctlCommand)
CLICmd.AddCommand(etcdctlCommand)
}
4 changes: 2 additions & 2 deletions cmd/cli_export.go → cmd/cli/cli_export.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"encoding/json"
Expand Down Expand Up @@ -43,5 +43,5 @@ var exportCmd = &cobra.Command{

func init() {
exportCmd.Flags().BoolVar(&exportSafe, "safe", false, "Export only safe values")
cliCmd.AddCommand(exportCmd)
CLICmd.AddCommand(exportCmd)
}
4 changes: 2 additions & 2 deletions cmd/cli_health.go → cmd/cli/cli_health.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"os"
Expand All @@ -20,5 +20,5 @@ var healthCmd = &cobra.Command{
}

func init() {
cliCmd.AddCommand(healthCmd)
CLICmd.AddCommand(healthCmd)
}
4 changes: 2 additions & 2 deletions cmd/cli_import.go → cmd/cli/cli_import.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"encoding/json"
Expand Down Expand Up @@ -36,5 +36,5 @@ var importCmd = &cobra.Command{
}

func init() {
cliCmd.AddCommand(importCmd)
CLICmd.AddCommand(importCmd)
}
4 changes: 2 additions & 2 deletions cmd/cli_snapshot.go → cmd/cli/cli_snapshot.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"github.com/spf13/cobra"
Expand All @@ -14,5 +14,5 @@ func init() {
if err != nil {
panic(err)
}
cliCmd.AddCommand(sc)
CLICmd.AddCommand(sc)
}
2 changes: 1 addition & 1 deletion cmd/cli_token_add.go → cmd/cli/cli_token_add.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli_tokens.go → cmd/cli/cli_tokens.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import "github.com/spf13/cobra"

Expand All @@ -8,5 +8,5 @@ var cliTokensCmd = &cobra.Command{
}

func init() {
cliCmd.AddCommand(cliTokensCmd)
CLICmd.AddCommand(cliTokensCmd)
}
4 changes: 2 additions & 2 deletions cmd/cli_users.go → cmd/cli/cli_users.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import "github.com/spf13/cobra"

Expand All @@ -8,5 +8,5 @@ var cliUsersCmd = &cobra.Command{
}

func init() {
cliCmd.AddCommand(cliUsersCmd)
CLICmd.AddCommand(cliUsersCmd)
}
2 changes: 1 addition & 1 deletion cmd/cli_users_add.go → cmd/cli/cli_users_add.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"fmt"
Expand Down
8 changes: 8 additions & 0 deletions cmd/cli/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "beryju.io/gravity/cmd/cli"

func main() {
cli.CLICmd.Use = "gravity-cli"
cli.CLICmd.Execute()

Check failure on line 7 in cmd/cli/main/main.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `cli.CLICmd.Execute` is not checked (errcheck)
}
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"

"beryju.io/gravity/cmd/cli"
"beryju.io/gravity/pkg/extconfig"
"github.com/spf13/cobra"
)
Expand All @@ -13,6 +14,10 @@ var rootCmd = &cobra.Command{
Version: extconfig.FullVersion(),
}

func init() {
rootCmd.AddCommand(cli.CLICmd)
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand Down

0 comments on commit ed8a66f

Please sign in to comment.