From e3508eee23e7ccd6499cc786f29cb2d208d55936 Mon Sep 17 00:00:00 2001 From: "Debucquoy Anthony (tonitch)" Date: Sun, 4 Jun 2023 12:35:42 +0200 Subject: [PATCH] Refactoring in C, currently in development --- CMakeLists.txt | 12 ------------ build.sh | 8 ++++++++ giteaAPI.c | 41 +++++++++++++++++++++++++++++++++++++++++ giteaAPI.h | 19 +++++++++++++++++++ main.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 3 --- src/main.cpp | 7 ------- 7 files changed, 108 insertions(+), 22 deletions(-) delete mode 100644 CMakeLists.txt create mode 100755 build.sh create mode 100644 giteaAPI.c create mode 100644 giteaAPI.h create mode 100644 main.c delete mode 100644 src/CMakeLists.txt delete mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 2555e51..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -cmake_minimum_required(VERSION 3.15) - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED On) - -set(CMAKE_EXPORT_COMPILE_COMMANDS On) - -project(teaui) - -add_executable(teaui) - -add_subdirectory(src) diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..6631a54 --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -xe + +CFLAGS=$(pkg-config --cflags ncurses libcurl) +LIBS=$(pkg-config --libs ncurses libcurl) + +gcc ${CFLAGS} -o main main.c giteaAPI.c ${LIBS} diff --git a/giteaAPI.c b/giteaAPI.c new file mode 100644 index 0000000..e3834d7 --- /dev/null +++ b/giteaAPI.c @@ -0,0 +1,41 @@ +#include "giteaAPI.h" +#include +#include +#include +#include + + +SESSION teaui_gitea_session(const char *instance){ + curl_global_init(0); + CURL *handle = curl_easy_init(); + SESSION ret = {.handle=handle}; + memcpy(ret.instance, instance, strlen(instance)); + return ret; +} + +void teaui_gitea_cleanup(SESSION s){ + curl_global_cleanup(); + curl_easy_cleanup(s.handle); +} + +void teaui_gitea_auth_basic(SESSION s, const char *user, const char *pass){ + curl_easy_setopt(s.handle, CURLOPT_USERNAME, user); + curl_easy_setopt(s.handle, CURLOPT_PASSWORD, pass); +} + +void teaui_gitea_auth_token(SESSION s, const char *token){ + char header[256] = "Authorization: "; + strcat(header, token); + curl_easy_setopt(s.handle , CURLOPT_HEADERDATA, header); +} + +const char* teaui_gitea_auth_generateToken(SESSION s, const char * username){ + char endpoint[1024]; + strcpy(endpoint, s.instance); + strcat(endpoint, "/api/v1/users/"); + strcat(endpoint, username); + strcat(endpoint, "/tokens"); + curl_easy_setopt(s.handle, CURLOPT_URL, endpoint); + curl_easy_perform(s.handle); + return ""; +} diff --git a/giteaAPI.h b/giteaAPI.h new file mode 100644 index 0000000..2b6da65 --- /dev/null +++ b/giteaAPI.h @@ -0,0 +1,19 @@ +#ifndef GITEA_API_H_ +#define GITEA_API_H_ + +#include + +typedef struct { + CURL* handle; + char* instance; +} SESSION; + +SESSION teaui_gitea_session(const char *instance); +void teaui_gitea_cleanup(SESSION s); + +void teaui_gitea_auth_basic(SESSION s, const char *user, const char *pass); +void teaui_gitea_auth_token(SESSION s, const char *token); + +const char* teaui_gitea_auth_generateToken(SESSION s, const char *username); + +#endif /* ifndef GITEA_API_H_ */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..9bce6ed --- /dev/null +++ b/main.c @@ -0,0 +1,40 @@ +#include +#include +#include "giteaAPI.h" + +#include + +int main(void) +{ + SESSION s = teaui_gitea_session("https://git.herisson.ovh"); + +#if 0 + initscr(); + start_color(); + init_pair(1, COLOR_RED, COLOR_GREEN); + WINDOW* test = newwin(16, 9, 2, 2); + wattron(test, COLOR_PAIR(1)); + box(test, 0, 0); + wattroff(test, COLOR_PAIR(1)); + noecho(); + timeout(0); + refresh(); + bool should_quit = false; + while(!should_quit){ + wrefresh(test); + int in = getch(); + if(in != ERR){ + switch (in) { + case 'a': + should_quit = true; + break; + case 'd': + wattron(test, COLOR_PAIR(1)); + wprintw(test, "Hello, Dari!"); + wattroff(test, COLOR_PAIR(1)); + } + } + } + endwin(); +#endif +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index 3189280..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE - main.cpp -) diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index b8d3908..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main(int argc, char *argv[]) -{ - printf("Hello, World!"); - return 0; -}