Skip to content

Commit

Permalink
refactor go
Browse files Browse the repository at this point in the history
  • Loading branch information
azahnen committed Oct 12, 2024
1 parent 624bdb1 commit 800c586
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 559 deletions.
10 changes: 10 additions & 0 deletions libxtracfg/c/include/libxtracfg.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@

#ifdef __cplusplus
extern "C" {

#endif
typedef void (*progress_callback) (const char* msg);

int xtracfg_init();

void xtracfg_cleanup();

void xtracfg_progress_subscribe(progress_callback progress_cb);

char* xtracfg_execute(const char* command, int* err);

#ifdef __cplusplus
}
#endif
56 changes: 41 additions & 15 deletions libxtracfg/c/wrapper/libxtracfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include "../include/libxtracfg.h"

char* xtracfg_execute(const char* command, int* err) {
JavaVM *jvm;
JNIEnv *env;
JavaVM *jvm = NULL;
JNIEnv *env = NULL;
progress_callback progress_cb = NULL;

// called from java with progress, pass to callback
JNIEXPORT void JNICALL Java_de_ii_xtraplatform_cli_Cli_00024NativeProgress_update
(JNIEnv *env2, jobject obj, jstring update) {
if (progress_cb != NULL) {
const char* msg = (*env)->GetStringUTFChars(env, update, NULL);

progress_cb(msg);
}
}

int xtracfg_init() {
JavaVMInitArgs vm_args;
JavaVMOption options[0];
vm_args.version = JNI_VERSION_10;
Expand All @@ -14,35 +27,48 @@ char* xtracfg_execute(const char* command, int* err) {
vm_args.ignoreUnrecognized = 0;

jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

if (JNI_OK != res) {
printf("ERR\n");
*err = 1;
return "Could not create JVM";
return 1;
}

return 0;
}

void xtracfg_cleanup() {
if (jvm != NULL) {
(*jvm)->DestroyJavaVM(jvm);
}
}

jclass cls = (*env)->FindClass(env, "de/ii/xtraplatform/cli/CommandHandler");
jmethodID cid = (*env)->GetMethodID(env, cls, "<init>", "()V");
jmethodID mid = (*env)->GetMethodID(env, cls, "handleCommand", "(Ljava/lang/String;)Ljava/lang/String;");
void xtracfg_progress_subscribe(progress_callback callback) {
progress_cb = callback;
}

//printf("JVM %d %d %d\n", cls, cid, mid);
char* xtracfg_execute(const char* command, int* err) {
if (jvm == NULL || env == NULL) {
printf("ERR\n");
*err = 1;
return "Not intialized";
}

jobject ch = (*env)->NewObject(env, cls, cid);
jclass cls = (*env)->FindClass(env, "de/ii/xtraplatform/cli/XtraCfg");
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "execute", "(Ljava/lang/String;)Ljava/lang/String;");
//printf("JVM %d %d\n", cls, mid);

jstring command2 = (*env)->NewStringUTF(env, command);

jstring result2 = (jstring)(*env)->CallObjectMethod(env, ch, mid, command2);
jstring result2 = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid, command2);

(*env)->DeleteLocalRef(env, command2);

const char* result = (*env)->GetStringUTFChars(env, result2, NULL);

char *result3 = malloc(strlen(result) + 1);
strcpy(result3,result);
//printf("%s", result3);

(*jvm)->DestroyJavaVM(jvm);
//printf("%s", result3);D");

*err = 0;
return result3;
}

3 changes: 3 additions & 0 deletions libxtracfg/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/interactive-instruments/xtraplatform-cli/libxtracfg/go

go 1.23.2
Empty file added libxtracfg/go/go.sum
Empty file.
67 changes: 44 additions & 23 deletions xtracfg/client/client.go → libxtracfg/go/xtracfg/client.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,63 @@
package client
package xtracfg

/*
#cgo CFLAGS: -I ../../c/include
#cgo LDFLAGS: -L../../c/build -lxtracfg -framework CoreServices -framework Foundation
#include <stdlib.h>
#include "libxtracfg.h"
void progress(char *msg);
*/
import "C"

