Skip to content

Commit

Permalink
feat: refactor and use kong for cli command parsing (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
connerdouglass authored Jun 25, 2024
1 parent b86d271 commit 2e54555
Show file tree
Hide file tree
Showing 34 changed files with 405 additions and 433 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions:
contents: write

jobs:
goreleaser:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -23,11 +23,15 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'

- uses: actions/setup-node@v4
with:
go-version: 1.17
node-version: lts

- name: Run GoReleaser
- name: Build Go Binaries
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
Expand All @@ -36,10 +40,6 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/setup-node@v1
with:
node-version: 10

- name: Update version in package.json
uses: actions/github-script@v6
with:
Expand All @@ -50,7 +50,7 @@ jobs:
packageJson.version = '${{ steps.get_release.outputs.tag_name }}'.replace(/^v/, '');
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2));
- run: npm install
- run: npm ci

- uses: JS-DevTools/npm-publish@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ builds:
- linux
- windows
- darwin
main: ./cmd/crx/main.go
main: ./cmd/main.go
binary: crx
ldflags:
- -s -w -X github.com/customrealms/cli/pkg/version.Version={{.Version}}
Expand Down
55 changes: 55 additions & 0 deletions cmd/cmd_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"os"

"github.com/customrealms/cli/internal/actions/build"
"github.com/customrealms/cli/internal/project"
)

type BuildCmd struct {
ProjectDir string `name:"project" short:"p" usage:"plugin project directory" optional:""`
McVersion string `name:"mc" usage:"Minecraft version number target" optional:""`
TemplateJarFile string `name:"jar" short:"t" usage:"template JAR file" optional:""`
OutputFile string `name:"output" short:"o" usage:"output JAR file path"`
}

func (c *BuildCmd) Run() error {
// Root context for the CLI
ctx, cancel := rootContext()
defer cancel()

// Default to the current working directory
if c.ProjectDir == "" {
c.ProjectDir, _ = os.Getwd()
}

// Get the Minecraft version
minecraftVersion := mustMinecraftVersion(ctx, c.McVersion)

// Create the JAR template to build with
var jarTemplate build.JarTemplate
if len(c.TemplateJarFile) > 0 {
jarTemplate = &build.FileJarTemplate{
Filename: c.TemplateJarFile,
}
} else {
jarTemplate = &build.GitHubJarTemplate{
MinecraftVersion: minecraftVersion,
}
}

// Create the project
crProject := project.Project{
Dir: c.ProjectDir,
}

// Create the build action
buildAction := build.BuildAction{
Project: &crProject,
JarTemplate: jarTemplate,
MinecraftVersion: minecraftVersion,
OutputFile: c.OutputFile,
}
return buildAction.Run(ctx)
}
31 changes: 31 additions & 0 deletions cmd/cmd_init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"os"
"path/filepath"

"github.com/customrealms/cli/internal/actions/initialize"
)

type InitCmd struct {
ProjectDir string `name:"project" short:"p" usage:"plugin project directory" optional:""`
}

func (c *InitCmd) Run() error {
// Root context for the CLI
ctx, cancel := rootContext()
defer cancel()

// Default to the current working directory
if c.ProjectDir == "" {
c.ProjectDir, _ = os.Getwd()
}

// Create the init runner
initAction := initialize.InitAction{
Name: filepath.Base(c.ProjectDir),
Dir: c.ProjectDir,
Template: nil,
}
return initAction.Run(ctx)
}
80 changes: 80 additions & 0 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"os"

"github.com/customrealms/cli/internal/actions/build"
"github.com/customrealms/cli/internal/actions/serve"
"github.com/customrealms/cli/internal/project"
"github.com/customrealms/cli/internal/server"
)

type RunCmd struct {
ProjectDir string `name:"project" short:"p" usage:"plugin project directory" optional:""`
McVersion string `name:"mc" short:"mc" usage:"Minecraft version number target" optional:""`
TemplateJarFile string `name:"jar" short:"t" usage:"template JAR file" optional:""`
}

func (c *RunCmd) Run() error {
// Root context for the CLI
ctx, cancel := rootContext()
defer cancel()

// Default to the current working directory
if c.ProjectDir == "" {
c.ProjectDir, _ = os.Getwd()
}

// Get the Minecraft version
minecraftVersion := mustMinecraftVersion(ctx, c.McVersion)

// Generate a temp filename for the plugin JAR file
ofile, _ := os.CreateTemp("", "cr-jar-output-*.jar")
ofile.Close()
outputFile := ofile.Name()
defer os.Remove(outputFile)

// Create the JAR template to build with
var jarTemplate build.JarTemplate
if len(c.TemplateJarFile) > 0 {
jarTemplate = &build.FileJarTemplate{
Filename: c.TemplateJarFile,
}
} else {
jarTemplate = &build.GitHubJarTemplate{
MinecraftVersion: minecraftVersion,
}
}

// Create the project
crProject := project.Project{
Dir: c.ProjectDir,
}

// Create the build action
buildAction := build.BuildAction{
Project: &crProject,
JarTemplate: jarTemplate,
MinecraftVersion: minecraftVersion,
OutputFile: outputFile,
}

// Run the build action
if err := buildAction.Run(ctx); err != nil {
return err
}

// Create a fetcher for the Minecraft server JAR file that caches the files locally
serverJarFetcher, err := server.NewCachedFetcher(&server.HttpFetcher{})
if err != nil {
return err
}

// Create the serve runner
serveAction := serve.ServeAction{
MinecraftVersion: minecraftVersion,
PluginJarPath: outputFile,
ServerJarFetcher: serverJarFetcher,
}
return serveAction.Run(ctx)
}
14 changes: 14 additions & 0 deletions cmd/cmd_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"

"github.com/customrealms/cli/pkg/version"
)

type VersionCmd struct{}

func (c *VersionCmd) Run() error {
fmt.Printf("@customrealms/cli (crx) v%s\n", version.Version)
return nil
}
48 changes: 48 additions & 0 deletions cmd/cmd_yml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"os"

"github.com/customrealms/cli/internal/actions/build"
"github.com/customrealms/cli/internal/project"
)

type YmlCmd struct {
ProjectDir string `name:"project" short:"p" usage:"plugin project directory" optional:""`
McVersion string `name:"mc" short:"mc" usage:"Minecraft version number target" optional:""`
}

func (c *YmlCmd) Run() error {
// Root context for the CLI
ctx, cancel := rootContext()
defer cancel()

// Default to the current working directory
if c.ProjectDir == "" {
c.ProjectDir, _ = os.Getwd()
}

// Get the Minecraft version
minecraftVersion := mustMinecraftVersion(ctx, c.McVersion)

// Create the project
crProject := project.Project{
Dir: c.ProjectDir,
}

// Read the package.json file
packageJson, err := crProject.PackageJSON()
if err != nil {
return err
}

// Define the plugin.yml details for the plugin
pluginYml := &build.PluginYml{
MinecraftVersion: minecraftVersion,
PackageJSON: packageJson,
}
fmt.Println(pluginYml)

return nil
}
Loading

0 comments on commit 2e54555

Please sign in to comment.