Skip to content

Commit

Permalink
Merge pull request #8 from Morganamilo/question
Browse files Browse the repository at this point in the history
Add support for question callback
  • Loading branch information
Jguer authored Mar 16, 2018
2 parents 946a37d + df56af8 commit c2766b2
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 11 deletions.
10 changes: 9 additions & 1 deletion callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <alpm.h>

void logCallback(uint16_t level, char *cstring);
void questionCallback(alpm_question_t *question);

void go_alpm_log_cb(alpm_loglevel_t level, const char *fmt, va_list arg) {
char *s = malloc(128);
Expand All @@ -25,7 +26,14 @@ void go_alpm_log_cb(alpm_loglevel_t level, const char *fmt, va_list arg) {
}
}

void go_alpm_question_cb(alpm_question_t *question) {
questionCallback(question);
}

void go_alpm_set_logging(alpm_handle_t *handle) {
alpm_option_set_logcb(handle, go_alpm_log_cb);
alpm_option_set_logcb(handle, go_alpm_log_cb);
}

void go_alpm_set_question(alpm_handle_t *handle) {
alpm_option_set_questioncb(handle, go_alpm_question_cb);
}
24 changes: 22 additions & 2 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ package alpm
void logCallback(uint16_t level, char *cstring);
void go_alpm_log_cb(alpm_loglevel_t level, const char *fmt, va_list arg);
void go_alpm_set_logging(alpm_handle_t *handle);
void go_alpm_set_question(alpm_handle_t *handle);
*/
import "C"

import (
"unsafe"
)

type logCallbackSig func(uint16, string)
type questionCallbackSig func(QuestionAny)

var DefaultLogLevel = LogWarning

func DefaultLogCallback(lvl uint16, s string) {
Expand All @@ -23,14 +31,26 @@ func DefaultLogCallback(lvl uint16, s string) {
}
}

var log_callback = DefaultLogCallback
var log_callback logCallbackSig
var question_callback questionCallbackSig

//export logCallback
func logCallback(level uint16, cstring *C.char) {
log_callback(level, C.GoString(cstring))
}

func (h *Handle) SetLogCallback(cb func(uint16, string)) {
//export questionCallback
func questionCallback(question *C.alpm_question_t) {
q := (*C.alpm_question_any_t)(unsafe.Pointer(question))
question_callback(QuestionAny{q})
}

func (h *Handle) SetLogCallback(cb logCallbackSig) {
log_callback = cb
C.go_alpm_set_logging(h.ptr)
}

func (h *Handle) SetQuestionCallback(cb questionCallbackSig) {
question_callback = cb
C.go_alpm_set_question(h.ptr)
}
16 changes: 8 additions & 8 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ const (
LogFunction
)

type Question uint
type QuestionType uint

const (
QuestionInstallIgnorepkg Question = 1 << iota
QuestionReplacePkg
QuestionConflictPkg
QuestionCorruptedPkg
QuestionRemovePkgs
QuestionSelectProvider
QuestionImportKey
QuestionTypeInstallIgnorepkg QuestionType = 1 << iota
QuestionTypeReplacePkg
QuestionTypeConflictPkg
QuestionTypeCorruptedPkg
QuestionTypeRemovePkgs
QuestionTypeSelectProvider
QuestionTypeImportKey
)

type Validation int
Expand Down
97 changes: 97 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "C"
import (
"reflect"
"unsafe"
"fmt"
)

// Description of a dependency.
Expand Down Expand Up @@ -150,3 +151,99 @@ func (l BackupList) Slice() (slice []BackupFile) {
})
return
}

type QuestionAny struct {
ptr *C.alpm_question_any_t
}

func (question QuestionAny) SetAnswer(answer bool) {
if answer {
question.ptr.answer = 1
} else {
question.ptr.answer = 0
}
}

type QuestionInstallIgnorepkg struct {
ptr *C.alpm_question_install_ignorepkg_t
}

func (question QuestionAny) Type() QuestionType{
return QuestionType(question.ptr._type)
}

func (question QuestionAny) Answer() bool {
return question.ptr.answer == 1
}

func (question QuestionAny) QuestionInstallIgnorepkg() (QuestionInstallIgnorepkg, error) {
if question.Type() == QuestionTypeInstallIgnorepkg {
return *(*QuestionInstallIgnorepkg)(unsafe.Pointer(&question)), nil
}

return QuestionInstallIgnorepkg{}, fmt.Errorf("Can not convert to QuestionInstallIgnorepkg")
}

func (question QuestionInstallIgnorepkg) SetInstall(install bool) {
if install {
question.ptr.install = 1
} else {
question.ptr.install = 0
}
}

func (question QuestionInstallIgnorepkg) Type() QuestionType {
return QuestionType(question.ptr._type)
}

func (question QuestionInstallIgnorepkg) Install() bool {
return question.ptr.install == 1
}

func (question QuestionInstallIgnorepkg) Pkg(h *Handle) Package {
return Package {
question.ptr.pkg,
*h,
}
}

type QuestionReplace struct {
ptr *C.alpm_question_replace_t
}

func (question QuestionReplace) Type() QuestionType {
return QuestionType(question.ptr._type)
}

func (question QuestionReplace) SetReplace(replace bool) {
if replace {
question.ptr.replace = 1
} else {
question.ptr.replace = 0
}
}

func (question QuestionReplace) Replace() bool {
return question.ptr.replace == 1
}

func (question QuestionReplace) NewPkg(h *Handle) Package {
return Package {
question.ptr.newpkg,
*h,
}
}

func (question QuestionReplace) OldPkg(h *Handle) Package {
return Package {
question.ptr.oldpkg,
*h,
}
}

func (question QuestionReplace) newDb(h *Handle) Db {
return Db {
question.ptr.newdb,
*h,
}
}

0 comments on commit c2766b2

Please sign in to comment.