import (
"encoding/json"
"fmt"
"path/filepath"
"strconv"
"strings"
"unsafe"
)

type CommandHandler func(command string) string

// Store is
type Store struct {
source *string
driver *string
verbose *bool
debug *bool
progress *chan string
Progress *chan string
}

var handle CommandHandler
var progress chan string
var progress_chan chan string

//export progress
func progress(msg *C.char) {
progress_chan <- C.GoString(msg)
}

func xtracfg_init() {
progress_chan = make(chan string, 16)
/*go func() {
for {
msg, more := <-progress_chan
if !more {
return
}
log.Println("PROGRESS", msg)
}
}()*/

C.xtracfg_init()
C.xtracfg_progress_subscribe((C.progress_callback)(unsafe.Pointer(C.progress)))
}

// New is
func New(source *string, driver *string, verbose *bool, debug *bool) *Store {
return &Store{source: source, driver: driver, debug: debug, verbose: verbose, progress: &progress}
}
xtracfg_init()

func Init(handle_command CommandHandler, progress_chan chan string) {
handle = handle_command
progress = progress_chan
return &Store{source: source, driver: driver, debug: debug, verbose: verbose, Progress: &progress_chan}
}

// Label is
Expand Down Expand Up @@ -69,7 +97,7 @@ func (store Store) Connect() error {

if err == nil {
if *store.verbose {
PrintResults(*response.Results, err)
//PrintResults(*response.Results, err)
// fmt.Printf("Connected to store source %s\n\n", store.Label())
}
return nil
Expand Down Expand Up @@ -134,21 +162,14 @@ func (store Store) Request(request []byte) (response []byte) {
fmt.Println("->", request2)
}

response = requestC(request2)
var err C.int
r := C.xtracfg_execute(C.CString(request2), &err)
response = []byte(C.GoString(r))
C.free(unsafe.Pointer(r))

if *store.debug {
fmt.Println("<-", string(response))
}

return response
}

func requestC(command string) (response []byte) {
if handle == nil {
handle = mockHandler
}

response = []byte(handle(command))

return response
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package xtracfg

import (
"encoding/json"
Expand Down
29 changes: 0 additions & 29 deletions xtracfg/client/mock.go

This file was deleted.

6 changes: 3 additions & 3 deletions xtracfg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/interactive-instruments/xtraplatform-cli/xtracfg/client"
"github.com/interactive-instruments/xtraplatform-cli/libxtracfg/go/xtracfg"
"github.com/interactive-instruments/xtraplatform-cli/xtracfg/cmd/store"

"github.com/spf13/cobra"
Expand All @@ -25,7 +25,7 @@ func version() string {
}

var name string = "xtracfg"
var storeSrc client.Store
var storeSrc xtracfg.Store

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -61,7 +61,7 @@ func init() {
RootCmd.PersistentFlags().MarkHidden("debug")
RootCmd.PersistentFlags().Bool("help", false, "show help")

storeSrc = *client.New(src, typ, verbose, debug)
storeSrc = *xtracfg.New(src, typ, verbose, debug)

infoCmd := store.InfoCmd(storeSrc, name, verbose, debug)

Expand Down
17 changes: 9 additions & 8 deletions xtracfg/cmd/store/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"strconv"
"strings"

"github.com/interactive-instruments/xtraplatform-cli/xtracfg/client"
"github.com/interactive-instruments/xtraplatform-cli/libxtracfg/go/xtracfg"
"github.com/interactive-instruments/xtraplatform-cli/xtracfg/util"
"github.com/spf13/cobra"
)

var ignoreRedundant *bool

// CheckCmd represents the check command
func CheckCmd(store client.Store, name string, verbose *bool, debug *bool) *cobra.Command {
func CheckCmd(store xtracfg.Store, name string, verbose *bool, debug *bool) *cobra.Command {
check := &cobra.Command{
Use: "check",
Short: "Check the store source",
Expand All @@ -27,7 +28,7 @@ Executes all subcommands in order, see the subcommand help for details.`,
}
results, err := store.Handle(map[string]interface{}{"ignoreRedundant": strconv.FormatBool(*ignoreRedundant)}, "check")

client.PrintResults(results, err)
util.PrintResults(results, err)

printFix(results, err, name)
},
Expand All @@ -50,7 +51,7 @@ Executes all subcommands in order, see the subcommand help for details.`,

results, err := store.Handle(map[string]interface{}{"ignoreRedundant": strconv.FormatBool(*ignoreRedundant)}, "check", "cfg")

client.PrintResults(results, err)
util.PrintResults(results, err)

printFix(results, err, name)
},
Expand Down Expand Up @@ -84,7 +85,7 @@ To check only a single entity, pass the path to the file relative to the source

results, err := store.Handle(map[string]interface{}{"ignoreRedundant": strconv.FormatBool(*ignoreRedundant), "path": path}, "check", "entities")

client.PrintResults(results, err)
util.PrintResults(results, err)

printFix(results, err, name)
},
Expand All @@ -102,7 +103,7 @@ To check only a single entity, pass the path to the file relative to the source

results, err := store.Handle(map[string]interface{}{"ignoreRedundant": strconv.FormatBool(*ignoreRedundant)}, "check", "layout")

client.PrintResults(results, err)
util.PrintResults(results, err)

printFix(results, err, name)
},
Expand All @@ -115,8 +116,8 @@ To check only a single entity, pass the path to the file relative to the source
return check
}

func printFix(results []client.Result, err error, name string) {
if err == nil && client.HasStatus(results, client.Warning) {
func printFix(results []xtracfg.Result, err error, name string) {
if err == nil && xtracfg.HasStatus(results, xtracfg.Warning) {
fmt.Fprint(os.Stdout, "\n", "Run '", name, " ", strings.Replace(strings.Join(os.Args[1:], " "), "check", "upgrade", 1), "' to fix all detected issues.", "\n")
} else {
fmt.Fprint(os.Stdout, "\n")
Expand Down
7 changes: 4 additions & 3 deletions xtracfg/cmd/store/info.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package store

import (
"github.com/interactive-instruments/xtraplatform-cli/xtracfg/client"
"github.com/interactive-instruments/xtraplatform-cli/libxtracfg/go/xtracfg"
"github.com/interactive-instruments/xtraplatform-cli/xtracfg/util"
"github.com/spf13/cobra"
)

// InfoCmd represents the check command
func InfoCmd(store client.Store, name string, verbose *bool, debug *bool) *cobra.Command {
func InfoCmd(store xtracfg.Store, name string, verbose *bool, debug *bool) *cobra.Command {
info := &cobra.Command{
Use: "info",
Short: "Print info about the store source",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
results, err := store.Handle(map[string]interface{}{}, "info")

client.PrintResults(results, err)
util.PrintResults(results, err)
},
}

Expand Down
7 changes: 4 additions & 3 deletions xtracfg/cmd/store/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package store
import (
"fmt"

"github.com/interactive-instruments/xtraplatform-cli/xtracfg/client"
"github.com/interactive-instruments/xtraplatform-cli/libxtracfg/go/xtracfg"
"github.com/interactive-instruments/xtraplatform-cli/xtracfg/util"
"github.com/spf13/cobra"
)

// InfoCmd represents the check command
func ListenCmd(store client.Store, name string, version string, verbose *bool, debug *bool) *cobra.Command {
func ListenCmd(store xtracfg.Store, name string, version string, verbose *bool, debug *bool) *cobra.Command {
listen := &cobra.Command{
Use: "listen [port]",
Short: "Listen for command on websocket",
Expand All @@ -20,7 +21,7 @@ func ListenCmd(store client.Store, name string, version string, verbose *bool, d
port = ":" + args[0]
}
fmt.Printf("%s (%s) listening on port %s\n", name, version, port)
client.OpenWebsocket(store, port)
util.OpenWebsocket(store, port)
},
}

Expand Down
Loading

0 comments on commit 800c586

Please sign in to comment.